2
votes

I have noticed a shift of -1 when doing convolution in MatLab (R2011b) using the conv function and I don't understand why. I am using the 'same' option to the convolution function because my signal and the function I am convolving with are the same N pixels in length and I want my result to also be N pixels.

This shift only happens when I have my value of N as even number.

I wrote this short script to illustrate the problem. It convolves a rectangle pulse with an impulse response, so I dont expect any shift in my result.

%% Set up rectangle pulse

N = 21;

signal = zeros(N, 1);

% Designate some pixels in the signal as 1's to make rectangle pulse

signal(9:11) = 1;

%% Set up impulse for convolution

impulse = zeros(N, 1);

impulse(round(N/2)) = 1;

%% Convolution

convolutionResult = conv(signal, impulse, 'same');

%% Plot Results - not shown

When N is odd the result looks OK, i.e. the rectangle pulse has values of 1 at pixels 9, 10 and 11, as expected, the same as before convolution.

N is odd

But if N is odd, then the rectangle pulse has values of 1 at pixels 8, 9 and 10, so a shift of -1 which I don't get. Thanks in advance.

enter image description here

Does anybody understand why this happens?

1
OK I know why, after writing it out I noticed that the position of the '1' value in my impulse is obviously the reason. Not to worry! - DukeOfMarmalade

1 Answers

4
votes

For w = conv(u,v,'same');, It says,

same : Returns the central part of the convolution of the same size as u.

when N is even and same for both signals the result of convolution has 2N - 1 samples, which is odd, so when trying to select the center of that with an even length, N, the expression central part looses its meaning.

If time axis is important for you, don't use same option.