Conv produces different answers for different steps

조회 수: 4 (최근 30일)
Aqeel Mohamed
Aqeel Mohamed 2020년 4월 30일
편집: Aqeel Mohamed 2020년 5월 2일
Dear all,
I have this piece of code I wrote to plot two signals - x(t) and h(t) - alongside their time range then find the convolution of the two signals.
It works fine with a step size of 1. Howerver, when I try to change the step size - to have better graphs, I get an error in the vectors dimensions. Hence, I can't plot the graphs.
fs = 1; % Sampling rate - step size
x = [2*((-2:1)+2).^2 18*1.^(2:3) -9*((4:5)-5)]; % x(t) function
tx1 = -2; tx2 = 5; tx = tx1:fs:tx2; % time for function x
h = [3*(0:2) -6*((3:4)-3)]; % h(t) function
th1 = 0; th2 = 4; th = th1:fs:th2; % time for function h
figure(1)
subplot(2,1,1); plot(tx,x); title('x(t)');
subplot(2,1,2); plot(th,h); title('h(t)');
t = ((tx1+th1):fs:(tx2+th2)); % time for function yc (conv)
yc = conv(x,h); % Continuous convolution
figure(2)
subplot(2,1,1); stem(yc); xlabel('--->n'); ylabel('y(n)'); title('Discrete Convolution');
subplot(2,1,2); plot(t,yc); xlabel('t'); ylabel('y(t)'); title('Continuous Convolution');
When I change fs to 0.1, I get this error. Is there a way to solve this issue?
Error using plot
Vectors must be the same length.
Thanks in advance for your answers.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 30일
It happens because when you decrease 'fs', the length of tx increase, but the value of vector 'x' is hardcoded
x = [2*((-2:1)+2).^2 18*1.^(2:3) -9*((4:5)-5)];
Its length remains the same, so the plot function gives an error. You need to increase its length of 'x' to be of the same size as the time vector.
As a side note, remember that conv() is a function for discrete-time convolution. If you try to change the step size, then you also need to multiply it with the step-size to get the correct result
yc = conv(x,h)*fs;
  댓글 수: 6
Aqeel Mohamed
Aqeel Mohamed 2020년 4월 30일
Works great!
Thank you very much Ameer :)
Ameer Hamza
Ameer Hamza 2020년 4월 30일
I am glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by