]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/convmtx.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / convmtx.m
1 ## Copyright (C) 2003 David Bateman <adb014@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} convmtx (@var{a}, @var{n})
18 ## If @var{a} is a column vector and @var{x} is a column vector
19 ## of length @var{n}, then 
20 ##
21 ## @code{convmtx(@var{a}, @var{n}) * @var{x}}
22 ##
23 ## gives the convolution of of @var{a} and @var{x} and is the
24 ## same as @code{conv(@var{a}, @var{x})}. The difference is if
25 ## many vectors are to be convolved with the same vector, then
26 ## this technique is possibly faster.
27 ##
28 ## Similarly, if @var{a} is a row vector and @var{x} is a row 
29 ## vector of length @var{n}, then
30 ##
31 ## @code{@var{x} * convmtx(@var{a}, @var{n})}
32 ##
33 ## is the same as @code{conv(@var{x}, @var{a})}.
34 ## @end deftypefn
35 ## @seealso{conv}
36
37 function b = convmtx (a, n)
38
39   if (nargin != 2)
40     print_usage;
41   endif
42    
43   [r, c] = size(a);
44   
45   if ((r != 1) && (c != 1)) || (r*c == 0)
46     error("convmtx: expecting vector argument");
47   endif
48   
49   b = toeplitz([a(:); zeros(n-1,1)],[a(1); zeros(n-1,1)]);
50   if (c > r)
51     b = b.';
52   endif
53
54 endfunction
55
56 %!assert(convmtx([3,4,5],3),[3,4,5,0,0;0,3,4,5,0;0,0,3,4,5])
57 %!assert(convmtx([3;4;5],3),[3,0,0;4,3,0;5,4,3;0,5,4;0,0,5])