Why is the convolution for the same signals different?

Here is the code for convolution of two signals. I have two questions:
1) If I change the number of datapoints from n=1000 to n=100, the output is different. Why? How do I make sure that the given outout is the correct result?
2) Is it okay to label the x-axis of the last plot as "time" in this particular case?
n = 1000; % Whatever.
f = .10; % Whatever.
tau = 2; % Whatever.
t = linspace(0, 4*pi, n); % Time.
x = sin(2*pi*f*t);
h = exp(-t/tau);
y = conv(x, h, 'full');
% Plot u
subplot(3, 1, 1);
plot(t, x, 'b--', 'LineWidth', 3);
grid on;
set(gca,'xscale', 'linear', 'FontSize', 28)
set(gca,'yscale', 'linear', 'FontSize', 28)
set(gca,'XMinorTick', 'on')
set(gca,'YMinorTick', 'on')
xlim([0 12]);
xlabel('Time, $t$', 'interpreter', 'latex', 'FontSize', 40);
ylabel('$x\left(t\right)$', 'interpreter', 'latex', 'FontSize', 40);
title('Input, $x\left(t\right)=\sin\left(2\pi f t\right),\mbox{ }f=0.10$', 'interpreter', 'latex', 'FontSize', 44);
% Plot g
subplot(3, 1, 2);
plot(t, h, 'r-.', 'LineWidth', 3);
grid on;
set(gca,'xscale', 'linear', 'FontSize', 28)
set(gca,'yscale', 'linear', 'FontSize', 28)
set(gca,'XMinorTick', 'on')
set(gca,'YMinorTick', 'on')
xlim([0 12]);
xlabel('Time, $t$', 'interpreter', 'latex', 'FontSize', 40);
ylabel('$h\left(t\right)$', 'interpreter', 'latex', 'FontSize', 40);
title('Impulse response, $h\left(t\right)=e^{-t/\tau},\mbox{ }\tau=2$', 'interpreter', 'latex', 'FontSize', 44);
% Plot out
subplot(3, 1, 3);
plot(y, 'k-', 'LineWidth', 3);
grid on;
set(gca,'xscale', 'linear', 'FontSize', 28)
set(gca,'yscale', 'linear', 'FontSize', 28)
set(gca,'XMinorTick', 'on')
set(gca,'YMinorTick', 'on')
xlim([0 140]);
xlabel('Time, $t$', 'interpreter', 'latex', 'FontSize', 40);
ylabel('$y\left(t\right)$', 'interpreter', 'latex', 'FontSize', 40);
title('Output, $y\left(t\right)=x\left(t\right)*h\left(t\right)$', 'interpreter', 'latex', 'FontSize', 44);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

3 comentarios

Paul
Paul el 2 de Ag. de 2023
Editada: Paul el 3 de Ag. de 2023
Hi Vikash,
Two things to consider:
1) The code assumes that x(t) and h(t) are both 0 outside the interval 0 <= t <= 4*pi. If either x(t) or h(t) extend to infinite time, then conv can't be used, at least not to get the full solution (though in a practical sense there will be some time, tc, where for t > tc h(t) could probably be safely assumed to be zero).
2). If x(t) and h(t) are both 0 outside that interval, then the output conv needs to be multiplied by a dt so that result approximates the convolution integral, assuming the convolution integral is the goal.
3. If the convolution integral is the goal, consider the Symbolic Toolobox, if you have access to it, to get its closed form expression.
Thanks Paul for your clarification.
So according to your second point, the output needs to be uniformly scaled by dt times. It implies that the "nature" of the solution stays the same, but a scaled version of the former. Am I correct?
Correct. In this problem, that scaling needs to be applied if you want the the output of conv, which is the convolution sum of discrete time samples, to be samples that approximate the convolution integral of the underlying continuous time signals. If either x(t) or h(t) had included a Dirac delta function, we'd have to do just bit more work to use conv to approximate the convolution integral.

Iniciar sesión para comentar.

 Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 2 de Ag. de 2023
