annotate scripts/signal/fftfilt.m @ 3449:858695b3ed62

[project @ 2000-01-18 04:08:59 by jwe]
author jwe
date Tue, 18 Jan 2000 04:09:14 +0000
parents f8dde1807dee
children e031284eea27
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2847
8b262e771614 [project @ 1997-03-27 16:18:26 by jwe]
jwe
parents: 2326
diff changeset
1 ## Copyright (C) 1996, 1997 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## under the terms of the GNU General Public License as published by
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
8 ## any later version.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## You should have received a copy of the GNU General Public License
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
18 ## 02111-1307, USA.
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
19
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
20 ## -*- texinfo -*-
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
21 ## @deftypefn {Function File} {} fftfilt (@var{b}, @var{x}, @var{n})
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
22 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
23 ## With two arguments, @code{fftfilt} filters @var{x} with the FIR filter
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
24 ## @var{b} using the FFT.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
25 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
26 ## Given the optional third argument, @var{n}, @code{fftfilt} uses the
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
27 ## overlap-add method to filter @var{x} with @var{b} using an N-point FFT.
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2847
diff changeset
28 ## @end deftypefn
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
29
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
31 ## Created: 3 September 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
32 ## Adapted-By: jwe
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
33
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
34 function y = fftfilt (b, x, N)
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
35
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
36 ## If N is not specified explicitly, we do not use the overlap-add
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
37 ## 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
38 ## 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
39 ## 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
40 ## one blocks, but if the user knows better ...
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
41
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
42 if (nargin < 2 || nargin > 3)
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
43 usage (" fftfilt (b, x, N)");
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
44 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
45
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
46 [r_x, c_x] = size (x);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
47 [r_b, c_b] = size (b);
1337
52a3f38cbfeb [project @ 1995-08-25 02:00:18 by jwe]
jwe
parents: 1315
diff changeset
48 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
49 error ("fftfilt: both x and b should be vectors");
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
50 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
51 l_x = r_x * c_x;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
52 l_b = r_b * c_b;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
53
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
54 if ((l_x == 1) && (l_b == 1))
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
55 y = b * x;
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
56 return;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
57 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
58
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
59 x = reshape (x, 1, l_x);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
60 b = reshape (b, 1, l_b);
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
61
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
62 if (nargin == 2)
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
63 ## 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
64 ## length (b) - 1 as number of points ...
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
65 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
66 y = ifft (fft (x, N) .* fft(b, N));
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
67 else
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
68 ## Use overlap-add method ...
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
69 if !(is_scalar (N))
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
70 error ("fftfilt: N has to be a scalar");
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
71 endif
1337
52a3f38cbfeb [project @ 1995-08-25 02:00:18 by jwe]
jwe
parents: 1315
diff changeset
72 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
73 L = N - l_b + 1;
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
74 B = fft (b, N);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
75 R = ceil (l_x / L);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
76 y = zeros (1, l_x);
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
77 for r = 1:R;
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
78 lo = (r - 1) * L + 1;
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
79 hi = min (r * L, l_x);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
80 tmp = ifft (fft (x(lo:hi), N) .* B);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
81 hi = min (lo+N-1, l_x);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
82 y(lo:hi) = y(lo:hi) + tmp(1:(hi-lo+1));
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
83 endfor
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
84 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
85
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
86 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
87
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
88 ## 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
89 ## should also be
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
90
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
91 if (! (any (imag (x)) || any (imag (b))))
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
92 y = real (y);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
93 endif
1026
9fc405c8c06c [project @ 1995-01-11 21:17:01 by jwe]
jwe
parents: 904
diff changeset
94 if (! (any (x - round (x)) || any (b - round (b))))
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
95 y = round (y);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
96 endif
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 endfunction