user3450687
user3450687

Reputation: 339

Matlab Convolution 'same'

I read Matlab documentation about convolution and i have found this :

u = [-1 2 3 -2 0 1 2]; v = [2 4 -1 1]; w = conv(u,v,'same')

And the answer is

 w = 15     5    -9     7     6     7    -1

Do you know how was w calculated? I know how you calculate the normal convolution product, but what about this?

Thank you for you help in advance !

Upvotes: 0

Views: 2870

Answers (2)

Chenfei Zhu
Chenfei Zhu

Reputation: 11

Actually the above answer is almost correct, except for the misplaced u and v in the last line of the code. That is:

w = w_full(floor(length(v)/2) + (1:length(u))); % 'same' like convolution

After all, the length of the output is the same as the length of u, not v.

Upvotes: 1

johnnyprimavera
johnnyprimavera

Reputation: 11

Apparently your question is how to determine the limits of the convolution when shape is set to 'same'. From the 'full' shape convolution provided by Matlab, 'same' subvector can be calculated as follows:

u = [-1 2 3 -2 0 1 2];
v = [2 4 -1 1];
w = conv(u,v,'same');

w_full = conv(u,v,'full'); % full size (zero-padded) convolution
w = w_full(floor(length(u)/2) + (1:length(v))); % 'same' like convolution

Upvotes: 0

Related Questions