Mercurial > hg > octave-nkf
annotate scripts/signal/fftfilt.m @ 11472:1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Jan 2011 21:33:04 -0800 |
parents | c776f063fefe |
children | fd0a3ac60b0e |
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/>. | |
1026 | 19 |
3367 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} fftfilt (@var{b}, @var{x}, @var{n}) | |
3426 | 22 ## |
3367 | 23 ## With two arguments, @code{fftfilt} filters @var{x} with the FIR filter |
24 ## @var{b} using the FFT. | |
3426 | 25 ## |
3367 | 26 ## Given the optional third argument, @var{n}, @code{fftfilt} uses the |
27 ## overlap-add method to filter @var{x} with @var{b} using an N-point FFT. | |
3890 | 28 ## |
29 ## If @var{x} is a matrix, filter each column of the matrix. | |
3367 | 30 ## @end deftypefn |
2312 | 31 |
5428 | 32 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2312 | 33 ## Created: 3 September 1994 |
34 ## Adapted-By: jwe | |
787 | 35 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
36 function y = fftfilt (b, x, n) |
787 | 37 |
2325 | 38 ## If N is not specified explicitly, we do not use the overlap-add |
2303 | 39 ## method at all because loops are really slow. Otherwise, we only |
40 ## ensure that the number of points in the FFT is the smallest power | |
41 ## of two larger than N and length(b). This could result in length | |
42 ## one blocks, but if the user knows better ... | |
2325 | 43 |
787 | 44 if (nargin < 2 || nargin > 3) |
6046 | 45 print_usage (); |
787 | 46 endif |
2325 | 47 |
3890 | 48 transpose = (rows (x) == 1); |
49 | |
50 if (transpose) | |
51 x = x.'; | |
52 endif | |
53 | |
787 | 54 [r_x, c_x] = size (x); |
55 [r_b, c_b] = size (b); | |
56 | |
3890 | 57 if min ([r_b, c_b]) != 1 |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
58 error ("fftfilt: B should be a vector"); |
787 | 59 endif |
2325 | 60 |
3890 | 61 l_b = r_b * c_b; |
62 b = reshape (b, l_b, 1); | |
2325 | 63 |
787 | 64 if (nargin == 2) |
2303 | 65 ## Use FFT with the smallest power of 2 which is >= length (x) + |
66 ## length (b) - 1 as number of points ... | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
67 n = 2 ^ (ceil (log (r_x + l_b - 1) / log (2))); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
68 B = fft (b, n); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
69 y = ifft (fft (x, n) .* B(:,ones (1, c_x))); |
787 | 70 else |
2303 | 71 ## Use overlap-add method ... |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
72 if (! (isscalar (n))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
73 error ("fftfilt: N has to be a scalar"); |
787 | 74 endif |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
75 n = 2 ^ (ceil (log (max ([n, l_b])) / log (2))); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
76 L = n - l_b + 1; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
77 B = fft (b, n); |
3890 | 78 B = B(:,ones (c_x,1)); |
79 R = ceil (r_x / L); | |
80 y = zeros (r_x, c_x); | |
1026 | 81 for r = 1:R; |
3890 | 82 lo = (r - 1) * L + 1; |
83 hi = min (r * L, r_x); | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
84 tmp = zeros (n, c_x); |
3890 | 85 tmp(1:(hi-lo+1),:) = x(lo:hi,:); |
86 tmp = ifft (fft (tmp) .* B); | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
87 hi = min (lo+n-1, r_x); |
3890 | 88 y(lo:hi,:) = y(lo:hi,:) + tmp(1:(hi-lo+1),:); |
2325 | 89 endfor |
787 | 90 endif |
2325 | 91 |
3890 | 92 y = y(1:r_x,:); |
93 if (transpose) | |
94 y = y.'; | |
95 endif | |
787 | 96 |
3457 | 97 ## Final cleanups: if both x and b are real respectively integer, y |
2303 | 98 ## should also be |
1026 | 99 |
3890 | 100 if (isreal (b) && isreal (x)) |
787 | 101 y = real (y); |
102 endif | |
3890 | 103 if (! any (b - round (b))) |
104 idx = !any (x - round (x)); | |
105 y(:,idx) = round (y(:,idx)); | |
787 | 106 endif |
107 | |
108 endfunction |