课程设计
题目:用汉宁窗设计FIR数字
低通滤波器对给定数据进行滤波
院系:
专业:通 信 工 程
班级:
学号:
姓名:
指导教师:
职称:
1.1 FIR滤波器:
有限长单位脉冲响应数字滤波器(Finite Impulse Response Digital Filter,缩写FIRDF):有限长单位冲激响应滤波器,是数字信号处理系统中最基本的元件,最大优点是可以实现线性相性滤波,它可以在保证任意幅频特性的同时具有严格的线性相频特性,同时其单位抽样响应是有限长的,因而滤波器是稳定的系统。因此,FIR滤波器在通信、图像处理、模式识别等领域都有着广泛的应用。FIR滤波器的设计方法主要分为两类:第一类是基于逼近理想滤波器器特性的方法包括窗函数法、频率采样法、和等波纹最佳逼近法;第二类是最优设计法。
设FIRDF的单位脉冲响应的长度为,则其频率响应函数为
(1-1)
一般将表示成如下形式:
(1-2)
式中,是的实函数(可以去负值)。与前面的表示形式,即相比,与不同。与不同。为了区别于幅频响应函数和相频响应函数,称为幅频特性函数,称为相频特性函数。
第一类线性相位FIRDF的相位特性函数是的严格线性函数:
(1-3)
第二类线性相位FIRDF的相位特性函数如下:
(1-4)
式中,是常数,是起始相位。在信号处理中很有实用价值(如希伯尔特变换器),这是FIRDF除了线性相位滤波外,还具有真正交变换作用。
1.2 窗函数设计法:
窗函数设计法的基本思想是用FIRDF逼近希望的滤波特性。设希望逼近的滤波器的频率响应为,其单位脉冲响应用表示。为了设计简单方便,通常选择为具有片段常数特性的理想滤波器。因此是无限长非因果序列,不能直接作为FIRDF的单位脉冲响应。窗函数设计法就是截取为有限长的一段因果序列,并用合适的窗口函数进行加权作为FIRDF的单位脉冲响应。
常见的窗函数,可以分为以下主要类型,
(1) 幂窗--采用时间变量某种幂次的函数,如矩形、三角形、梯形或其它时间(t)的高次幂;
(2)三角函数窗--应用三角函数,即正弦或余弦函数等组合成复合函数,例如汉宁窗、海明窗等;
(3) 指数窗--采用指数时间函数,例如高斯窗等[5]。
其性能如表1-1所示:
名称 | 滤波器 过渡带宽 | 最小阻带衰减 | 名称 | 滤波器 过渡带宽 | 最小阻带衰减 |
矩形 | 1.8π/M | 21dB | PARZENWIN | 6.6π/M | 56db |
巴特利特 | 6.1π/M | 25dB | FLATTOPWIN | 19.6π/M | 108db |
汉宁 | 6.2π/M | 44dB | GAUSSWIN | 5.8π/M | 60db |
汉明 | 6.6π/M | 51dB | BARTHANNWIN | 3.6π/M | 40db |
布莱克曼 | 11π/M | 74dB | BLACKMANHARRIS | 16.1π/M | 109db |
BOHMANWIN | 5.8π/M | 51.5db | CHEBWIN | 15.2π/M | 113db |
NUTTALLWIN | 15.4π/M | 108db | TUKEYWIN | 2.4π/M | 22db |
用窗口设计法基本步骤如下:
(1)构造希望逼近的频率响应函数。以低通线性相位FIRDF设计为例,一般选择为线性理想低通滤波器,即
(1-5)
(2)求出。对进行IFT得到
(1-6)
(3)加窗得到FIRDF的单位脉冲响应,
(1-7)
式中,称为窗口函数,其长度为。如果要求第一类线性相位FIRDF,则要求关于点偶对称。而关于点偶对称,所,同时要求关于点偶对称。
1.3 汉宁窗:
汉宁窗(Hanning Window)又称升余弦窗,汉宁窗可以看作是3个矩形时间窗的频谱之和,或者说是3个型函数之和,而括号中的两项相对于第一个谱窗向左、右各移动了π/T,从而使旁瓣互相抵消,消去高频干扰和漏能。可以看出,汉宁窗主瓣加宽并降低,旁瓣则显著减小,从减小泄漏观点出发,汉宁窗优于矩形窗.但汉宁窗主瓣加宽,相当于分析带宽加宽,频率分辨力下降。
(1-8)
根据傅里叶变换的线性性质和调制定理得到
(1-9)
当时,,则
(1-10)
为汉宁窗的幅度响应函数。
二、设计流程图:
本课程设计主要是对一段数据,加入噪声后,用汉宁窗函数法设计出的FIR滤波器对加入噪声后的数据信号进行滤波去噪处理,并且分析对比前后时域和频域波形的程序设计。程序的设计流程图如下图2-1所示:
图2-1 程序设计流程图
三、matlab设计函数及结果:
用汉宁窗设计的FIR数字低通滤波器的程序(含界面):
function varargout = hanning(varargin)
% HANNING M-file for hanning.fig
% HANNING, by itself, creates a new HANNING or raises the existing
% singleton*.
%
% H = HANNING returns the handle to a new HANNING or the handle to
% the existing singleton*.
%
% HANNING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HANNING.M with the given input arguments.
%
% HANNING('Property','Value',...) creates a new HANNING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before hanning_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to hanning_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 hanning
% Last Modified by GUIDE v2.5 29-Jun-2011 11:03:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @hanning_OpeningFcn, ...
'gui_OutputFcn', @hanning_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(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 hanning is made visible.
function hanning_OpeningFcn(hObject, eventdata, handles, varargin)
global xn;global hj1;global hj2;
% 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 hanning (see VARARGIN)
hj1=load('E:\\a.txt');
xn=hj1;
% Choose default command line output for hanning
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes hanning wait for user response (see UIRESUME)
% uiwait(handles.hanning_gui);
% --- Outputs from this function are returned to the command line.
function varargout = hanning_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;
function edt_wp_Callback(hObject, eventdata, handles)
% hObject handle to edt_wp (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 edt_wp as text
% str2double(get(hObject,'String')) returns contents of edt_wp as a double
% --- Executes during object creation, after setting all properties.
function edt_wp_CreateFcn(hObject, eventdata, handles)
% hObject handle to edt_wp (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edt_ws_Callback(hObject, eventdata, handles)
% hObject handle to edt_ws (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 edt_ws as text
% str2double(get(hObject,'String')) returns contents of edt_ws as a double
% --- Executes during object creation, after setting all properties.
function edt_ws_CreateFcn(hObject, eventdata, handles)
% hObject handle to edt_ws (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btn_creat.
function btn_creat_Callback(hObject, eventdata, handles)
global xn;global hj1;global hj2;
% hObject handle to btn_creat (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
H=findobj('Tag','edt_wp');aaa=get(H, 'string');wp=str2num(aaa)*pi;
H=findobj('Tag','edt_ws');aaa=get(H, 'string');ws=str2num(aaa)*pi;
deltaw=ws-wp;
N0=ceil(6.2*pi/deltaw);
N=N0+mod(N0+1,2);
n=0:N-1;
wn=0.5*(1-cos(2*pi*n/(N-1)));
wc=(ws+wp)/2;
nn=(N-1)/2;
hd=sin(wc*(n-nn)+eps)./(pi*(n-nn)+eps);
h=hd.*wn;
[H,w]=freqz(h,1,1000,'whole');
H=(H(1:1:501))';
w=(w(1:1:501))';
mag=abs(H); %绝对幅值响应
db=20*log10((mag+eps)/max(mag)); %相对幅值响应
pha=angle(H); %相位响应
yn=conv(xn,h);
axes(handles.axes1);plot(h);
grid;title('实际单位冲击响应');
axes(handles.axes2);plot(w/pi,db);;
grid;title('汉宁窗滤波器的幅频特性');
axes(handles.axes3);plot(w/pi,pha);
grid;title('汉宁窗滤波器的相频特性');
axes(handles.axes4);plot(xn);
grid;title('输入信号');
axes(handles.axes5);plot(yn);
grid;title('输出信号');
% --- Executes on button press in btn_zero.
function btn_zero_Callback(hObject, eventdata, handles)
% hObject handle to btn_zero (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);plot(0);
axes(handles.axes2);plot(0);
axes(handles.axes3);plot(0);
axes(handles.axes4);plot(0);
axes(handles.axes5);plot(0);
% --- Executes on button press in btn_quit.
function btn_quit_Callback(hObject, eventdata, handles)
% hObject handle to btn_quit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(gcf);
设计结果:
输入wp=0.1;ws=0.3后,点击“生成滤波器”即可产生如图所示波形;点击“选项”菜单即可实现图形的“清除”和 “退出”。
四、标准函数调用:
用 wn=(hanning(N))' 替换 wn=0.5*(1-cos(2*pi*n/(N-1)))即可实现汉宁窗函数的调用。
五、分析和总结:
所编程序运行结果与matlab内置汉宁窗滤波器设计函数所得结果相比,带宽基本一致,误差相对较小。
通过本次课程设计,使我更加深切的了解了matlab的使用,以及简单的窗函数的设计方法。