annotate scripts/signal/fftfilt.m @ 1026:9fc405c8c06c

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