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

1．通过窗函数法设计FIR滤波器
2．使用窗函数法设计FIR滤波器，了解窗函数的形式和长度对滤波器性能的影响。

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
% 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');

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');


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');


10-04

11-24
01-02 2139
12-03
06-26 3万+
05-24 3万+
11-27 2万+
01-04 9999