Conv produces different answers for different steps
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Aqeel Mohamed
le 30 Avr 2020
Modifié(e) : Aqeel Mohamed
le 2 Mai 2020
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 commentaires
Réponse acceptée
Ameer Hamza
le 30 Avr 2020
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Subplots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!