1. 代码
% For data sampled at 1000 Hz, design a lowpass filter with lessthan 3 dB
% of ripple in the passband, defined from 0 to 40 Hz, and at least60 dB of
% attenuation in the stopband, defined from 150 Hz to the Nyquistfrequency
% (500 Hz). Plot the filter's frequency response and implement thisfilter
% for a 1V DC signal corrupted by noise signal given as1000*sin(2*pi*150*t)
%% Filter Design
sa =1000;fn =sa/2;% Sampling frequency (1000Hz), Nyquist frequency(500Hz)
fp =40;fs =150;% Passband (0~40Hz), Stop band(150Hz-500Hz)
Wp =fp/fn;Ws = 150/fn;% Normalized passband (40/500), stopband (150/500)
Rp =3;Rs =60;% ripple less than 3 dB, attenuation larger than 60dB
[n,Wn] = buttord(Wp,Ws,Rp,Rs); % Returns n =5; Wn=0.0810;
[b,a] =butter(n,Wn);% Designde signs an order n lowpass digital
% Butterworth filter with normalized cutoff
% frequency Wn. It returns the filter coefficients
% in length n+1 row vectors b and a, with
% coefficients in descending powers of z.
% b(1) + b(2)*z^(-1) + ... + b(n+1)*z^(-n)
% -----------------------------------------
% 1 +a(2)*z^(-1) + ... + a(n+1)*z^(-n)
freqz(b,a,512,sa);% returns the frequency response vector h and
% the corresponding frequency vector f for
% the digital filter whose transfer function
% is determined by the (real or complex) numerator
% and denominator polynomials represented in
% the vectors b and a, respectively. The vectors h
% and f are both of length n. For this syntax,
% the frequency response is calculated using the
% sampling frequency specified by the scalar fs (in hertz).
% The frequency vector f is calculated in units of hertz(Hz).
% The frequency vector f has values ranging from 0 to fs/2Hz.
title('n=5 Butterworth Lowpass Filter');
%% Filter Implementation
t =0:(1/sa):0.5;% Sampling time series at sampling frequency sa(Hz)
x = ones(1,length(t));% DC signal 1V
n = 1000 *sin(2*pi*fs*t);% Sinewave sampled at 150Hz, with magnitude 1000V.
% Note that the designed filter can suppress
% the noise signal at 150Hz -60dB, which means
% 20*log10(1/1000) = -60dB. This noise signal noise
% should be suppressed to a sine wave with magnitude
% equal to 1.
xn = x +n;% Corrupted Signal
y = filter(b, a,xn);% Implement designed filter
figure(1);
subplot(2,1,1);
plot(t, xn);
title('Before Filter');
xlim([0.1 0.5]);
subplot(2,1,2);
plot(t, y);
title('After Filter');
xlim([0.1 0.5]);
2. 实验结果
实验结果清晰的表明,原本幅度高达1000V的噪声信号已经被抑制到1V。信号衰减60dB。当我们将
Rs设为100时,原噪声信号被进一步衰减为幅度0.01V的微弱噪声信号。