DSP

在matlab中使用不同的窗函数构造FIR数字低通滤波器

2019-07-13 11:24发布

目的:
1.通过窗函数法设计FIR滤波器
2.使用窗函数法设计FIR滤波器,了解窗函数的形式和长度对滤波器性能的影响。 原理:
ex5.1 实现环境:
Matlab 理想低通滤波器设计: function my_output=ideallp(wc,N) %Ideal Lowpass filter computation %------------------------------------ %[hd]=ideal_lp(wc,M) % hd=ideal impulse response between 0 to M-1 % wc=cutoff frequency in radians % M=length of the ideal filter % alpha=(N-1)/2; n=0:1:(N-1); m=n-alpha+eps; my_output=sin(wc*m)./(pi*m); end
  1. Boxcar窗
wp=0.2*pi; ws=0.3*pi; deltaw=ws-wp; N=ceil(1.8*pi/deltaw); wc=(wp+ws)/2; hd=ideallp(wc,N); wd1=boxcar(N)'; h1=hd.*wd1; [H1,w]=freqz(h1,1); plot(w/pi,20*log10(abs(H1))); title('矩形窗'); 得到频率响应曲线如下: 2.Kaiser窗 wp=0.2*pi; ws=0.5*pi; As=50; deltaf=(ws-wp)/(2*pi); N=ceil((As-7.95)/(14.36*deltaf))+1; % beta=0.1102*(As-8.7); wdkai=kaiser(N,beta)'; wc=(ws+wp)/2; hd=ideallp(wc,N); h=hd.*wdkai; [H,w]=freqz(h,1); plot(w/pi,20*log10(abs(H))); title('凯泽窗'); 得到频率响应图像如下: 3.triang窗 wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.1*pi/deltaw); wdtri=triang(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdtri; [H,w]=freqz(h,1); plot(w/pi,20*log10(abs(H))); title('Triang'); 得到频率响应图像如下: 4.hanning窗 wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.2*pi/deltaw); wdhann=hanning(N)’; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdhann; [H,w]=freqz(h,1); plot(w/pi,20*log10(abs(H))); title(‘Hanning’); 得到频率响应图像如下: 5.hamming窗 wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.6*pi/deltaw); wdhamm=hamming(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdhamm; [H,w]=freqz(h,1); plot(w/pi,20*log10(abs(H))); title('Hamming'); 得到频率响应图像如下: 6.blackman窗 wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.1*pi/deltaw); wdblack=blackman(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdblack; [H,w]=freqz(h,1); plot(w/pi,20*log10(abs(H))); title('Blackman'); 得到频率响应图像如下:

一、 主瓣的宽度由N决定,旁瓣的衰减由窗函数的形状决定。
二、 Kaiser的不同在于可以同时调整主瓣宽度和旁瓣衰减,这是其他窗函数不具备的。 三、 实际滤波的应用
设置初始信号为10Hz的余弦与40Hz余弦叠加而成的信号,分别采用以上滤波方法滤波。
  1. boxcar
%初始信号 t=0:0.01:2; y=cos(2*pi*10*t)+cos(2*pi*40*t); subplot(2,1,1); plot(t,y); title('initial signal'); yfft=fftshift(fft(y)); w=-50:0.5:50; subplot(2,1,2); plot(w,abs(yfft)); axis([0 50 0 100]); title('frequence'); %滤波器内容 wp=0.2*pi; ws=0.3*pi; deltaw=ws-wp; N=ceil(1.8*pi/deltaw); wc=(wp+ws)/2; hd=ideallp(wc,N); wd1=boxcar(N)'; h1=hd.*wd1; final=fftfilt(h1,y,200); %滤波之后的信号 figure; subplot(2,1,1); plot(final); title('processed signal'); ynfft=fftshift(fft(final)); subplot(2,1,2); plot(w,abs(ynfft)); axis([0 50 0 100]); title('frequence filted'); 运行结果如下:
初始信号如下

处理后信号如下:

可见40Hz分频成分明显降低,而10Hz分频保留较好。
同理,更改代码中的滤波器部分分别得到下面几个滤波结果对比
2. Kaiser t=0:0.01:2; y=cos(2*pi*10*t)+cos(2*pi*40*t); w=-50:0.5:50; wp=0.2*pi; ws=0.5*pi; As=50; % deltaf=(ws-wp)/(2*pi); N=ceil((As-7.95)/(14.36*deltaf))+1; % beta=0.1102*(As-8.7); wdkai=kaiser(N,beta)'; wc=(ws+wp)/2; hd=ideallp(wc,N); h=hd.*wdkai; final=fftfilt(h1,y,200); subplot(2,1,1); plot(final); title('processed signal'); ynfft=fftshift(fft(final)); subplot(2,1,2); plot(w,abs(ynfft)); axis([0 50 0 100]); title('frequence filted'); 3. Triang t=0:0.01:2; y=cos(2*pi*10*t)+cos(2*pi*40*t); w=-50:0.5:50; wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.1*pi/deltaw); wdtri=triang(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdtri; final=fftfilt(h1,y,200); subplot(2,1,1); plot(final); title('processed signal'); ynfft=fftshift(fft(final)); subplot(2,1,2); plot(w,abs(ynfft)); axis([0 50 0 100]); title('frequence filted');
  1. Hanning
t=0:0.01:2; y=cos(2*pi*10*t)+cos(2*pi*40*t); w=-50:0.5:50; wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.2*pi/deltaw); wdhann=hanning(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdhann; final=fftfilt(h1,y,200); subplot(2,1,1); plot(final); title('processed signal'); ynfft=fftshift(fft(final)); subplot(2,1,2); plot(w,abs(ynfft)); axis([0 50 0 100]); title('frequence filted'); 5. Hamming t=0:0.01:2; y=cos(2*pi*10*t)+cos(2*pi*40*t); w=-50:0.5:50; wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.6*pi/deltaw); wdhamm=hamming(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdhamm; final=fftfilt(h1,y,200); subplot(2,1,1); plot(final); title('processed signal'); ynfft=fftshift(fft(final)); subplot(2,1,2); plot(w,abs(ynfft)); axis([0 50 0 100]); title('frequence filted'); 6. Blackman t=0:0.01:2; y=cos(2*pi*10*t)+cos(2*pi*40*t); w=-50:0.5:50; wp=0.2*pi; % ws=0.3*pi; As=50; deltaw=ws-wp; N=ceil(6.1*pi/deltaw); wdblack=blackman(N)'; wc=(wp+ws)/2; hd=ideallp(wc,N); h=hd.*wdblack; final=fftfilt(h1,y,200); subplot(2,1,1); plot(final); title('processed signal'); ynfft=fftshift(fft(final)); subplot(2,1,2); plot(w,abs(ynfft)); axis([0 50 0 100]); title('frequence filted');
不同滤波方式衰减过程不同,但由于此次信号只有两个分频,体现的不太明显。