MATLAB: Convolution of 2 signals ,

convolution

so i am getting the answer 100 times bigger than it shoud , as the true peak is 0.6366 and not 63.66 , how to fix or whats the problem ? thanks
%here is my code:
clear;
close all;
%%X(t)
dt=0.01;
t = [-10: dt: 10];
x = zeros(1,length(t));
for i = 1 : length(t)
if t(i) >=0 && t(i) <= 2
x(i) = sin( pi * t(i) ) ;
end %if

end %for

figure
plot(t,x);
title('x(t)')
xlabel('t')
ylabel('x(t)')
%% Define h(t)
h = zeros(1,length(t));
for i = 1 : length(t)
if t(i) >=-8 && t(i) <=-3
h(i) = 1 ;
end %if
end %for
figure
plot(t,h, '.');
title('h(t)')
xlabel('t')
ylabel('h(t)')
%%define y
y = conv(x,h, 'same');
figure
plot(t,y);
title('y(t)')
xlabel('t')
ylabel('y(t)')

Best Answer

conv() function is defined for discrete-time convolution, where the index of the x-axis are integers. You are trying to do a continuous-time convolution, but conv() does not know the sampling time of the signal. You need to multiply with dt to get the correct output.
y = conv(x,h, 'same')*dt;