annotate scripts/signal/fftfilt.m @ 2311:2b5788792cad

[project @ 1996-07-11 20:18:38 by jwe]
author jwe
date Thu, 11 Jul 1996 20:18:38 +0000
parents 5cffc4b8de57
children 204cc7db6f4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
1 ### Copyright (C) 1996 John W. Eaton
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
2 ###
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
3 ### This file is part of Octave.
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
4 ###
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
5 ### Octave is free software; you can redistribute it and/or modify it
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
6 ### under the terms of the GNU General Public License as published by
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
7 ### the Free Software Foundation; either version 2, or (at your option)
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
8 ### any later version.
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
9 ###
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
10 ### Octave is distributed in the hope that it will be useful, but
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
11 ### WITHOUT ANY WARRANTY; without even the implied warranty of
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
12 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
13 ### General Public License for more details.
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
14 ###
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
15 ### You should have received a copy of the GNU General Public License
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
16 ### along with Octave; see the file COPYING. If not, write to the Free
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
17 ### Software Foundation, 59 Temple Place - Suite 330, Boston, MA
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
18 ### 02111-1307, USA.
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
19
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
20 ## usage: fftfilt (b, x [, N])
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
21 ##
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
22 ## y = fftfilt (b, x) filters x with the FIR filter b using the FFT.
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
23 ## y = fftfilt (b, x, N) uses the overlap-add method to filter x with
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
24 ## b using an N-point FFT.
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
25
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
26 function y = fftfilt (b, x, N)
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
27
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
28 ## Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Sep 3, 1994
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
29
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
30 ## Reference: Oppenheim & Schafer (1989). Discrete-time Signal
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
31 ## Processing (Chapter 8). Prentice-Hall.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 787
diff changeset
32
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
33 ## If N is not specified explicitly, we do not use the overlap-add
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
34 ## method at all because loops are really slow. Otherwise, we only
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
35 ## ensure that the number of points in the FFT is the smallest power
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
36 ## of two larger than N and length(b). This could result in length
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
37 ## one blocks, but if the user knows better ...
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
38
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
39 if (nargin < 2 || nargin > 3)
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 787
diff changeset
40 usage (" fftfilt (b, x [, N])");
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
41 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
42
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
43 [r_x, c_x] = size (x);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
44 [r_b, c_b] = size (b);
1337
52a3f38cbfeb [project @ 1995-08-25 02:00:18 by jwe]
jwe
parents: 1315
diff changeset
45 if (! (min ([r_x, c_x]) == 1 || min ([r_b, c_b]) == 1))
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
46 error ("fftfilt: both x and b should be vectors");
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
47 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
48 l_x = r_x * c_x;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
49 l_b = r_b * c_b;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
50
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
51 if ((l_x == 1) && (l_b == 1))
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
52 y = b * x;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
53 return;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
54 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
55
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
56 x = reshape (x, 1, l_x);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
57 b = reshape (b, 1, l_b);
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
58
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
59 if (nargin == 2)
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
60 ## Use FFT with the smallest power of 2 which is >= length (x) +
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
61 ## length (b) - 1 as number of points ...
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
62 N = 2^(ceil (log (l_x + l_b - 1) / log(2)));
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
63 y = ifft (fft (x, N) .* fft(b, N));
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
64 else
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
65 ## Use overlap-add method ...
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
66 if !(is_scalar (N))
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
67 error ("fftfilt: N has to be a scalar");
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
68 endif
1337
52a3f38cbfeb [project @ 1995-08-25 02:00:18 by jwe]
jwe
parents: 1315
diff changeset
69 N = 2^(ceil (log (max ([N, l_b])) / log(2)));
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
70 L = N - l_b + 1;
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
71 B = fft (b, N);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
72 R = ceil (l_x / L);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
73 y = zeros (1, l_x);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
74 for r = 1:R;
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
75 lo = (r - 1) * L + 1;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
76 hi = min (r * L, l_x);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
77 tmp = ifft (fft (x(lo:hi), N) .* B);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
78 hi = min (lo+N-1, l_x);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
79 y(lo:hi) = y(lo:hi) + tmp(1:(hi-lo+1));
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
80 endfor
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
81 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
82
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
83 y = reshape (y(1:l_x), r_x, c_x);
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
84
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
85 ## Final cleanups: if both x and b are real respectively integer, y
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
86 ## should also be
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
87
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
88 if (! (any (imag (x)) || any (imag (b))))
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
89 y = real (y);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
90 endif
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
91 if (! (any (x - round (x)) || any (b - round (b))))
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
92 y = round (y);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
93 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
94
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
95 endfunction
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
96
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
97
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
98
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
99
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
100
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
101