先放子函数:
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('\n***********************************************************\n');
fprintf(' <DSP using MATLAB> Problem 6.20 \n\n'); banner();
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%format long;
format short;
fprintf('\n FIR filter DIRECT-form: \n');
h = [1, 2, 3, 2, 1]/9; b = h
a = 1.0 fprintf('\nConvert DIRECT-form to PARALLEL-form : \n');
[C, Bp, Ap] = dir2par(b, a) if size(C)==0
C = 0;
end fprintf('\nConvert DIRECT-form to CASCADE-form : \n');
[b0, Bc, Ac] = dir2cas(b, a) fprintf('\nConvert TF-form to SOS-form : \n');
[sos, g] = tf2sos(b, a) % ----------------------------------------------------------
% NOTE: linear-phase can not use LATTICE-form
% ----------------------------------------------------------
fprintf('\nConvert DIRECT-form to All-Zero LATTICE-form : \n');
[Klc] = dir2latc(b) fprintf('\nConvert DIRECT-form to FREQUENCY-SAMPLE-form 1 : \n');
[Cfs, Bfs, Afs] = dir2fs(b) fprintf('\nConvert DIRECT-form to FREQUENCY-SAMPLE-form 2 : \n');
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
% -------------------------------------------
运行结果: