%% Filter a Signal Using Biquadratic Filter
% *Note*: If you are using R2016a or an earlier release, replace each call to 
% the object with the equivalent step syntax. For example, |obj(x)| becomes  |step(obj,x)|.
% 
% Use a fourth order, lowpass biquadratic filter object with a normalized 
% cutoff frequency of 0.4 to filter high frequencies from an input signal. Display 
% the result as a power spectrum using the Spectrum Analyzer.

t = (0:1000)'/8e3;
xin = sin(2*pi*0.3e3*t)+sin(2*pi*3e3*t); % Input is 0.3 &amp; 
                                       % 3kHz sinusoids
src = dsp.SignalSource(xin, 100);
sink = dsp.SignalSink;
 
[z,p,k] = ellip(4,1,60,.4);    % Set up the filter
[s,g] = zp2sos(z,p,k);
biquad = dsp.BiquadFilter(s,g,'Structure','Direct form I'); 
 
sa = dsp.SpectrumAnalyzer('SampleRate',8e3,...
    'PlotAsTwoSidedSpectrum',false,...
    'OverlapPercent', 80,'PowerUnits','dBW',...
    'YLimits', [-160 -10]);

while ~isDone(src)
     input = src();
     filteredOutput = biquad(input);
     sink(filteredOutput);
     sa(filteredOutput)
end

filteredResult = sink.Buffer;
fvtool(biquad,'Fs',8000)
%% 
% Design and apply a lowpass biquad filter System object using the  |design| 
% function.
%%
lpSpec = fdesign.lowpass('Fp,Fst,Ap,Ast',500,550,0.5,60,10000);
lpfilter = design(lpSpec,'butter','systemobject',true)
fvtool(lpfilter);

%% 
% Copyright 2012 The MathWorks, Inc.