%% Design of Lowpass Filter
% Design a lowpass filter to use on a signal sampled at 96 kHz. The passband of the filter extends up to 20 kHz. The stopband of the filter starts at 24 kHz. Specify a passband ripple of 0.01 dB and a stopband attenuation of 80 dB. Determine automatically the order required to meet the specifications.
% Set up the filter design specifications object using the fdesign.lowpass function.

Fs = 96e3;
Fpass = 20e3;
Fstop = 24e3;
Apass = 0.01;
Astop = 80;

filtSpecs = fdesign.lowpass(Fpass,Fstop,Apass,Astop,Fs);

% Determine the available design algorithms using the designmethods function.

designmethods(filtSpecs,'SystemObject',true)

%
% Using the design function, design an equiripple FIR filter and an elliptic IIR filter that meet the specifications.

lpFIR = design(filtSpecs,'equiripple','SystemObject',true)
lpIIR = design(filtSpecs,'ellip','SystemObject',true)

% You can also measure the designs to verify that the filters satisfy the constraints.

FIRmeas = measure(lpFIR)
IIRmeas = measure(lpIIR)

%
% Estimate and display the computational cost of each filter. The equiripple FIR filter requires many more coefficients than the elliptic IIR filter.

FIRcost = cost(lpFIR)
IIRcost = cost(lpIIR)

%
% Use fvtool function to visualize the resulting designs and compare their properties.

fvtool(lpFIR,lpIIR,'Fs',Fs);
legend('FIR Equiripple','Elliptic IIR')

% Copyright 2019 The MathWorks, Inc.