📄 firdigitallowpassfilter(withwindow).m
字号:
%Design a FIR filter with window function,Parameter:Wp=0.5*pi,Ws=0.66*pi,passband ripple rp:3dB,Stopband attenuation rs:30dB.
%Assuming input signal:f1=3Hz,f2=20Hz,sample frequency:50Hz.
clear;
wp=0.5*pi;ws=0.66*pi;
deltaw=ws-wp;%The bandwidth of transition;
N=ceil(8*pi/deltaw);%The length of the window;
wc=(ws+wp)/2;
n=0:N-1;
alpha=(N-1)/2;%The delay of filter;
m=n-alpha+eps;%eps is the precision of Matlab system;
hd=sin(wc*m)./(pi*m);
win=hanning(N);%Using Hanning window;
h=hd.*win';%Convolution in Time domain;
figure(1)
[H,f]=freqz(h,1,512,50);%Sample frequency 50Hz;
subplot(2,1,1),plot(f,20*log10(abs(H)))
xlabel('Frequency/Hz');ylabel('Amplitude/dB');
grid on;
subplot(2,1,2),plot(f,180/pi*unwrap(angle(H)))
xlabel('Frequency/Hz');ylabel('Phase/^o');grid on;
f1=3;f2=20;
dt=0.02;t=0:dt:3;
x=sin(2*pi*f1*t)+cos(2*pi*f2*t);
y=fftfilt(h,x);
figure(2);
subplot(2,1,1),plot(t,x),title('Input signal')
subplot(2,1,2),plot(t,y)
hold on;
plot([1 1]*(N-1)/2*dt,ylim,'g')
xlabel('Time/s'),title('Output signal')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -