%% Impulse and Frequency Response of FIR and IIR Lowpass Filters
% Create a minimum-order FIR lowpass filter for data sampled at 44.1 kHz. Specify a passband frequency of 8 kHz, a stopband frequency of 12 kHz, a passband ripple of 0.1 dB, and a stopband attenuation of 80 dB.

Fs = 44.1e3; 
filtertype = 'FIR';
Fpass = 8e3;
Fstop = 12e3; 
Rp = 0.1;
Astop = 80;
FIRLPF = dsp.LowpassFilter('SampleRate',Fs, ...
                             'FilterType',filtertype, ...
                             'PassbandFrequency',Fpass, ...
                             'StopbandFrequency',Fstop, ...
                             'PassbandRipple',Rp, ...
                             'StopbandAttenuation',Astop);

%
% Design a minimum-order IIR lowpass filter with the same properties as the FIR lowpass filter. Change the FilterType property of the cloned filter to IIR.

IIRLPF = clone(FIRLPF);
IIRLPF.FilterType = 'IIR';

%
% Plot the impulse response of the FIR lowpass filter. The zeroth-order coefficient is delayed by 19 samples, which is equal to the group delay of the filter. The FIR lowpass filter is a causal FIR filter.

fvtool(FIRLPF,'Analysis','impulse')

%
% Plot the impulse response of the IIR lowpass filter.

fvtool(IIRLPF,'Analysis','impulse')

%
% Plot the magnitude and phase response of the FIR lowpass filter.

fvtool(FIRLPF,'Analysis','freq')

%
% Plot the magnitude and phase response of the IIR lowpass filter.

fvtool(IIRLPF,'Analysis','freq')

%
% Calculate the cost of implementing the FIR lowpass filter.

cost(FIRLPF)

%
% Calculate the cost of implementing the IIR lowpass filter. The IIR filter is more efficient to implement than the FIR filter.

cost(IIRLPF)

%
% Calculate the group delay of the FIR lowpass filter.

grpdelay(FIRLPF)

%
% Calculate the group delay of the IIR lowpass filter. The FIR filter has a constant group delay (linear phase), while its IIR counterpart does not.

grpdelay(IIRLPF)

%
% Copyright 2015 The MathWorks, Inc.