www.gusucode.com > signal 案例源码程序 matlab代码 > signal/AttenuationOfLowFrequenciesExample.m

    %% Attenuation of Low Frequencies
% Load the MAT-file |chirp|. The file contains a signal, |y|, sampled at a
% frequency ${\tt Fs}=8192\;\rm Hz$. |y| has most of its power above ${\tt
% Fs}/4$, or half the Nyquist frequency. Add random noise to the signal.

% Copyright 2015 The MathWorks, Inc.


%%

load chirp
y = y + 0.25*(rand(size(y))-0.5);

%%
% Design a 34th-order FIR highpass filter to attenuate the components of
% the signal below ${\tt Fs}/4$. Specify a cutoff frequency of 0.48.
% Visualize the frequency response of the filter.

f = [0 0.48 0.48 1];
mhi = [0 0 1 1];
bhi = fir2(34,f,mhi);

freqz(bhi,1)

%%
% Filter the chirp signal. Plot the signal before and after filtering.

outhi = filter(bhi,1,y);
t = (0:length(y)-1)/Fs;

subplot(2,1,1)
plot(t,y)
title('Original Signal')
ylim([-1.2 1.2])

subplot(2,1,2)
plot(t,outhi)
title('Higpass Filtered Signal')
xlabel('Time (s)')
ylim([-1.2 1.2])

%%
% Change the filter from highpass to lowpass. Use the same order and
% cutoff. Filter the signal again. The result is mostly noise.

mlo = [1 1 0 0];
blo = fir2(34,f,mlo);
outlo = filter(blo,1,y);

subplot(2,1,1)
plot(t,y)
title('Original Signal')
ylim([-1.2 1.2])

subplot(2,1,2)
plot(t,outlo)
title('Lowpass Filtered Signal')
xlabel('Time (s)')
ylim([-1.2 1.2])