%% Bandpass Filtering of Sinusoids
% Bandpass filter a discrete-time sine wave signal which consists of three sinusoids at frequencies, 1 kHz, 10 kHz, and 15 kHz.
% Design an FIR Equiripple bandpass filter by first creating a bandpass filter design specifications object, and then designing a filter using these specifications.
%
%% Design Bandpass Filter
% Create a bandpass filter design specifications object using fdesign.bandpass.

bandpassSpecs = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2', ...
    1/4,3/8,5/8,6/8,60,1,60);

% List the available design methods for this object.

designmethods(bandpassSpecs)

% To design an Equiripple filter, pick 'equiripple'.

bpFilter = design(bandpassSpecs,'equiripple','Systemobject',true)

% Visualize the frequency response of the designed filter.

fvtool(bpFilter,'Fs',44100)

%
%% Create Sinusoidal Signal
% Create a signal that is a sum of three sinusoids with frequencies at 1 kHz, 10 kHz, and 15 kHz. Initialize Spectrum Analyzer to view the original signal and the filtered signal.

Sine1 = dsp.SineWave('Frequency',1e3,'SampleRate',44.1e3,'SamplesPerFrame',4000);
Sine2 = dsp.SineWave('Frequency',10e3,'SampleRate',44.1e3,'SamplesPerFrame',4000);
Sine3 = dsp.SineWave('Frequency',15e3,'SampleRate',44.1e3,'SamplesPerFrame',4000);

SpecAna = dsp.SpectrumAnalyzer('PlotAsTwoSidedSpectrum',false, ...
    'SampleRate',Sine1.SampleRate, ...
    'NumInputPorts',2,...
    'ShowLegend',true, ...
    'YLimits',[-240,45]);

SpecAna.ChannelNames = {'Original noisy signal','Bandpass filtered signal'};

%
%% Filter Sinusoidal Signal
% Filter the sinusoidal signal using the bandpass filter that has been designed. View the original signal and the filtered signal in the Spectrum Analyzer. The tone at 1 kHz is filtered out and attenuated. The tone at 10 kHz is unaffected, and the tone at 15 kHz is mildly attenuated because it appears in the transition band of the filter.

for i = 1 : 1000
    x = Sine1()+Sine2()+Sine3();
    y = bpFilter(x);
    SpecAna(x,y);
end
release(SpecAna)

%
% Copyright 2012 The MathWorks, Inc.