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