www.gusucode.com > signal 案例源码程序 matlab代码 > signal/FIRApproximationToFrequencyResponseExample.m

    %% FIR Approximation to Allpass Response
% Design a nonlinear-phase allpass FIR filter of order 22 with frequency
% response given approximately by $\exp(-j\pi fN/2+j4\pi f|f|)$, where
% $f\in[-1,1]$.

% Copyright 2015 The MathWorks, Inc.


%%

n = 22;                              % Filter order
f = [-1 1];                          % Frequency band edges
w = [1 1];                           % Weights for optimization
gf = linspace(-1,1,256);             % Grid of frequency points 
d = exp(-1i*pi*gf*n/2 + 1i*pi*pi*sign(gf).*gf.*gf*(4/pi));
                                     % Desired frequency response

%%
% Use |cfirpm| to compute the FIR filter. Plot the actual and approximate
% magnitude responses in dB and the phase responses in degrees.

b = cfirpm(n,f,'allpass',w,'real');  % Approximation
freqz(b,1,256,'whole')

subplot(2,1,1)                       % Overlay response
hold on
plot(pi*(gf+1),20*log10(abs(fftshift(d))),'r--')

subplot(2,1,2)
hold on
plot(pi*(gf+1),unwrap(angle(fftshift(d)))*180/pi,'r--')
legend('Approximation','Desired','Location','SouthWest')