基于matlab的iir数字滤波器gui界面设计代码
时间: 2023-08-03 11:07:20 浏览: 179
下面是一个简单的基于MATLAB的IIR数字滤波器GUI界面设计代码,你可以根据自己的需求进行修改和扩展。
```matlab
function iir_filter_gui
% 创建主窗口
main_window = figure('Name','IIR数字滤波器','NumberTitle','off','Position',[200,200,300,250],'MenuBar','none','ToolBar','none');
% 创建选择滤波类型的文本和下拉菜单
uicontrol('Parent',main_window,'Style','text','String','选择滤波类型:','Position',[20,200,100,20]);
filter_type_menu = uicontrol('Parent',main_window,'Style','popupmenu','String',{'低通滤波器','高通滤波器','带通滤波器','带阻滤波器'},'Position',[130,200,120,20]);
% 创建选择滤波器阶数的文本和滑动条
uicontrol('Parent',main_window,'Style','text','String','选择滤波器阶数:','Position',[20,170,100,20]);
order_slider = uicontrol('Parent',main_window,'Style','slider','Min',1,'Max',10,'Value',5,'SliderStep',[1/9,1/9],'Position',[130,170,120,20]);
% 创建显示当前滤波器阶数的文本
order_text = uicontrol('Parent',main_window,'Style','text','String','5','Position',[260,170,30,20]);
% 创建选择滤波器截止频率的文本和滑动条
uicontrol('Parent',main_window,'Style','text','String','选择截止频率:','Position',[20,140,100,20]);
cutoff_slider = uicontrol('Parent',main_window,'Style','slider','Min',0,'Max',1,'Value',0.5,'SliderStep',[0.01,0.1],'Position',[130,140,120,20]);
% 创建显示当前截止频率的文本
cutoff_text = uicontrol('Parent',main_window,'Style','text','String','0.5','Position',[260,140,30,20]);
% 创建滤波器设计按钮
design_button = uicontrol('Parent',main_window,'Style','pushbutton','String','设计滤波器','Position',[100,100,100,30],'Callback',@design_filter);
% 创建显示滤波器响应的轴
response_axes = axes('Parent',main_window,'Position',[0.1,0.1,0.8,0.4]);
% 回调函数,用于设计滤波器并绘制其响应
function design_filter(~,~)
% 获取用户选择的滤波器类型、阶数和截止频率
filter_type = filter_type_menu.Value;
order = round(order_slider.Value);
cutoff = cutoff_slider.Value;
% 根据用户选择的滤波器类型创建滤波器对象
switch filter_type
case 1
filter_object = designfilt('lowpassiir','FilterOrder',order,'PassbandFrequency',cutoff);
case 2
filter_object = designfilt('highpassiir','FilterOrder',order,'PassbandFrequency',cutoff);
case 3
filter_object = designfilt('bandpassiir','FilterOrder',order,'PassbandFrequency1',cutoff-0.1,'PassbandFrequency2',cutoff+0.1);
case 4
filter_object = designfilt('bandstopiir','FilterOrder',order,'PassbandFrequency1',cutoff-0.1,'PassbandFrequency2',cutoff+0.1);
end
% 获取滤波器响应
[h, w] = freqz(filter_object);
% 绘制滤波器响应曲线
plot(response_axes,w/pi,20*log10(abs(h)));
xlabel(response_axes,'归一化频率');
ylabel(response_axes,'增益(dB)');
title(response_axes,'IIR数字滤波器响应');
end
% 回调函数,用于更新滤波器阶数显示文本
function update_order_text(~,~)
order_text.String = num2str(round(order_slider.Value));
end
% 回调函数,用于更新截止频率显示文本
function update_cutoff_text(~,~)
cutoff_text.String = num2str(cutoff_slider.Value);
end
% 为阶数滑动条和截止频率滑动条添加回调函数
addlistener(order_slider,'Value','PostSet',@update_order_text);
addlistener(cutoff_slider,'Value','PostSet',@update_cutoff_text);
end
```
运行该代码后,会弹出一个窗口,你可以选择滤波器类型、滤波器阶数和截止频率,点击“设计滤波器”按钮后,程序会根据你的选择设计滤波器并在窗口中绘制出其响应曲线。
阅读全文