Mercurial > hg > octave-lyh
annotate scripts/signal/freqz.m @ 8517:81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
author | sh@sh-laptop |
---|---|
date | Wed, 14 Jan 2009 20:44:25 -0500 |
parents | bc982528de11 |
children | a89198168175 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2005, 2006, |
2 ## 2007 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)); | |
3893 | 130 elseif (strcmp (region, "whole")) |
5583 | 131 f = Fs * (0:n-1)' / n; |
132 ## polyval(fliplr(P),exp(jw)) is O(p n) and fft(x) is O(n log(n)), | |
6653 | 133 ## where p is the order of the polynomial P. For small p it |
5377 | 134 ## would be faster to use polyval but in practice the overhead for |
135 ## polyval is much higher and the little bit of time saved isn't | |
136 ## worth the extra code. | |
137 hb = fft (postpad (b, n)); | |
138 ha = fft (postpad (a, n)); | |
566 | 139 else |
5583 | 140 f = Fs/2 * (0:n-1)' / n; |
5377 | 141 hb = fft (postpad (b, 2*n))(1:n); |
142 ha = fft (postpad (a, 2*n))(1:n); | |
566 | 143 endif |
144 | |
3893 | 145 h = hb ./ ha; |
146 | |
8506 | 147 if (nargout != 0) |
148 ## Return values and don't plot. | |
3907 | 149 h_r = h; |
5377 | 150 f_r = f; |
8506 | 151 else |
152 ## Plot and don't return values. | |
5377 | 153 freqz_plot (f, h); |
7151 | 154 endif |
3907 | 155 |
566 | 156 endfunction |
5377 | 157 |
158 %!test # correct values and fft-polyval consistency | |
159 %! # butterworth filter, order 2, cutoff pi/2 radians | |
160 %! b = [0.292893218813452 0.585786437626905 0.292893218813452]; | |
161 %! a = [1 0 0.171572875253810]; | |
162 %! [h,w] = freqz(b,a,32); | |
163 %! assert(h(1),1,10*eps); | |
164 %! assert(abs(h(17)).^2,0.5,10*eps); | |
165 %! assert(h,freqz(b,a,w),10*eps); # fft should be consistent with polyval | |
166 | |
167 %!test # whole-half consistency | |
168 %! b = [1 1 1]/3; # 3-sample average | |
169 %! [h,w] = freqz(b,1,32,'whole'); | |
170 %! assert(h(2:16),conj(h(32:-1:18)),20*eps); | |
171 %! [h2,w2] = freqz(b,1,16,'half'); | |
172 %! assert(h(1:16),h2,20*eps); | |
173 %! assert(w(1:16),w2,20*eps); | |
174 | |
175 %!test # Sampling frequency properly interpreted | |
5986 | 176 %! b = [1 1 1]/3; a = [1 0.2]; |
177 %! [h,f] = freqz(b,a,16,320); | |
5583 | 178 %! assert(f,[0:15]'*10,10*eps); |
5986 | 179 %! [h2,f2] = freqz(b,a,[0:15]*10,320); |
5377 | 180 %! assert(f2,[0:15]*10,10*eps); |
5986 | 181 %! assert(h,h2.',20*eps); |
182 %! [h3,f3] = freqz(b,a,32,'whole',320); | |
5583 | 183 %! assert(f3,[0:31]'*10,10*eps); |