Mercurial > hg > octave-lyh
annotate scripts/signal/freqz.m @ 9207:25f50d2d76b3
improve TR updating strategy for fminunc and fsolve
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 17 May 2009 17:54:51 +0200 |
parents | eb63fbe60fab |
children | f0c3d3fc4903 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2005, 2006, |
8920 | 2 ## 2007, 2009 John W. Eaton |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2303 | 19 |
3367 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {[@var{h}, @var{w}] =} freqz (@var{b}, @var{a}, @var{n}, "whole") | |
22 ## Return the complex frequency response @var{h} of the rational IIR filter | |
23 ## whose numerator and denominator coefficients are @var{b} and @var{a}, | |
24 ## respectively. The response is evaluated at @var{n} angular frequencies | |
25 ## between 0 and | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
26 ## @ifnottex |
3367 | 27 ## 2*pi. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
28 ## @end ifnottex |
3367 | 29 ## @iftex |
30 ## @tex | |
31 ## $2\pi$. | |
32 ## @end tex | |
33 ## @end iftex | |
3426 | 34 ## |
3367 | 35 ## @noindent |
36 ## The output value @var{w} is a vector of the frequencies. | |
3426 | 37 ## |
3367 | 38 ## If the fourth argument is omitted, the response is evaluated at |
39 ## frequencies between 0 and | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
40 ## @ifnottex |
3367 | 41 ## pi. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
42 ## @end ifnottex |
3367 | 43 ## @iftex |
44 ## @tex | |
45 ## $\pi$. | |
46 ## @end tex | |
47 ## @end iftex | |
3426 | 48 ## |
3367 | 49 ## If @var{n} is omitted, a value of 512 is assumed. |
3426 | 50 ## |
3367 | 51 ## If @var{a} is omitted, the denominator is assumed to be 1 (this |
52 ## corresponds to a simple FIR filter). | |
3426 | 53 ## |
3367 | 54 ## For fastest computation, @var{n} should factor into a small number of |
55 ## small primes. | |
3893 | 56 ## |
57 ## @deftypefnx {Function File} {@var{h} =} freqz (@var{b}, @var{a}, @var{w}) | |
58 ## Evaluate the response at the specific frequencies in the vector @var{w}. | |
59 ## The values for @var{w} are measured in radians. | |
60 ## | |
61 ## @deftypefnx {Function File} {[@dots{}] =} freqz (@dots{}, @var{Fs}) | |
62 ## Return frequencies in Hz instead of radians assuming a sampling rate | |
63 ## @var{Fs}. If you are evaluating the response at specific frequencies | |
64 ## @var{w}, those frequencies should be requested in Hz rather than radians. | |
65 ## | |
3920 | 66 ## @deftypefnx {Function File} {} freqz (@dots{}) |
3907 | 67 ## Plot the pass band, stop band and phase response of @var{h} rather |
68 ## than returning them. | |
3367 | 69 ## @end deftypefn |
904 | 70 |
3367 | 71 ## Author: jwe ??? |
2314 | 72 |
5377 | 73 function [h_r, f_r] = freqz (b, a, n, region, Fs) |
566 | 74 |
3893 | 75 if (nargin < 1 || nargin > 5) |
6046 | 76 print_usage (); |
3893 | 77 elseif (nargin == 1) |
2303 | 78 ## Response of an FIR filter. |
3893 | 79 a = n = region = Fs = []; |
566 | 80 elseif (nargin == 2) |
2303 | 81 ## Response of an IIR filter |
3893 | 82 n = region = Fs = []; |
566 | 83 elseif (nargin == 3) |
3893 | 84 region = Fs = []; |
566 | 85 elseif (nargin == 4) |
3893 | 86 Fs = []; |
5443 | 87 if (! ischar (region) && ! isempty (region)) |
3893 | 88 Fs = region; |
89 region = []; | |
90 endif | |
91 endif | |
92 | |
5377 | 93 if (isempty (b)) |
94 b = 1; | |
95 endif | |
3893 | 96 if (isempty (a)) |
97 a = 1; | |
98 endif | |
99 if (isempty (n)) | |
100 n = 512; | |
101 endif | |
102 if (isempty (region)) | |
103 if (isreal (b) && isreal (a)) | |
104 region = "half"; | |
105 else | |
106 region = "whole"; | |
107 endif | |
108 endif | |
109 if (isempty (Fs)) | |
110 if (nargout == 0) | |
111 Fs = 2; | |
112 else | |
113 Fs = 2*pi; | |
114 endif | |
566 | 115 endif |
116 | |
5583 | 117 a = a(:); |
118 b = b(:); | |
566 | 119 |
8506 | 120 if (! isscalar (n)) |
121 ## Explicit frequency vector given | |
5377 | 122 w = f = n; |
8506 | 123 if (nargin == 4) |
124 ## Sampling rate Fs was specified | |
5377 | 125 w = 2*pi*f/Fs; |
566 | 126 endif |
5986 | 127 k = max (length (b), length (a)); |
128 hb = polyval (postpad (b, k), exp (j*w)); | |
129 ha = polyval (postpad (a, k), exp (j*w)); | |
8667
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
130 else |
5583 | 131 ## polyval(fliplr(P),exp(jw)) is O(p n) and fft(x) is O(n log(n)), |
6653 | 132 ## where p is the order of the polynomial P. For small p it |
5377 | 133 ## would be faster to use polyval but in practice the overhead for |
134 ## polyval is much higher and the little bit of time saved isn't | |
135 ## worth the extra code. | |
8667
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
136 k = max (length (b), length (a)); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
137 if (k > n/2 && nargout == 0) |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
138 ## Ensure a causal phase response. |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
139 n = n * 2 .^ ceil (log2 (2*k/n)); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
140 endif |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
141 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
142 if (strcmp (region, "whole")) |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
143 N = n; |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
144 else |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
145 N = 2*n; |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
146 endif |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
147 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
148 f = Fs * (0:n-1).' / N; |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
149 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
150 pad_sz = N*ceil (k/N); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
151 b = postpad (b, pad_sz); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
152 a = postpad (a, pad_sz); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
153 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
154 hb = zeros (n, 1); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
155 ha = zeros (n, 1); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
156 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
157 for i = 1:N:pad_sz |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
158 hb = hb + fft (postpad (b(i:i+N-1), N))(1:n); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
159 ha = ha + fft (postpad (a(i:i+N-1), N))(1:n); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
160 endfor |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
161 |
566 | 162 endif |
163 | |
3893 | 164 h = hb ./ ha; |
165 | |
8506 | 166 if (nargout != 0) |
167 ## Return values and don't plot. | |
3907 | 168 h_r = h; |
5377 | 169 f_r = f; |
8506 | 170 else |
171 ## Plot and don't return values. | |
5377 | 172 freqz_plot (f, h); |
7151 | 173 endif |
3907 | 174 |
566 | 175 endfunction |
5377 | 176 |
177 %!test # correct values and fft-polyval consistency | |
178 %! # butterworth filter, order 2, cutoff pi/2 radians | |
179 %! b = [0.292893218813452 0.585786437626905 0.292893218813452]; | |
180 %! a = [1 0 0.171572875253810]; | |
181 %! [h,w] = freqz(b,a,32); | |
182 %! assert(h(1),1,10*eps); | |
183 %! assert(abs(h(17)).^2,0.5,10*eps); | |
184 %! assert(h,freqz(b,a,w),10*eps); # fft should be consistent with polyval | |
185 | |
186 %!test # whole-half consistency | |
187 %! b = [1 1 1]/3; # 3-sample average | |
188 %! [h,w] = freqz(b,1,32,'whole'); | |
189 %! assert(h(2:16),conj(h(32:-1:18)),20*eps); | |
190 %! [h2,w2] = freqz(b,1,16,'half'); | |
191 %! assert(h(1:16),h2,20*eps); | |
192 %! assert(w(1:16),w2,20*eps); | |
193 | |
194 %!test # Sampling frequency properly interpreted | |
5986 | 195 %! b = [1 1 1]/3; a = [1 0.2]; |
196 %! [h,f] = freqz(b,a,16,320); | |
5583 | 197 %! assert(f,[0:15]'*10,10*eps); |
5986 | 198 %! [h2,f2] = freqz(b,a,[0:15]*10,320); |
5377 | 199 %! assert(f2,[0:15]*10,10*eps); |
5986 | 200 %! assert(h,h2.',20*eps); |
201 %! [h3,f3] = freqz(b,a,32,'whole',320); | |
5583 | 202 %! assert(f3,[0:31]'*10,10*eps); |