%% Design Efficient Decimator
% Design a single-stage decimator using the designMultirateFIR function and a multistage decimator using the designMultistageDecimator function. Determine the efficiency of the two designs using the cost function. The implementation efficiency is characterized by two cost metrics - NumCoefficients and MultiplicationsPerInputSample.
% Compute the cost of implementing both designs, and determine which design is more efficient. To make a comparison, design the filters such that their transition width is the same.
%% Initialization
% Choose a decimation factor of 48, input sample rate of 30\ldotp 72\times 48\;\textrm{MHz}, one-sided bandwidth of 10 MHz, and a stopband attenution of 90 dB.

M = 48;
Fin = 30.72e6*M;
Astop = 90;
BW = 1e7;

%
%% Using the designMultirateFIR Function
% Designing the decimation filter using the designMultirateFIR function yields a single-stage design. Set the half-polyphase length to a finite integer, in this case 8.

HalfPolyLength = 8;
b = designMultirateFIR(1,M,HalfPolyLength,Astop);
d = dsp.FIRDecimator(M,b)

% Compute the cost of implementing the decimator. The decimation filter requires 753 coefficients and 720 states. The number of multiplications per input sample and additions per input sample are 15.6875 and 15.6667, respectively.

cost(d)

%
%% Using the designMultistageDecimator Function
% Design a multistage decimator with the same filter specifications as the single-stage design. Compute the transition width using the following relationship:

Fc = Fin/(2*M);
TW = 2*(Fc-BW);

% By default, the number of stages given by the NumStages argument is set to 'Auto', yielding an optimal design that tries to minimize the number of multiplications per input sample.

c = designMultistageDecimator(M,Fin,TW,Astop)

%
% Calling the info function on c shows that the filter is implemented as a cascade of four dsp.FIRDecimator objects, with decimation factors of 3, 2, 2, and 4, respectively.
%
% Compute the cost of implementing the decimator.

cost(c)

% The NumCoefficients and the MultiplicationsPerInputSample parameters are lower for the four-stage filter designed by the designMultistageDecimator function, making it more efficient.
%
% Compare the magnitude response of both the designs.

fvtool(b,c)
legend('Single-stage','Multistage')

% The magnitude response shows that the transition width of both the filters is the same, making the filters comparable. The cost function shows that implementing the multistage design is more efficient compared to implementing the single-stage design.
%
%% Using the 'design' Option in the designMultistageDecimator Function
% The filter can be made even more efficient by setting the 'CostMethod' argument of the designMultistageDecimator function to 'design'. By default, this argument is set to 'estimate'.
% In the 'design' mode, the function designs each stage and computes the filter order. This yields an optimal design compared to the 'estimate' mode, where the function estimates the filter order for each stage and designs the filter based on the estimate.
% Note that the 'design' option can take much longer compared to the 'estimate' option.

cOptimal = designMultistageDecimator(M,Fin,TW,Astop,'CostMethod','design')

%

cost(cOptimal)

%
% Copyright 2018 The MathWorks, Inc.