Mercurial > hg > octave-nkf
annotate scripts/signal/fftfilt.m @ 11542:695141f1c05c ss-3-3-55
snapshot 3.3.55
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 15 Jan 2011 04:53:04 -0500 |
parents | fd0a3ac60b0e |
children | 39ca02387a32 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1994-2011 John W. Eaton |
2313 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1026 | 18 |
3367 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} fftfilt (@var{b}, @var{x}, @var{n}) | |
3426 | 21 ## |
3367 | 22 ## With two arguments, @code{fftfilt} filters @var{x} with the FIR filter |
23 ## @var{b} using the FFT. | |
3426 | 24 ## |
3367 | 25 ## Given the optional third argument, @var{n}, @code{fftfilt} uses the |
26 ## overlap-add method to filter @var{x} with @var{b} using an N-point FFT. | |
3890 | 27 ## |
28 ## If @var{x} is a matrix, filter each column of the matrix. | |
3367 | 29 ## @end deftypefn |
2312 | 30 |
5428 | 31 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2312 | 32 ## Created: 3 September 1994 |
33 ## Adapted-By: jwe | |
787 | 34 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
35 function y = fftfilt (b, x, n) |
787 | 36 |
2325 | 37 ## If N is not specified explicitly, we do not use the overlap-add |
2303 | 38 ## method at all because loops are really slow. Otherwise, we only |
39 ## ensure that the number of points in the FFT is the smallest power | |
40 ## of two larger than N and length(b). This could result in length | |
41 ## one blocks, but if the user knows better ... | |
2325 | 42 |
787 | 43 if (nargin < 2 || nargin > 3) |
6046 | 44 print_usage (); |
787 | 45 endif |
2325 | 46 |
3890 | 47 transpose = (rows (x) == 1); |
48 | |
49 if (transpose) | |
50 x = x.'; | |
51 endif | |
52 | |
787 | 53 [r_x, c_x] = size (x); |
54 [r_b, c_b] = size (b); | |
55 | |
3890 | 56 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
|
57 error ("fftfilt: B should be a vector"); |
787 | 58 endif |
2325 | 59 |
3890 | 60 l_b = r_b * c_b; |
61 b = reshape (b, l_b, 1); | |
2325 | 62 |
787 | 63 if (nargin == 2) |
2303 | 64 ## Use FFT with the smallest power of 2 which is >= length (x) + |
65 ## 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
|
66 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
|
67 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
|
68 y = ifft (fft (x, n) .* B(:,ones (1, c_x))); |
787 | 69 else |
2303 | 70 ## 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
|
71 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
|
72 error ("fftfilt: N has to be a scalar"); |
787 | 73 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
|
74 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
|
75 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
|
76 B = fft (b, n); |
3890 | 77 B = B(:,ones (c_x,1)); |
78 R = ceil (r_x / L); | |
79 y = zeros (r_x, c_x); | |
1026 | 80 for r = 1:R; |
3890 | 81 lo = (r - 1) * L + 1; |
82 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
|
83 tmp = zeros (n, c_x); |
3890 | 84 tmp(1:(hi-lo+1),:) = x(lo:hi,:); |
85 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
|
86 hi = min (lo+n-1, r_x); |
3890 | 87 y(lo:hi,:) = y(lo:hi,:) + tmp(1:(hi-lo+1),:); |
2325 | 88 endfor |
787 | 89 endif |
2325 | 90 |
3890 | 91 y = y(1:r_x,:); |
92 if (transpose) | |
93 y = y.'; | |
94 endif | |
787 | 95 |
3457 | 96 ## Final cleanups: if both x and b are real respectively integer, y |
2303 | 97 ## should also be |
1026 | 98 |
3890 | 99 if (isreal (b) && isreal (x)) |
787 | 100 y = real (y); |
101 endif | |
3890 | 102 if (! any (b - round (b))) |
103 idx = !any (x - round (x)); | |
104 y(:,idx) = round (y(:,idx)); | |
787 | 105 endif |
106 | |
107 endfunction |