MATLAB 带通滤波器回调函数实现
function bandpass_pushbutton10_Callback(~, eventdata, handles) % hObject handle to bandpass_pushbutton10 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
global x; global Fs; global tl; global x2;
x1=x;
% fp = [2000,3000];
% fs = [1800,3300];
% Wp = 2fp/Fs;
% Ws = 2fs/Fs;
% if(Wp >= 1)
% Wp = 0.99;
% end
% if(Ws >= 1)
% Ws = 0.99;
% end
fp = get(handles.edit3,'string');
fp = str2double(fp)2;
fs = get(handles.edit4,'string');
fs = str2double(fs)2;
if get(handles.radiobutton1,'value')
[n, Wn]=buttord(Wp,Ws, 2, 15);
[b, a]=butter(n, Wn,'bandpass');
axes(handles.axes3);
[h,w]=freqz(b,a);
plot(w/piFs/2,abs(h));
x1=filter(b,a,x1);
elseif get(handles.radiobutton4,'value')
b2=fir1(30,[fp/Fs fs/Fs],boxcar(31));
axes(handles.axes3);
[h,w]=freqz(b2, 1,512);
plot(w/piFs/2,20log(abs(h)));
x1=fftfilt(b2,x1);
elseif get(handles.radiobutton5,'value')
b2=fir1(30,[fp/Fs fs/Fs], triang(31));
axes(handles.axes3);
[h,w]=freqz(b2, 1,512);
plot(w/piFs/2,20log(abs(h)));
x1=fftfilt(b2,x1);
elseif get(handles.radiobutton6,'value')
b2=fir1(30,[fp/Fs fs/Fs],hamming(31));
axes(handles.axes3);
[h,w]=freqz(b2, 1,512);
plot(w/piFs/2,20log(abs(h)));
x1=fftfilt(b2,x1);
elseif get(handles.radiobutton7,'value')
b2=fir1(30,[fp/Fs fs/Fs],hanning(31));
axes(handles.axes3);
[h,w]=freqz(b2, 1,512);
plot(w/piFs/2,20log(abs(h)));
x1=fftfilt(b2,x1);
elseif get(handles.radiobutton8,'value')
b2=fir1(30,[fp/Fs fs/Fs],blackman(31));
axes(handles.axes3);
[h,w]=freqz(b2, 1,512);
plot(w/piFs/2,20log(abs(h)));
x1=fftfilt(b2,x1);
elseif get(handles.radiobutton9,'value')
b2=fir1(30,[fp/Fs fs/Fs],kaiser(31));
axes(handles.axes3);
[h,w]=freqz(b2, 1,512);
plot(w/piFs/2,20log(abs(h)));
x1=fftfilt(b2,x1);
end;
axes(handles.axes5);
plot(tl,x1);
title('高通滤波器作用下的波形图');
xlabel('鏃堕棿/s');
N=length(x1);
df=Fs/N;
w=[0:df:df(N-1)] - Fs/2; %棰戠巼灏哄害
X=fft(x1);
X=fftshift(X);
axes(handles.axes6);
plot(w,abs(X)/max(abs(X)));
axis([-10000,10000,0,1]);
title('婊ら櫎鍣0鍚庤闊抽璋?');
xlabel('棰戠巼/Hz');
grid on;
x2=x1;
代码逐句详细解释内容:该函数是一个回调函数,当用户点击GUI界面上的“带通滤波”按钮时,MATLAB会调用该函数。
首先,将全局变量x、Fs、tl、x2引入函数中。
然后,将x1初始化为x,以便在滤波过程中不改变原始信号x。
接着,根据用户在GUI界面上输入的参数,计算出所需的滤波参数fp和fs。
根据用户在GUI界面上选择的滤波器类型,计算出所需的滤波器系数b和a,并在GUI界面上显示滤波器的频率响应图。
根据所得到的滤波器系数b和a,对x1进行滤波操作。
最后,在GUI界面上显示滤波后的信号x1和傅里叶变换后的频谱图。并将滤波后的信号赋值给全局变量x2。
原文地址: https://www.cveoy.top/t/topic/nKf4 著作权归作者所有。请勿转载和采集!