Can't plot convolution

8 views (last 30 days)
Gabriel Ramirez
Gabriel Ramirez on 15 May 2018
Commented: Gabriel Ramirez on 15 May 2018
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?

Accepted Answer

Image Analyst
Image Analyst on 15 May 2018
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');
  1 Comment
Gabriel Ramirez
Gabriel Ramirez on 15 May 2018
Thank you so much. It is my second code in Matlab, I don't understand the comands quiet well yet, thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!