Why is the convolution for the same signals different?
8 views (last 30 days)
Show older comments
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 Comments
Paul
on 3 Aug 2023
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.
Accepted Answer
Dyuman Joshi
on 2 Aug 2023
Edited: Dyuman Joshi
on 2 Aug 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')
fun(100)
fprintf('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 Comments
Paul
on 2 Aug 2023
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
on 3 Aug 2023
Edited: Dyuman Joshi
on 3 Aug 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.
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!