(Code snippet with example below).
In MATLAB, assume I have a long signal vector
N) that I wish to convolve with a square wave
h (consisting of
H 1's). This would give a convolution result of length
Since I have to perform this operation quite often, I thought that I could speed up my code by downsampling the signal
y (and also the square wave
h) and then perform the convolution. The downsampled signals are referred to as
However, the result is shifted when compared to the downsampled version of the convolution result (if corrected for scaling differences). So in general I found that:
However, the shift is between the two results is not constant, i.e. shifting such that the maxima align, causes the edges to be different.
- Where does this shift originate from?
- How can this code be modified such that the downsampled convolution result equals the convolution result of the downsampled signals?
N = 100; dsfactor = 3; H = dsfactor*3; x = linspace(0,10,N); y = sind(18*x); h = ones(H,1); convyh = conv(y,h); convyh_ds = downsample(convyh,dsfactor)./H; convyh_ds3 = conv(downsample(y,dsfactor),downsample(h,dsfactor))./(ceil(H/dsfactor)); figure; plot(convyh_ds,'DisplayName','downsample(conv)','LineWidth',1); hold on; plot(convyh_ds3,'DisplayName','conv(downsample)','LineWidth',1); grid minor legend('show'); line([0 length(convyh_ds2)],[0 0],'LineStyle','--');