先放子函数:
function [C, B, A, rM] = dir2fs_r(h, r); % DIRECT-form to Frequency Sampling form conversion % ---------------------------------------------------------- % [C, B, A, rM] = dir2fs_r(h, r) % C = Row vector containing gains for parallel sections % B = matrix containing numerator coefficients arranged in rows % A = matrix containing denominator coefficients arranged in rows % h = impulse response vector of an FIR filter % rM = r^M factor needed in the feedforward loop % r = radius of the circle over which samples are taken (r<1) M = length(h); H = fft(h, M); magH = abs(H); phaH = angle(H)'; % check even or odd M if (M == 2*floor(M/2)) L = M/2-1; % M is even %A1 = [1, -1, 0; 1, 1, 0]; A1 = [1, -r, 0; 1, r, 0]; C1 = [real(H(1)), real(H(L+2))]; else L = (M-1)/2; % M is odd %A1 = [1, -1, 0]; A1 = [1, -r, 0]; C1 = [real(H(1))]; end k = [1:L]'; % initialize B and A arrays B = zeros(L, 2); A = ones(L, 3); % compute denominator coefficients A(1:L, 2) = -2*r*cos(2*pi*k/M); A(1:L, 3) = r*r; A = [A;A1]; % compute numerator coefficients B(1:L,1) = cos(phaH(2:L+1)); B(1:L,2) = -r*cos(phaH(2:L+1) - (2*pi*k/M)); % compute gain coefficients C = [2*magH(2:L+1), C1]'; rM = r^M;
再放主函数:
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output Info about this m-file fprintf(' *********************************************************** '); fprintf(' <DSP using MATLAB> Problem 6.20 '); banner(); %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %format long; format short; fprintf(' FIR filter DIRECT-form: '); h = [1, 2, 3, 2, 1]/9; b = h a = 1.0 fprintf(' Convert DIRECT-form to PARALLEL-form : '); [C, Bp, Ap] = dir2par(b, a) if size(C)==0 C = 0; end fprintf(' Convert DIRECT-form to CASCADE-form : '); [b0, Bc, Ac] = dir2cas(b, a) fprintf(' Convert TF-form to SOS-form : '); [sos, g] = tf2sos(b, a) % ---------------------------------------------------------- % NOTE: linear-phase can not use LATTICE-form % ---------------------------------------------------------- fprintf(' Convert DIRECT-form to All-Zero LATTICE-form : '); [Klc] = dir2latc(b) fprintf(' Convert DIRECT-form to FREQUENCY-SAMPLE-form 1 : '); [Cfs, Bfs, Afs] = dir2fs(b) fprintf(' Convert DIRECT-form to FREQUENCY-SAMPLE-form 2 : '); r = 0.9; [Cfs_r, Bfs_r, Afs_r, rM] = dir2fs_r(b, r) % ----------------------------------------- % START check % ----------------------------------------- n = [0:7]; delta = impseq(0, 0, 7) %format long format short hcas = casfiltr(b0, Bc, Ac, delta) hltc = latcfilt(Klc, delta) %hladr = ladrfilt(Klr, Clr, delta) hdir = filter(b, a, delta) % ------------------------------------------- % END check % -------------------------------------------
运行结果: