558
|
1 function c=conv(a,b) |
|
2 # |
|
3 # usage: conv(a,b) |
|
4 # |
|
5 # Returns the convolution of vectors a and b. The resulting vector |
|
6 # is of length(a)+length(b)-1. If a and b are polynomial coefficients |
|
7 # conv(a,b) is equivalent to polynomial multiplication. |
|
8 |
|
9 # written by Gerhard Kircher on Aug 27, 1993 |
786
|
10 # modified by KH (Kurt.Hornik@ci.tuwien.ac.at) on Dec 23, 1993. |
558
|
11 |
|
12 l_a = length(a); |
|
13 l_b = length(b); |
|
14 return_row = 0; |
|
15 if (l_a > l_b) |
|
16 if (rows(a) == 1) |
|
17 return_row = 1; |
|
18 endif |
|
19 else |
|
20 if (rows(b) == 1) |
|
21 return_row = 1; |
|
22 endif |
|
23 endif |
|
24 a = reshape(a, l_a, 1); |
|
25 b = reshape(b, l_b, 1); |
|
26 if (l_a == 1 || l_b == 1) |
|
27 c = a * b; |
|
28 else |
|
29 l_c = l_a + l_b - 1; |
|
30 a(l_c) = 0; |
|
31 b(l_c) = 0; |
|
32 c = ifft(fft(a) .* fft(b)); |
|
33 if !( any(imag(a)) || any(imag(b)) ) |
|
34 c = real(c); |
|
35 endif |
|
36 if !( any(a-round(a)) || any(b-round(b)) ) |
|
37 c = round(c); |
|
38 endif |
|
39 endif |
|
40 if (return_row == 1) |
|
41 c = c'; |
|
42 end |
|
43 endfunction |