Mercurial > hg > octave-lyh
annotate scripts/signal/fftconv.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | e79f59d31a74 |
children | c776f063fefe |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2005, 2006, |
2 ## 2007 John W. Eaton | |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
1026 | 19 |
3367 | 20 ## -*- texinfo -*- |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
21 ## @deftypefn {Function File} {} fftconv (@var{a}, @var{b}) |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
22 ## @deftypefnx {Function File} {} fftconv (@var{a}, @var{b}, @var{n}) |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
23 ## Convolve two vectors using the FFT for computation. |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
24 ## |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
25 ## @code{c = fftconv (@var{a}, @var{b})} returns a vector of length equal to |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
26 ## @code{length (@var{a}) + length (@var{b}) - 1}. |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
27 ## If @var{a} and @var{b} are the coefficient vectors of two polynomials, the |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
28 ## returned value is the coefficient vector of the product polynomial. |
3426 | 29 ## |
3367 | 30 ## The computation uses the FFT by calling the function @code{fftfilt}. If |
31 ## the optional argument @var{n} is specified, an N-point FFT is used. | |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
32 ## @seealso{deconv, conv, conv2} |
3367 | 33 ## @end deftypefn |
787 | 34 |
5428 | 35 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 36 ## Created: 3 September 1994 |
37 ## Adapted-By: jwe | |
38 | |
2311 | 39 function c = fftconv (a, b, N) |
787 | 40 |
41 if (nargin < 2 || nargin > 3) | |
6046 | 42 print_usage (); |
787 | 43 endif |
2325 | 44 |
4030 | 45 if (! (isvector (a) && isvector (b))) |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
46 error ("fftconv: both A and B must be vectors"); |
787 | 47 endif |
48 la = length (a); | |
49 lb = length (b); | |
50 if ((la == 1) || (lb == 1)) | |
51 c = a * b; | |
52 else | |
53 lc = la + lb - 1; | |
54 a(lc) = 0; | |
55 b(lc) = 0; | |
56 if (nargin == 2) | |
57 c = fftfilt (a, b); | |
58 else | |
11124 | 59 if (! isscalar (N)) |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
60 error ("fftconv: N must be a scalar"); |
787 | 61 endif |
62 c = fftfilt (a, b, N); | |
63 endif | |
64 endif | |
65 | |
66 endfunction | |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
67 |
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
68 %% FIXME: Borrow tests from conv.m. May need a tolerance on the assert comparison |
11124 | 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]); |