Mercurial > hg > octave-lyh
annotate scripts/signal/freqz.m @ 10297:ed88ea036716
improve docs of fzero/fminbnd
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 10 Feb 2010 16:15:30 +0100 |
parents | f0c3d3fc4903 |
children | fd0a3ac60b0e |
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 ## @tex |
30 ## $2\pi$. | |
31 ## @end tex | |
3426 | 32 ## |
3367 | 33 ## @noindent |
34 ## The output value @var{w} is a vector of the frequencies. | |
3426 | 35 ## |
3367 | 36 ## If the fourth argument is omitted, the response is evaluated at |
37 ## frequencies between 0 and | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
38 ## @ifnottex |
3367 | 39 ## pi. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
40 ## @end ifnottex |
3367 | 41 ## @tex |
42 ## $\pi$. | |
43 ## @end tex | |
3426 | 44 ## |
3367 | 45 ## If @var{n} is omitted, a value of 512 is assumed. |
3426 | 46 ## |
3367 | 47 ## If @var{a} is omitted, the denominator is assumed to be 1 (this |
48 ## corresponds to a simple FIR filter). | |
3426 | 49 ## |
3367 | 50 ## For fastest computation, @var{n} should factor into a small number of |
51 ## small primes. | |
3893 | 52 ## |
53 ## @deftypefnx {Function File} {@var{h} =} freqz (@var{b}, @var{a}, @var{w}) | |
54 ## Evaluate the response at the specific frequencies in the vector @var{w}. | |
55 ## The values for @var{w} are measured in radians. | |
56 ## | |
57 ## @deftypefnx {Function File} {[@dots{}] =} freqz (@dots{}, @var{Fs}) | |
58 ## Return frequencies in Hz instead of radians assuming a sampling rate | |
59 ## @var{Fs}. If you are evaluating the response at specific frequencies | |
60 ## @var{w}, those frequencies should be requested in Hz rather than radians. | |
61 ## | |
3920 | 62 ## @deftypefnx {Function File} {} freqz (@dots{}) |
3907 | 63 ## Plot the pass band, stop band and phase response of @var{h} rather |
64 ## than returning them. | |
3367 | 65 ## @end deftypefn |
904 | 66 |
3367 | 67 ## Author: jwe ??? |
2314 | 68 |
5377 | 69 function [h_r, f_r] = freqz (b, a, n, region, Fs) |
566 | 70 |
3893 | 71 if (nargin < 1 || nargin > 5) |
6046 | 72 print_usage (); |
3893 | 73 elseif (nargin == 1) |
2303 | 74 ## Response of an FIR filter. |
3893 | 75 a = n = region = Fs = []; |
566 | 76 elseif (nargin == 2) |
2303 | 77 ## Response of an IIR filter |
3893 | 78 n = region = Fs = []; |
566 | 79 elseif (nargin == 3) |
3893 | 80 region = Fs = []; |
566 | 81 elseif (nargin == 4) |
3893 | 82 Fs = []; |
5443 | 83 if (! ischar (region) && ! isempty (region)) |
3893 | 84 Fs = region; |
85 region = []; | |
86 endif | |
87 endif | |
88 | |
5377 | 89 if (isempty (b)) |
90 b = 1; | |
91 endif | |
3893 | 92 if (isempty (a)) |
93 a = 1; | |
94 endif | |
95 if (isempty (n)) | |
96 n = 512; | |
97 endif | |
98 if (isempty (region)) | |
99 if (isreal (b) && isreal (a)) | |
100 region = "half"; | |
101 else | |
102 region = "whole"; | |
103 endif | |
104 endif | |
105 if (isempty (Fs)) | |
106 if (nargout == 0) | |
107 Fs = 2; | |
108 else | |
109 Fs = 2*pi; | |
110 endif | |
566 | 111 endif |
112 | |
5583 | 113 a = a(:); |
114 b = b(:); | |
566 | 115 |
8506 | 116 if (! isscalar (n)) |
117 ## Explicit frequency vector given | |
5377 | 118 w = f = n; |
8506 | 119 if (nargin == 4) |
120 ## Sampling rate Fs was specified | |
5377 | 121 w = 2*pi*f/Fs; |
566 | 122 endif |
5986 | 123 k = max (length (b), length (a)); |
124 hb = polyval (postpad (b, k), exp (j*w)); | |
125 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
|
126 else |
5583 | 127 ## polyval(fliplr(P),exp(jw)) is O(p n) and fft(x) is O(n log(n)), |
6653 | 128 ## where p is the order of the polynomial P. For small p it |
5377 | 129 ## would be faster to use polyval but in practice the overhead for |
130 ## polyval is much higher and the little bit of time saved isn't | |
131 ## worth the extra code. | |
8667
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
132 k = max (length (b), length (a)); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
133 if (k > n/2 && nargout == 0) |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
134 ## Ensure a causal phase response. |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
135 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
|
136 endif |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
137 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
138 if (strcmp (region, "whole")) |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
139 N = n; |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
140 else |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
141 N = 2*n; |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
142 endif |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
143 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
144 f = Fs * (0:n-1).' / N; |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
145 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
146 pad_sz = N*ceil (k/N); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
147 b = postpad (b, pad_sz); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
148 a = postpad (a, pad_sz); |
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 hb = zeros (n, 1); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
151 ha = zeros (n, 1); |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
152 |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
153 for i = 1:N:pad_sz |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
154 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
|
155 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
|
156 endfor |
a89198168175
freqz.m: freqz.m: fix for long input
Ben Abbott <bpabbott@mac.com>
parents:
8517
diff
changeset
|
157 |
566 | 158 endif |
159 | |
3893 | 160 h = hb ./ ha; |
161 | |
8506 | 162 if (nargout != 0) |
163 ## Return values and don't plot. | |
3907 | 164 h_r = h; |
5377 | 165 f_r = f; |
8506 | 166 else |
167 ## Plot and don't return values. | |
5377 | 168 freqz_plot (f, h); |
7151 | 169 endif |
3907 | 170 |
566 | 171 endfunction |
5377 | 172 |
173 %!test # correct values and fft-polyval consistency | |
174 %! # butterworth filter, order 2, cutoff pi/2 radians | |
175 %! b = [0.292893218813452 0.585786437626905 0.292893218813452]; | |
176 %! a = [1 0 0.171572875253810]; | |
177 %! [h,w] = freqz(b,a,32); | |
178 %! assert(h(1),1,10*eps); | |
179 %! assert(abs(h(17)).^2,0.5,10*eps); | |
180 %! assert(h,freqz(b,a,w),10*eps); # fft should be consistent with polyval | |
181 | |
182 %!test # whole-half consistency | |
183 %! b = [1 1 1]/3; # 3-sample average | |
184 %! [h,w] = freqz(b,1,32,'whole'); | |
185 %! assert(h(2:16),conj(h(32:-1:18)),20*eps); | |
186 %! [h2,w2] = freqz(b,1,16,'half'); | |
187 %! assert(h(1:16),h2,20*eps); | |
188 %! assert(w(1:16),w2,20*eps); | |
189 | |
190 %!test # Sampling frequency properly interpreted | |
5986 | 191 %! b = [1 1 1]/3; a = [1 0.2]; |
192 %! [h,f] = freqz(b,a,16,320); | |
5583 | 193 %! assert(f,[0:15]'*10,10*eps); |
5986 | 194 %! [h2,f2] = freqz(b,a,[0:15]*10,320); |
5377 | 195 %! assert(f2,[0:15]*10,10*eps); |
5986 | 196 %! assert(h,h2.',20*eps); |
197 %! [h3,f3] = freqz(b,a,32,'whole',320); | |
5583 | 198 %! assert(f3,[0:31]'*10,10*eps); |