558
|
1 function y = conv(a,b) |
|
2 #Convolve two vectors. |
|
3 #y = conv(a,b) returns a vector of length equal to length(a)+length(b)-1. |
|
4 #If a and b are polynomial coefficient vectors, conv returns the |
|
5 #coefficients of the product polynomial. |
|
6 # |
|
7 #SEE ALSO: deconv, poly, roots, residue, polyval, polyderiv, polyinteg |
|
8 |
|
9 # Author: |
|
10 # Tony Richardson |
|
11 # amr@mpl.ucsd.edu |
|
12 # June 1994 |
|
13 |
|
14 if(nargin != 2) |
|
15 error("usage: conv(a,b)"); |
|
16 endif |
|
17 |
|
18 if(is_matrix(a) || is_matrix(b)) |
|
19 error("conv: both arguments must be vectors"); |
|
20 endif |
|
21 |
|
22 la = length(a); |
|
23 lb = length(b); |
|
24 |
|
25 ly = la + lb - 1; |
|
26 |
|
27 # Ensure that both vectors are row vectors. |
|
28 if(rows(a) > 1) |
|
29 a = reshape(a,1,la); |
|
30 endif |
|
31 if(rows(b) > 1) |
|
32 b = reshape(b,1,lb); |
|
33 endif |
|
34 |
|
35 # Use the shortest vector as the coefficent vector to filter. |
|
36 if (la < lb) |
|
37 if(ly>lb) |
|
38 x = [b zeros(1,ly-lb)]; |
|
39 else |
|
40 x = b; |
|
41 endif |
|
42 y = filter(a,1,x); |
|
43 else |
|
44 if(ly>la) |
|
45 x = [a zeros(1,ly-la)]; |
|
46 else |
|
47 x = a; |
|
48 endif |
|
49 y = filter(b,1,x); |
|
50 endif |
|
51 |
|
52 endfunction |