Reputation: 689
I got the matrix below:
9 18 27 36 45
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
and the kernel:
-0.5+0.8662i 1 -0.5-0.8662i
I'm trying to perform the convolution using valid mode:
ans = conv2(matrix,kernel,'valid');
The matlab returns:
0.0000+15.5916i 0.0000+15.5916i 0.0000+15.5916i
My question is how I can achieve the same results like matlab. I'm trying to do in the matlab in the first point, but the results is different.
a = matrix(1,1) * kernel(1);
a = a + matrix(1,2) * kernel(2);
a = a + matrix(1,3) * kernel(3);
Result: 0-15.5916i
For some reason the sign of the imaginary is positive using convolution. Why ?
Upvotes: 5
Views: 303
Reputation: 5821
I believe convolution is usually performed by "flipping" the kernel (left-right, up-down) and then sliding it across the matrix to perform a sum of multiplications.
In other words, what matlab is actually computing is:
a = matrix(1,1) * kernel(3);
a = a + matrix(1,2) * kernel(2);
a = a + matrix(1,3) * kernel(1);
Upvotes: 7
Reputation: 112659
In the convolution process the kernel is flipped. So you have to flip it too in your check; that is, swap kernel(1)
and kernel(3)
, as follows:
>> a = matrix(1,1) * kernel(3);
>> a = a + matrix(1,2) * kernel(2);
>> a = a + matrix(1,3) * kernel(1)
a =
27.0000 +15.5916i
This is in accordance with the result of the convolution:
>> A = conv2(matrix,kernel,'valid');
>> A(1,1)
ans =
27.0000 +15.5916i
Upvotes: 6