1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} fftconv (@var{x}, @var{y})
21 ## @deftypefnx {Function File} {} fftconv (@var{x}, @var{y}, @var{n})
22 ## Convolve two vectors using the FFT for computation.
24 ## @code{c = fftconv (@var{x}, @var{y})} returns a vector of length equal to
25 ## @code{length (@var{x}) + length (@var{y}) - 1}.
26 ## If @var{x} and @var{y} are the coefficient vectors of two polynomials, the
27 ## returned value is the coefficient vector of the product polynomial.
29 ## The computation uses the FFT by calling the function @code{fftfilt}. If
30 ## the optional argument @var{n} is specified, an N-point FFT is used.
31 ## @seealso{deconv, conv, conv2}
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Created: 3 September 1994
38 function c = fftconv (x, y, n)
40 if (nargin < 2 || nargin > 3)
44 if (! (isvector (x) && isvector (y)))
45 error ("fftconv: both A and B must be vectors");
49 if ((la == 1) || (lb == 1))
59 error ("fftconv: N must be a scalar");
61 c = fftfilt (x, y, n);
68 %% FIXME: Borrow tests from conv.m. May need a tolerance on the assert comparison
74 %! assert (fftconv (x, x), [1; 2; 3; 2; 1], 5*eps);
75 %! assert (fftconv (y, y), [1, 2, 3, 2, 1], 5*eps);
76 %! assert (fftconv (x, y), [1, 2, 3, 2, 1], 5*eps);
77 %! assert (fftconv (y, x), [1; 2; 3; 2; 1], 5*eps);
78 %! assert (fftconv (c, x), [3; 3; 3], 5*eps);
79 %! assert (fftconv (c, y), [3, 3, 3], 5*eps);
80 %! assert (fftconv (x, c), [3; 3; 3], 5*eps);
81 %! assert (fftconv (y, c), [3, 3, 3], 5*eps);
82 %! assert (fftconv (b, c), 6, 5*eps);
87 %! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
88 %! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
92 %! assert (size(conv(a,b)), [numel(a)+numel(b)-1, 1])
93 %! assert (size(conv(b,a)), [numel(a)+numel(b)-1, 1])
98 %! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
99 %! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
101 %% Test input validation
103 %!error fftconv (1,2,3,4);
104 %!error fftconv ([1, 2; 3, 4], 3);
105 %!error fftconv (2, []);
106 %!error fftconv ([1,1], [2,2] , [3, 4]);