MATLAB 低通滤波器按钮回调函数

该函数为低通滤波器按钮的回调函数,当用户点击该按钮时,会根据用户选择的滤波器类型和截止频率对全局变量 'x' 进行滤波操作,并在图形界面上显示滤波后的信号和滤波器的频率响应。

function low_pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to low_pushbutton5 (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=1000;
% fs = 1000;
% Wp = 2*fp/Fs;
% Ws = 2*fs/Fs;
% if(Wp >= 1)
%     Wp = 0.99;
% end
% if(Ws >= 1)
%     Ws = 0.99;  
% end
fp =  get(handles.edit3,'string');     
fp = str2double(fp)*2;
if get(handles.radiobutton1,'value')
    [n, Wn]=buttord(Wp,Ws, 2, 15);
    [b, a]=butter(n, Wn,'low');
    axes(handles.axes3);
    [h,w]=freqz(b,a);
    plot(w/pi*Fs/2,abs(h)); 
    x1=filter(b,a,x1);        % 璋冪敤鍑芥暟婊ゆ尝
elseif get(handles.radiobutton4,'value')
    b2=fir1(30, fp/Fs, boxcar(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton5,'value')
    b2=fir1(30, fp/Fs, triang(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton6,'value')
    b2=fir1(30, fp/Fs, hamming(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton7,'value')
    b2=fir1(30, fp/Fs, hanning(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton8,'value')
    b2=fir1(30, fp/Fs, blackman(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton9,'value')
    b2=fir1(30,fp/Fs, kaiser(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
end;
axes(handles.axes5);
plot(tl,x1);
title('婊ら櫎鍣0鍚庤闊虫椂鍩熸尝褰?');
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;

代码详细解释内容:

该函数实现以下功能:

  1. 获取全局变量: x (原始信号), Fs (采样频率), tl (时间轴), x2 (滤波后的信号)。
  2. 读取截止频率: 从 GUI 界面上的 edit3 输入框获取截止频率并进行转换。
  3. 根据用户选择的滤波器类型计算滤波器系数:
    • Butterworth 滤波器: 使用 buttordbutter 函数计算滤波器系数。
    • FIR 滤波器: 使用 fir1 函数计算滤波器系数,并可以选择不同的窗函数进行滤波,包括 boxcar, triang, hamming, hanning, blackman, kaiser 等。
  4. 对信号进行滤波: 根据选择的滤波器类型使用 filterfftfilt 函数对信号进行滤波,得到滤波后的信号 x1
  5. 可视化滤波后的结果:
    • axes3 坐标轴上绘制滤波器的频率响应。
    • axes5 坐标轴上绘制滤波后的信号。
    • axes6 坐标轴上绘制滤波后的信号的频谱。
  6. 更新全局变量: 将滤波后的信号 x1 赋值给全局变量 x2

该函数通过结合 GUI 界面和信号处理功能,方便用户选择不同的滤波器类型和截止频率,并直观地观察滤波后的结果,有效地帮助用户进行信号处理分析。

MATLAB 低通滤波器按钮回调函数 - 信号处理和可视化

原文地址: https://www.cveoy.top/t/topic/nKfh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录