%% Create an Interpolator Using fdesign Object
% These examples show how to construct interpolating filter specification objects.
% First, create a default specifications object without using input arguments except for the interpolation factor l.

l = 2;
d = fdesign.interpolator(l); %#ok

%
% Now create an object by passing a specification option 'fst1,fp1,fp2,fst2,ast1,ap,ast2' and a design - the resulting object uses default values for all of the filter specifications. You must provide the design input argument when you include a specification.

d = fdesign.interpolator(8,'bandpass','fst1,fp1,fp2,fst2,ast1,ap,ast2'); %#ok

%
% Create another interpolating filter object, passing the specification values to the object rather than accepting the default values for, in this case, fp,fst,ap,ast.

d = fdesign.interpolator(3,'lowpass',.45,0.55,.1,60); %#ok

%
% Now pass the filter specifications that correspond to the specifications - n,fc,ap,ast.

d = fdesign.interpolator(3,'ciccomp',1,2,'n,fc,ap,ast',...
20,0.45,.05,50);

%
% With the specifications object in your workspace, design an interpolator using the equiripple design method.

hm = design(d,'equiripple','SystemObject',true); %#ok

%
% Pass a new specification type for the filter, specifying the filter order.

d = fdesign.interpolator(5,'CIC',1,'fp,ast',0.05,55);

%
% With the specifications object in your workspace, design an interpolator using the multisection design method.

hm = design(d,'multisection','SystemObject',true); %#ok

%
% In this example, you specify a sampling frequency as the right most input argument. Here, it is set to 1000 Hz.

d = fdesign.interpolator(8,'bandpass','fst1,fp1,fp2,fst2,ast1,ap,ast2',...
    0.25,0.35,.55,.65,50,.05,1e3); %#ok

%
% In this, the last example, use the linear option for the filter specification object and specify the stopband ripple attenuation in linear form.

d = fdesign.interpolator(4,'lowpass','n,fst,ap,ast',15,0.55,.05,0.001,...
    'linear'); %#ok

%
% Now design a CIC interpolator for a signal sampled at 19200 Hz. Specify the differential delay of 2 and set the attenuation of information beyond 50 Hz to be at least 80 dB.

% The filter object sampling frequency is (l x fs) where fs is the sampling frequency of the input signal.

dd  = 2;     % Differential delay.
fp  = 50;    % Passband of interest.
ast = 80;    % Minimum attenuation of alias components in passband.
fs  = 600;   % Sampling frequency for input signal.
l   = 32;    % Interpolation factor.
d   = fdesign.interpolator(l,'cic',dd,'fp,ast',fp,ast,l*fs);
hm = design(d,'SystemObject',true);  %Use the default design method.

%
% This next example results in a minimum-order CIC compensator that interpolates by 4 and compensates for the droop in the passband for the CIC filter hm from the previous example.

nsecs = hm.NumSections;
d = fdesign.interpolator(4,'ciccomp',dd,nsecs,...
50,100,0.1,80,fs);
hmc = design(d,'equiripple','SystemObject',true);

%
% hmc is designed to compensate for hm. To see the effect of the compensating CIC filter, use fvtool to analyze both filters individually and include the compound filter response by cascading hm and hmc.

hfvt = fvtool(hmc,hm,cascade(hmc,hm),'fs',[fs,l*fs,l*fs],'ShowReference','off');
legend(hfvt,'CIC Compensator','CIC Interpolator',...
'Overall Response');

% fvtool returns with this plot.
%
% For the third example, use fdesign.interpolator to design a minimum-order Nyquist interpolator that uses a Kaiser window. For comparison, design a multistage interpolator as well and compare the responses.

l   = 15;   % Set the interpolation factor and the Nyquist band.
tw  = 0.05; % Specify the normalized transition width.
ast = 40;   % Set the minimum stopband attenuation in dB.
d   = fdesign.interpolator(l,'nyquist',l,tw,ast);
hm = design(d,'kaiserwin','SystemObject',true);
hm2 = design(d,'multistage','SystemObject',true); % Design the multistage interpolator.
hfvt = fvtool(hm,hm2);
legend(hfvt,'Kaiser Window','Multistage')

% fvtool shows both responses.
%
% Design a lowpass interpolator for an interpolation factor of 8. Compare the single-stage equiripple design to a multistage design with the same interpolation factor.

l = 8; % Interpolation factor.
d = fdesign.interpolator(l,'lowpass');
hm1 = design(d,'equiripple','SystemObject',true);
% Use halfband filters whenever possible.
hm2 = design(d,'multistage','usehalfbands',true,'SystemObject',true);
hfvt = fvtool(hm1,hm2);
legend(hfvt,'Single-Stage Equiripple','Multistage')

% Copyright 2012 The MathWorks, Inc.