1

The question is pretty basic, I try to repeat results for continues convolution of two boxcar functions with conv function in matlab. Accordingly to http://en.wikipedia.org/wiki/Convolution it should result in the area overlap between the two given function. The results of discrete conv should be scaled to get a proper value for the area. Some suggest scaling with sampling frequency, but it does not give correct results for the area. It was suggested to use sum(f) in Scale Factor in Matlabs `conv()` but it does not work either. Can anybody explain what scaling factor should be used? Or maybe there is an error in the following code?

dx = 0.01
xmin = -0.7;
xmax =  0.7;
box = @(x) 0.5 * (sign(x - xmin) - sign(x - xmax));

x  = -2:dx:2;
f1 = box(x); 
f2 = box(x) * 1.5;
conv1 = conv(f1, f2, 'same');           % no scaling
conv2 = conv(f1, f2, 'same') * dx;      % scale with sampling frequency
conv3 = conv(f1 / sum(f1), f2, 'same'); % scale with sum of f1
conv4 = conv(f1, f2 / sum(f2), 'same'); % scale with sum of f2
conv5 = conv(f1 / sum(f1), f2 / sum(f1), 'same'); % scale with sum of f1 and f2
exact = ones(size(x)) * (xmax - xmin) * min(max(f1), max(f2));
plot(x, f1, 'c--o', x, f2, 'm--o' ...    % plot functions for reference 
     , x, conv2, 'r-'        ... 
     , x, conv3, 'g-'        ... 
     , x, conv4, 'b-'        ... 
     , x, conv5, 'y-'        ... 
     , x, exact, 'k:'         ...    % excat area
     );
legend({'f1' 'f2' 'dx scale' 'f1 scale' 'f2 scale' 'f1 and f2 scale' 'exact'})

Results produced by the code above

4
  • As far as my understanding of convolutions go, the red curve looks spot-on. Look at the definition in the Wikipedia article you linked to and substitute 't' with 0. You get a value of 2.1 which is approimately where the figure says the red curve lies
    – bas
    Jul 6, 2013 at 14:56
  • If you need overlap, something like this would work too: g=[f1;f2]; gmin = min(g,[],1); overlap = trapz(x,gmin); (I assume all functions are positive. If they aren't you need to figure out some algebra.)
    – bas
    Jul 6, 2013 at 15:05
  • @bas, at t = 0 the position of "boxes" is actually shown in the attache figure, and overlapping area is the area of the small box, that is 1.4. I'd like to understand how conv function works, not only find the overlap, but thanks for the hint anyway.
    – zeliboba7
    Jul 6, 2013 at 18:29
  • think of convolution as the weighted sum of a function weighted by another function. there may be 'deep' reasons for the time flipping of the second function, but the way i understand it, the time flipping makes some fourier analyses look very pretty. so it stuck
    – bas
    Jul 9, 2013 at 19:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.