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