%% Linear and Nonlinear Phase
% Use the window method to design a tenth-order lowpass FIR filter with normalized cutoff frequency 0.55. Verify that the filter has linear phase.

firSpecs = fdesign.lowpass('N,Fc',10,0.55);
lpFIR = design(firSpecs,'window','SystemObject',true);

flag = islinphase(lpFIR)
[phs,w] = phasez(lpFIR);

plot(w/pi,phs)
xlabel('Frequency \omega/\pi')
ylabel('Phase')

%
% IIR filters in general do not have linear phase. Verify the statement by constructing eighth-order Butterworth, Chebyshev, and elliptic filters with similar specifications.

ord = 4;

Wp = 0.35;
Wst = 0.4;
atten = 20;
rippl = 1;

buttSpecs = fdesign.lowpass('Fp,Fst,Ap,Ast',Wp,Wst,rippl,atten);
buttIIR = design(buttSpecs,'butter','SystemObject',true);

chb1Specs = fdesign.lowpass('Fp,Fst,Ap,Ast',Wp,Wst,rippl,atten);
chb1IIR = design(chb1Specs,'cheby1','SystemObject',true);

chb2Specs = fdesign.lowpass('Fp,Fst,Ap,Ast',Wp,Wst,rippl,atten);
chb2IIR = design(chb2Specs,'cheby2','SystemObject',true);

ellpSpecs = fdesign.lowpass('Fp,Fst,Ap,Ast',Wp,Wst,rippl,atten);
ellpIIR = design(ellpSpecs,'ellip','SystemObject',true);

%
% Plot the phase responses of the filters. Determine whether they have linear phase.

fv = fvtool(buttIIR,chb1IIR,chb2IIR,ellpIIR,'Analysis','phase');
legend(fv,'Butterworth','Chebyshev I','Chebyshev II','Elliptic')

phs = [islinphase(buttIIR) islinphase(chb1IIR) ...
       islinphase(chb2IIR) islinphase(ellpIIR)]

% Copyright 2012 The MathWorks, Inc.