Editada: Dyuman Joshi el 2 de Ag. de 2023
"Why?"
Because you have limited the x axis, so it only displays the curve in that limit, and why it looks different.
"Is it okay to label the x-axis of the last plot as "time" in this particular case?"
No, because you are not plotting against time. It is plotting against the indices of the data.
Edit - commented out the xlabel in the 3rd subplot.
This is the output from the code after removing all the xlim() commands, you can see that the outputs are similar -
fprintf('Output for n = 100')
Output for n = 100
fun(100)
fprintf('Output for n = 1000')
Output for n = 1000
fun(1000)
%%Convert the script into a function to test for different values of n
function fun(n)
f = 0.10; % Whatever.
tau = 2; % Whatever.
t = linspace(0, 4*pi, n); % Time.
x = sin(2*pi*f*t);
h = exp(-t/tau);
y = conv(x, h, 'full');
%%Plot a new figure
figure
% Plot u
subplot(3, 1, 1);
plot(t, x, 'b--', 'LineWidth', 3);
grid on;
set(gca,'xscale', 'linear', 'FontSize', 28)
set(gca,'yscale', 'linear', 'FontSize', 28)
set(gca,'XMinorTick', 'on')
set(gca,'YMinorTick', 'on')
xlabel('Time, $t$', 'interpreter', 'latex', 'FontSize', 40);
ylabel('$x\left(t\right)$', 'interpreter', 'latex', 'FontSize', 40);
title('Input, $x\left(t\right)=\sin\left(2\pi f t\right),\mbox{ }f=0.10$', 'interpreter', 'latex', 'FontSize', 44);
% Plot g
subplot(3, 1, 2);
plot(t, h, 'r-.', 'LineWidth', 3);
grid on;
set(gca,'xscale', 'linear', 'FontSize', 28)
set(gca,'yscale', 'linear', 'FontSize', 28)
set(gca,'XMinorTick', 'on')
set(gca,'YMinorTick', 'on')
xlabel('Time, $t$', 'interpreter', 'latex', 'FontSize', 40);
ylabel('$h\left(t\right)$', 'interpreter', 'latex', 'FontSize', 40);
title('Impulse response, $h\left(t\right)=e^{-t/\tau},\mbox{ }\tau=2$', 'interpreter', 'latex', 'FontSize', 44);
% Plot out
subplot(3, 1, 3);
plot(y, 'k-', 'LineWidth', 3);
grid on;
set(gca,'xscale', 'linear', 'FontSize', 28)
set(gca,'yscale', 'linear', 'FontSize', 28)
set(gca,'XMinorTick', 'on')
set(gca,'YMinorTick', 'on')
%xlabel('Time, $t$', 'interpreter', 'latex', 'FontSize', 40);
ylabel('$y\left(t\right)$', 'interpreter', 'latex', 'FontSize', 40);
title('Output, $y\left(t\right)=x\left(t\right)*h\left(t\right)$', 'interpreter', 'latex', 'FontSize', 44);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
end

6 comentarios

Thanks Dyuman, appreciate it. That was a silly mistake.
You are welcome!
Paul
Paul el 2 de Ag. de 2023
Editada: Paul el 2 de Ag. de 2023
Shouldn't y be plotted vs. time, not index?
Dyuman Joshi
Dyuman Joshi el 2 de Ag. de 2023
Editada: Dyuman Joshi el 2 de Ag. de 2023
"Shouldn't y be plotted vs. time, not index?"
I don't know about should, but you can not directly plot y vs time, as the dimensions don't match.
One can however, plot y upto the number of elements in time, but that wouldn't make sense as OP is performing full convolution.
And as OP has directly plotted a vector via plot, the resulting x coordinates are the indices of the vector.
I purposely wrote "time" , not "t" (though I originally wrote "t" and then corrected myself). So I think it should be
plot((0:numel(y)-1)*t(2),y, 'k-', 'LineWidth', 3);
Dyuman Joshi
Dyuman Joshi el 3 de Ag. de 2023
Editada: Dyuman Joshi el 3 de Ag. de 2023
t is the time variable in the code, so I responded according to that.
But your code makes it clear to me what you meant. That seems to be way to approach it.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2020b

Preguntada:

el 2 de Ag. de 2023

Editada:

el 3 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by