Why is the convolution for the same signals different?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Paul
2023-8-3
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.
采纳的回答
Dyuman Joshi
2023-8-2
编辑:Dyuman Joshi
2023-8-2
"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 个评论
Paul
2023-8-2
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
2023-8-3
编辑:Dyuman Joshi
2023-8-3
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!