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 |
2311
|
20 ## usage: fftfilt (b, x [, N]) |
|
21 ## |
|
22 ## y = fftfilt (b, x) filters x with the FIR filter b using the FFT. |
|
23 ## y = fftfilt (b, x, N) uses the overlap-add method to filter x with |
|
24 ## b using an N-point FFT. |
2312
|
25 ## |
|
26 ## Reference: Oppenheim & Schafer (1989). Discrete-time Signal |
|
27 ## Processing (Chapter 8). Prentice-Hall. |
|
28 |
|
29 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
30 ## Created: 3 September 1994 |
|
31 ## Adapted-By: jwe |
787
|
32 |
2311
|
33 function y = fftfilt (b, x, N) |
787
|
34 |
2325
|
35 ## If N is not specified explicitly, we do not use the overlap-add |
2303
|
36 ## method at all because loops are really slow. Otherwise, we only |
|
37 ## ensure that the number of points in the FFT is the smallest power |
|
38 ## of two larger than N and length(b). This could result in length |
|
39 ## one blocks, but if the user knows better ... |
2325
|
40 |
787
|
41 if (nargin < 2 || nargin > 3) |
904
|
42 usage (" fftfilt (b, x [, N])"); |
787
|
43 endif |
2325
|
44 |
787
|
45 [r_x, c_x] = size (x); |
|
46 [r_b, c_b] = size (b); |
1337
|
47 if (! (min ([r_x, c_x]) == 1 || min ([r_b, c_b]) == 1)) |
1026
|
48 error ("fftfilt: both x and b should be vectors"); |
787
|
49 endif |
|
50 l_x = r_x * c_x; |
|
51 l_b = r_b * c_b; |
|
52 |
|
53 if ((l_x == 1) && (l_b == 1)) |
2325
|
54 y = b * x; |
787
|
55 return; |
|
56 endif |
2325
|
57 |
1026
|
58 x = reshape (x, 1, l_x); |
|
59 b = reshape (b, 1, l_b); |
2325
|
60 |
787
|
61 if (nargin == 2) |
2303
|
62 ## Use FFT with the smallest power of 2 which is >= length (x) + |
|
63 ## length (b) - 1 as number of points ... |
787
|
64 N = 2^(ceil (log (l_x + l_b - 1) / log(2))); |
|
65 y = ifft (fft (x, N) .* fft(b, N)); |
|
66 else |
2303
|
67 ## Use overlap-add method ... |
787
|
68 if !(is_scalar (N)) |
1026
|
69 error ("fftfilt: N has to be a scalar"); |
787
|
70 endif |
1337
|
71 N = 2^(ceil (log (max ([N, l_b])) / log(2))); |
1026
|
72 L = N - l_b + 1; |
|
73 B = fft (b, N); |
|
74 R = ceil (l_x / L); |
|
75 y = zeros (1, l_x); |
|
76 for r = 1:R; |
787
|
77 lo = (r - 1) * L + 1; |
|
78 hi = min (r * L, l_x); |
|
79 tmp = ifft (fft (x(lo:hi), N) .* B); |
|
80 hi = min (lo+N-1, l_x); |
|
81 y(lo:hi) = y(lo:hi) + tmp(1:(hi-lo+1)); |
2325
|
82 endfor |
787
|
83 endif |
2325
|
84 |
1026
|
85 y = reshape (y(1:l_x), r_x, c_x); |
787
|
86 |
2303
|
87 ## Final cleanups: if both x and b are real respectively integer, y |
|
88 ## should also be |
1026
|
89 |
|
90 if (! (any (imag (x)) || any (imag (b)))) |
787
|
91 y = real (y); |
|
92 endif |
1026
|
93 if (! (any (x - round (x)) || any (b - round (b)))) |
787
|
94 y = round (y); |
|
95 endif |
|
96 |
|
97 endfunction |