]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/fftconv.m
update packages
[CreaPhase.git] / octave_packages / m / signal / fftconv.m
1 ## Copyright (C) 1994-2012 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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
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.
23 ##
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.
28 ##
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}
32 ## @end deftypefn
33
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Created: 3 September 1994
36 ## Adapted-By: jwe
37
38 function c = fftconv (x, y, n)
39
40   if (nargin < 2 || nargin > 3)
41     print_usage ();
42   endif
43
44   if (! (isvector (x) && isvector (y)))
45     error ("fftconv: both A and B must be vectors");
46   endif
47   la = length (x);
48   lb = length (y);
49   if ((la == 1) || (lb == 1))
50     c = x * y;
51   else
52     lc = la + lb - 1;
53     x(lc) = 0;
54     y(lc) = 0;
55     if (nargin == 2)
56       c = fftfilt (x, y);
57     else
58       if (! isscalar (n))
59         error ("fftconv: N must be a scalar");
60       endif
61       c = fftfilt (x, y, n);
62     endif
63   endif
64
65 endfunction
66
67
68 %% FIXME: Borrow tests from conv.m.  May need a tolerance on the assert comparison
69 %!test
70 %!  x = ones(3,1);
71 %!  y = ones(1,3);
72 %!  b = 2;
73 %!  c = 3;
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);
83
84 %!test
85 %!  a = 1:10;
86 %!  b = 1:3;
87 %!  assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
88 %!  assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
89
90 %!  a = (1:10).';
91 %!  b = 1:3;
92 %!  assert (size(conv(a,b)), [numel(a)+numel(b)-1, 1])
93 %!  assert (size(conv(b,a)), [numel(a)+numel(b)-1, 1])
94
95 %!test
96 %!  a = 1:10;
97 %!  b = (1:3).';
98 %!  assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
99 %!  assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
100
101 %% Test input validation
102 %!error fftconv (1);
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]);