]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/invfreqs.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / invfreqs.m
1 %% Copyright (C) 1986,2003 Julius O. Smith III <jos@ccrma.stanford.edu>
2 %% Copyright (C) 2003 Andrew Fitting
3 %%
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
7 %% version.
8 %%
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
12 %% details.
13 %%
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/>.
16
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')
20 %%
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
24 %% the weights W.
25 %% Note: all the guts are in invfreq.m
26 %%
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)
32 %%
33 %% Example:
34 %%       B = [1/2 1];
35 %%       A = [1 1];
36 %%       w = linspace(0,4,128);
37 %%       H = freqs(B,A,w);
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');
42 %%       err = norm(H-Hh);
43 %%       disp(sprintf('L2 norm of frequency response error = %f',err));
44
45 % TODO: check invfreq.m for todo's
46
47 function [B, A, SigN] = invfreqs(H,F,nB,nA,W,iter,tol,tr, varargin)
48
49   if nargin < 9
50     varargin = {};
51     if nargin < 8
52       tr = '';
53       if nargin < 7
54           tol = [];
55           if nargin < 6 
56               iter = [];
57               if nargin < 5
58                   W = ones(1,length(F));
59               end
60           end
61       end
62     end
63   end
64
65   % now for the real work
66   [B, A, SigN] = invfreq(H, F,nB, nA, W, iter, tol, tr, 's', varargin{:});
67 endfunction
68
69 %!demo
70 %! B = [1/2 1];
71 %! B = [1 0 0];
72 %! A = [1 1];
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');
92 %! err = norm(H0-Hh);
93 %! disp(sprintf('L2 norm of frequency response error = %f',err));