%% Lowpass Filtering of Sinusoids
% Lowpass filter a discrete-time signal consisting of two sine waves.
% Create a lowpass filter specification object. Specify the passband frequency to be 0.15\pi rad/sample and the stopband frequency to be 0.25\pi rad/sample. Specify 1 dB of allowable passband ripple and a stopband attenuation of 60 dB.

d = fdesign.lowpass('Fp,Fst,Ap,Ast',0.15,0.25,1,60);

%
% Query the valid design methods for your filter specification object, d.

designmethods(d)

%
% Create an FIR equiripple filter and view the filter magnitude response with fvtool.

Hd = design(d,'equiripple');
fvtool(Hd)

%
% Create a signal consisting of the sum of two discrete-time sinusoids with frequencies of \pi/8 and \pi/4 rad/sample and amplitudes of 1 and 0.25, respectively. Filter the discrete-time signal with the FIR equiripple filter object, Hd.

n = 0:159;
x = (0.25*cos((pi/8)*n)+sin((pi/4)*n));
y = filter(Hd,x);

%
% Compute the Fourier transform of the original signal and the filtered signal. Verify that the high-frequency component has been filtered out.

freq = 0:(2*pi)/160:pi;
xdft = fft(x);
ydft = fft(y);

figure
plot(freq/pi,abs(xdft(1:length(x)/2+1)))
hold on
plot(freq/pi,abs(ydft(1:length(y)/2+1)))
hold off

legend('Original Signal','Filtered Signal')
ylabel('Magnitude')
xlabel('Normalized Frequency (\times\pi rad/sample)')

% Copyright 2012 The MathWorks, Inc.