%% 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.

d = designfilt('lowpassfir','DesignMethod','window', ...
    'FilterOrder',10,'CutoffFrequency',0.55);
flag = islinphase(d)
[phs,w] = phasez(d);

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 = 8;
Wcut = 0.35;
atten = 20;
rippl = 1;

[zb,pb,kb] = butter(ord,Wcut);
sosb = zp2sos(zb,pb,kb);

[zc,pc,kc] = cheby1(ord,rippl,Wcut);
sosc = zp2sos(zc,pc,kc);

[zd,pd,kd] = cheby2(ord,atten,Wcut);
sosd = zp2sos(zd,pd,kd);

[ze,pe,ke] = ellip(ord,rippl,atten,Wcut);
sose = zp2sos(ze,pe,ke);

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

fv = fvtool(sosb,sosc,sosd,sose,'Analysis','phase');
legend(fv,'Butterworth','Chebyshev I','Chebyshev II','Elliptic')

phs = [islinphase(sosb) islinphase(sosc) ...
       islinphase(sosd) islinphase(sose)]

% Copyright 2015 The MathWorks, Inc.