Conv produces different answers for different steps
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!