MATLAB: Can’t plot convolution

convolutionplotsinewave

Hi, I'm having trouble plotting the convolution between a square wave and sine function, here is the code:
t=[0:0.001:1.5];
f=40 %square wave frequency
d=[0:1/f:1.5]; %repetition each 1/40
y=pulstran(t, d,'rectpuls',0.01);
f_s= 10; %sine wave frequency
sin_10= sin(2*pi*f_0*t); %sine wave
r = conv(y,sin_10);
subplot( 3, 1, 1); plot(t,y); title('Square wave');
subplot( 3, 1, 2); plot(t,sin_10); title('Sine wave');
subplot( 3, 1, 3); plot(t,r); title('Square and Sine convolution')
It gives me the error: Error using plot Vectors must be the same length.
Any help?

Best Answer

Remember, the convolution is larger than the two input signals. It's the sum of the two signal lengths. You need to take that into account and expand your t, or else use the 'same' option in conv().
r = conv(y, sin_10, 'same');