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 |
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 |
2311
|
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 |
|
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 ... |
3890
|
66 N = 2 ^ (ceil (log (r_x + l_b - 1) / log (2))); |
|
67 B = fft (b, N); |
|
68 y = ifft (fft (x, N) .* B(:,ones (1, c_x))); |
787
|
69 else |
2303
|
70 ## Use overlap-add method ... |
4030
|
71 if (! (isscalar (N))) |
1026
|
72 error ("fftfilt: N has to be a scalar"); |
787
|
73 endif |
3890
|
74 N = 2 ^ (ceil (log (max ([N, l_b])) / log (2))); |
1026
|
75 L = N - l_b + 1; |
|
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); |
|
83 tmp = zeros (N, c_x); |
|
84 tmp(1:(hi-lo+1),:) = x(lo:hi,:); |
|
85 tmp = ifft (fft (tmp) .* B); |
|
86 hi = min (lo+N-1, r_x); |
|
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 |