程序清单有
% 50Hz notch filter
% sample frequency=400
%
clear all;
clc;
b=[1 -sqrt(2) 1];
a=[1 -sqrt(2)*0.999 0.999];
[db, mag, pha, grd, w]=freqz_m(b, a);
subplot(221); plot(w*200/pi, db); title(' Magnitude Response' );
xlabel('frequency in Hz'); ylabel('dB'); axis([0, 100, -200, 5]);
set(gca, 'XTickMode', 'manual', 'XTick', [0, 50, 100]);
set(gca, 'YTickmode', 'manual', 'YTick', [-200, -100, 0]); grid
title('Notch filter response');
t0=1:8000;
t=1:256;
t1=1:100;
t2=1:128;
x=sin(2*pi*50*t0/400)+0.5*sin(2*pi*100*t0/400);
x1=x(t);
y=filter(b,a,x1);
subplot(222); plot(x1);
title('Original waveform');
X=fft(x1);
subplot(223); plot(t2*400/256,abs(X(t2)));
xlabel('frequency in Hz'); ylabel('|H|'); axis([0, 200, 0, 150]);
title('Spectrum for original');
set(gca, 'XTickMode', 'manual', 'XTick', [0, 50, 100, 150]);
set(gca, 'YTickmode', 'manual', 'YTick', [50, 100]); grid
y=filter(b,a,x);
x1=y(t+7600);
X=fft(x1);
subplot(224); plot(t2*400/256,abs(X(t2)));
xlabel('frequency in Hz'); ylabel('|H|'); axis([0, 200, 0, 150]);
title('Spectrum after filter');
set(gca, 'XTickMode', 'manual', 'XTick', [0, 50, 100, 150]);
set(gca, 'YTickmode', 'manual', 'YTick', [50, 100]); grid
figure(2);
subplot(611);plot(x(t1)); axis([1, 100, -1.5, 1.5]); ylabel('input x');
set(gca, 'YTickmode', 'manual', 'YTick', [-1,-0.5,0, 0.5,1]); grid
subplot(612);plot(y); axis([1, 100, -1.5, 1.5]); ylabel('first');
set(gca, 'YTickmode', 'manual', 'YTick', [-1,-0.5,0,0.5,1]); grid
subplot(613);plot(y); axis([401, 500, -1.5, 1.5]); ylabel('second');
set(gca, 'YTickmode', 'manual', 'YTick', [-1,-0.5,0, 0.5,1]); grid
subplot(614);plot(y); axis([1201, 1300, -1.0, 1.0]); ylabel('forth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5,0, 0.5]); grid
subplot(615);plot(y); axis([2000, 2100, -1.0, 1.0]); ylabel('sixth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5,0, 0.5]); grid
subplot(616);plot(y); axis([3601, 3700, -1.0, 1.0]); ylabel('tenth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5,0, 0.5]); grid
figure(3);
subplot(611);plot(y); axis([4401, 4500, -1, 1]); ylabel('twelfth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5,0, 0.5]); grid
subplot(612);plot(y); axis([5201, 5300, -1.0, 1.0]); ylabel('fourteenth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5,0, 0.5]); grid
subplot(613);plot(y); axis([6001, 6100, -1.0, 1.0]); ylabel('sixteenth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5,0, 0.5]); grid
subplot(212);plot(y); axis([7601, 7650, -1.0, 1.0]); ylabel('twentieth');
set(gca, 'YTickmode', 'manual', 'YTick', [-0.5, 0, 0.5]); grid
滤波器频响,输入和输出的谱图