1 %% Copyright (C) 1986,2003 Julius O. Smith III <jos@ccrma.stanford.edu>
2 %% Copyright (C) 2003 Andrew Fitting
4 %% This program is free software; you can redistribute it and/or modify it under
5 %% the terms of the GNU General Public License as published by the Free Software
6 %% Foundation; either version 3 of the License, or (at your option) any later
9 %% This program is distributed in the hope that it will be useful, but WITHOUT
10 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 %% You should have received a copy of the GNU General Public License along with
15 %% this program; if not, see <http://www.gnu.org/licenses/>.
17 %% Usage: [B,A] = invfreqs(H,F,nB,nA)
18 %% [B,A] = invfreqs(H,F,nB,nA,W)
19 %% [B,A] = invfreqs(H,F,nB,nA,W,iter,tol,'trace')
21 %% Fit filter B(s)/A(s)to the complex frequency response H at frequency
22 %% points F. A and B are real polynomial coefficients of order nA and nB.
23 %% Optionally, the fit-errors can be weighted vs frequency according to
25 %% Note: all the guts are in invfreq.m
27 %% H: desired complex frequency response
28 %% F: frequency (must be same length as H)
29 %% nA: order of the denominator polynomial A
30 %% nB: order of the numerator polynomial B
31 %% W: vector of weights (must be same length as F)
36 %% w = linspace(0,4,128);
38 %% [Bh,Ah] = invfreqs(H,w,1,1);
39 %% Hh = freqs(Bh,Ah,w);
40 %% plot(w,[abs(H);abs(Hh)])
41 %% legend('Original','Measured');
43 %% disp(sprintf('L2 norm of frequency response error = %f',err));
45 % TODO: check invfreq.m for todo's
47 function [B, A, SigN] = invfreqs(H,F,nB,nA,W,iter,tol,tr, varargin)
58 W = ones(1,length(F));
65 % now for the real work
66 [B, A, SigN] = invfreq(H, F,nB, nA, W, iter, tol, tr, 's', varargin{:});
73 %! %#A = [1 36 630 6930 51975 270270 945945 2027025 2027025]/2027025;
74 %! A = [1 21 210 1260 4725 10395 10395]/10395;
75 %! A = [1 6 15 15]/15;
76 %! w = linspace(0, 8, 128);
77 %! H0 = freqs(B, A, w);
78 %! Nn = (randn(size(w))+j*randn(size(w)))/sqrt(2);
79 %! order = length(A) - 1;
80 %! [Bh, Ah, Sig0] = invfreqs(H0, w, [length(B)-1 2], length(A)-1);
81 %! Hh = freqs(Bh,Ah,w);
82 %! [BLS, ALS, SigLS] = invfreqs(H0+1e-5*Nn, w, [2 2], order, [], [], [], [], "method", "LS");
83 %! HLS = freqs(BLS, ALS, w);
84 %! [BTLS, ATLS, SigTLS] = invfreqs(H0+1e-5*Nn, w, [2 2], order, [], [], [], [], "method", "TLS");
85 %! HTLS = freqs(BTLS, ATLS, w);
86 %! [BMLS, AMLS, SigMLS] = invfreqs(H0+1e-5*Nn, w, [2 2], order, [], [], [], [], "method", "QR");
87 %! HMLS = freqs(BMLS, AMLS, w);
88 %! xlabel("Frequency (rad/sec)");
89 %! ylabel("Magnitude");
90 %! plot(w,[abs(H0); abs(Hh)])
91 %! legend('Original','Measured');
93 %! disp(sprintf('L2 norm of frequency response error = %f',err));