【滤波器】基于matlab GUI高通+低通+带通+带阻FIR滤波器设计【含Matlab源码 699期】

231 阅读4分钟

一、简介

1 设计原理
在这里插入图片描述
1.1 滤波器概念
在这里插入图片描述
1.2 数字滤波器的系统函数和差分方程
在这里插入图片描述
1.3 数字滤波器结构的表示
在这里插入图片描述
在这里插入图片描述
1.4 数字滤波器的分类
在这里插入图片描述
在这里插入图片描述
2.1 IIR滤波器与FIR滤波器的分析比较
在这里插入图片描述
2.2 FIR滤波器的原理

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3 FIR滤波器的仿真步骤
在这里插入图片描述
在这里插入图片描述

二、源代码

%--------------------------------------------------------------------------
%利用kaiser窗设计低通滤波器m文件
%默认输入参数:   N=64
%                beta=5.568  
%                wc=0.5pi
%输出参数:      通带边界(wp)
%               阻带边界(ws)
%               通带波纹
%               阻带衰减
%--------------------------------------------------------------------------
function varargout = lpfilter(varargin)
% LPFILTER M-file for lpfilter.fig
%      LPFILTER, by itself, creates a new LPFILTER or raises the existing
%      singleton*.
%
%      H = LPFILTER returns the handle to a new LPFILTER or the handle to
%      the existing singleton*.
%
%      LPFILTER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LPFILTER.M with the given input arguments.
%
%      LPFILTER('Property','Value',...) creates a new LPFILTER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before lpfilter_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to lpfilter_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help lpfilter

% Last Modified by GUIDE v2.5 29-Jun-2007 13:21:40

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @lpfilter_OpeningFcn, ...
                   'gui_OutputFcn',  @lpfilter_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before lpfilter is made visible.
function lpfilter_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to lpfilter (see VARARGIN)

% Choose default command line output for lpfilter
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes lpfilter wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%--------------------------------------------------------------------------
%设置滤波器长度,beta和截止频率编辑框的初始值
%设置窗体的标题
set(handles.figure1,'name','FIR低通滤波器');
set(handles.edit_N,'string','64');
set(handles.edit_beta,'string','5.568');
set(handles.edit_fc,'string','0.5');
%--------------------------------------------------------------------------
%由设置的初始值求取滤波器的理想单位脉冲响应
handles.wc=0.5*pi;
handles.M=64;
handles.hd=ideal_lp(handles.wc,handles.M);
%--------------------------------------------------------------------------
%绘制滤波器的理想单位脉冲响应
axes(handles.axes_hd);
n=[0:1:handles.M-1];
plot(n,handles.hd);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55


title('滤波器理想脉冲响应');
axis([0 handles.M-1 min(handles.hd) max(handles.hd)+0.01]);
xlabel('n');
ylabel('hd(n)');
grid on;
guidata(hObject,handles);

% --- Executes on button press in plot.
function plot_Callback(hObject, eventdata, handles)
% hObject    handle to plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%--------------------------------------------------------------------------
%从编辑框获取滤波器长度,beta和截止频率的值
handles.M = str2double(get(handles.edit_N,'String'));
handles.beta = str2double(get(handles.edit_beta,'String'));
handles.wc = str2double(get(handles.edit_fc,'String'))*pi;
%求取滤波器的理想单位脉冲响应
handles.hd=ideal_lp(handles.wc,handles.M);
%绘制滤波器的理想单位脉冲响应
axes(handles.axes_hd);
plot(0,0);
n=[0:1:handles.M-1];
plot(n,handles.hd);
title('滤波器理想脉冲响应');
axis([0 handles.M-1 min(handles.hd) max(handles.hd)]);
xlabel('n');
ylabel('hd(n)');
grid on;
%--------------------------------------------------------------------------
%求取kaiser窗函数
handles.w_kai=(kaiser(handles.M,handles.beta))';
%绘制kaiser窗函数
axes(handles.axes_wkaiser);
plot(n,handles.w_kai);
title('kaiser窗');
axis([0 handles.M-1 min(handles.w_kai) max(handles.w_kai)]);
xlabel('n');
ylabel('w(n)');
grid on;
%--------------------------------------------------------------------------
%求取滤波器的实际单位脉冲响应
handles.h=handles.hd.*handles.w_kai;
%绘制滤波器的实际单位脉冲响应
axes(handles.axes_h);
plot(n,handles.h);
title('滤波器实际脉冲响应');
axis([0 handles.M-1 min(handles.h) max(handles.h)]);
xlabel('n');
ylabel('h(n)');
grid on;
%--------------------------------------------------------------------------
%求取滤波器的频率特性,并绘制幅频特性(转换到0—pi)
[H,w] = freqz(handles.h,[1],handles.M*6,'whole');
% freqz(handles.h,[1],handles.M*6);
H=(H(1:1:handles.M*3))';
w=(w(1:1:handles.M*3))';
mag=abs(H);
db=20*log10((mag+eps)/max(mag));
% pha=angle(H);
%绘制幅频特性(转换到0—pi)
axes(handles.axes_lp);
plot(w/pi,db);
title('FIR滤波器的幅频特性(0-pi)');
axis([0 1 min(db) max(db)]);
xlabel('Frequency in pi units');
ylabel('Decibels');
grid on;
guidata(hObject,handles);
%--------------------------------------------------------------------------
%由求得幅频特性计算通带边界,阻带边界,通带纹波和阻带衰减
%由beta值计算阻带衰减As
if handles.beta<4.5513
    As=fzero(@myfun,20,[],handles.beta)+21;
else
    As=handles.beta/0.1102+8.7;
end
%计算通带边界wp,阻带边界ws
w_w=(As-7.95)/((handles.M-1)*14.36)*2;
ws=0.5*(w_w+2*handles.wc/pi);
wp=0.5*(2*handles.wc/pi-w_w);
% mag=(mag+eps)/max(mag);
%计算通带纹波rp
% deta_w=2*pi/(handles.M*6);
% w_temp=mag(floor(ws/deta_w));
% mag_temp=abs(mag(1:floor(ws/deta_w))-w_temp);
rp=20*log10(max(mag));
%--------------------------------------------------------------------------
%在相应的编辑框中显示通带边界,阻带边界,通带纹波和阻带衰减
str=sprintf('%.2f',wp);
set(handles.edit_wp,'string',str);
str=sprintf('%.2f',ws);
set(handles.edit_ws,'string',str);
str=sprintf('%.4f',rp);
set(handles.edit_rp,'string',str);
str=sprintf('%.2f',-As);
set(handles.edit_As,'string',str);
%--------------------------------------------------------------------------
%退出程序
function exit_Callback(hObject, eventdata, handles)
% hObject    handle to exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(handles.figure1);

%--------------------------------------------------------------------------
%自定义函数,计算理想低通滤波器的单位脉冲响应
function hd=ideal_lp(wc,M);
alpha=(M-1)/2;
n = [0:1:(M-1)];
m=n-alpha+eps;
hd=sin(wc*m)./(pi*m);
% --- Outputs from this function are returned to the command line.
function varargout = lpfilter_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function edit_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_N_Callback(hObject, eventdata, handles)
% hObject    handle to edit_N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_N as text
%        str2double(get(hObject,'String')) returns contents of edit_N as a double


% --- Executes during object creation, after setting all properties.
function edit_beta_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_beta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_beta_Callback(hObject, eventdata, handles)
% hObject    handle to edit_beta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_beta as text
%        str2double(get(hObject,'String')) returns contents of edit_beta as a double

三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a