2847
|
1 ## Copyright (C) 1996, 1997 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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
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. |
|
28 ## @end deftypefn |
2312
|
29 |
|
30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
31 ## Created: 3 September 1994 |
|
32 ## Adapted-By: jwe |
787
|
33 |
2311
|
34 function y = fftfilt (b, x, N) |
787
|
35 |
2325
|
36 ## If N is not specified explicitly, we do not use the overlap-add |
2303
|
37 ## method at all because loops are really slow. Otherwise, we only |
|
38 ## ensure that the number of points in the FFT is the smallest power |
|
39 ## of two larger than N and length(b). This could result in length |
|
40 ## one blocks, but if the user knows better ... |
2325
|
41 |
787
|
42 if (nargin < 2 || nargin > 3) |
3449
|
43 usage (" fftfilt (b, x, N)"); |
787
|
44 endif |
2325
|
45 |
787
|
46 [r_x, c_x] = size (x); |
|
47 [r_b, c_b] = size (b); |
1337
|
48 if (! (min ([r_x, c_x]) == 1 || min ([r_b, c_b]) == 1)) |
1026
|
49 error ("fftfilt: both x and b should be vectors"); |
787
|
50 endif |
|
51 l_x = r_x * c_x; |
|
52 l_b = r_b * c_b; |
|
53 |
|
54 if ((l_x == 1) && (l_b == 1)) |
2325
|
55 y = b * x; |
787
|
56 return; |
|
57 endif |
2325
|
58 |
1026
|
59 x = reshape (x, 1, l_x); |
|
60 b = reshape (b, 1, l_b); |
2325
|
61 |
787
|
62 if (nargin == 2) |
2303
|
63 ## Use FFT with the smallest power of 2 which is >= length (x) + |
|
64 ## length (b) - 1 as number of points ... |
787
|
65 N = 2^(ceil (log (l_x + l_b - 1) / log(2))); |
|
66 y = ifft (fft (x, N) .* fft(b, N)); |
|
67 else |
2303
|
68 ## Use overlap-add method ... |
787
|
69 if !(is_scalar (N)) |
1026
|
70 error ("fftfilt: N has to be a scalar"); |
787
|
71 endif |
1337
|
72 N = 2^(ceil (log (max ([N, l_b])) / log(2))); |
1026
|
73 L = N - l_b + 1; |
|
74 B = fft (b, N); |
|
75 R = ceil (l_x / L); |
|
76 y = zeros (1, l_x); |
|
77 for r = 1:R; |
787
|
78 lo = (r - 1) * L + 1; |
|
79 hi = min (r * L, l_x); |
|
80 tmp = ifft (fft (x(lo:hi), N) .* B); |
|
81 hi = min (lo+N-1, l_x); |
|
82 y(lo:hi) = y(lo:hi) + tmp(1:(hi-lo+1)); |
2325
|
83 endfor |
787
|
84 endif |
2325
|
85 |
1026
|
86 y = reshape (y(1:l_x), r_x, c_x); |
787
|
87 |
2303
|
88 ## Final cleanups: if both x and b are real respectively integer, y |
|
89 ## should also be |
1026
|
90 |
|
91 if (! (any (imag (x)) || any (imag (b)))) |
787
|
92 y = real (y); |
|
93 endif |
1026
|
94 if (! (any (x - round (x)) || any (b - round (b)))) |
787
|
95 y = round (y); |
|
96 endif |
|
97 |
|
98 endfunction |