The prompt is to develop a program to evaluate convolutions. This is to be done without using MATLAB's built in conv function. Therefore utilizing the Fourier transform, multiplying the two functions together and then inverse Fourier transforming the product. The transform is done by using direct integration. The trapz function is recommended form of integration to accomplish this goal.
提示是制定评估卷积的程序。这是在不使用MATLAB的内置转换功能的情况下完成的。因此,利用傅立叶变换,将两个函数相乘,然后对产品进行逆傅里叶变换。通过使用直接集成完成转换。 trapz函数是推荐的集成形式,以实现此目标。
I would appreciate ANY feedback on how to improve my code, please thoroughly explain what the improvements and link an reference as to how they work.
我很感激有关如何改进我的代码的任何反馈,请彻底解释改进的内容并链接一个关于它们如何工作的参考。
Given the code:
鉴于代码:
t = -5:.1:5;
w = pi;
X = zeros(101,1);
H = zeros(101,1);
Y = zeros(101,1);
y = zeros(101,1);
if t >= 0
x = 0;
h = 0;
else
x = exp((-3.*t)+(-1i*w.*t));
h = exp((-2*t)+(-1i*w.*t));
end
for k=2:101
X(k)=trapz(t(1:k),x(1:k));
H(k)=trapz(t(1:k),h(1:k));
Y = (X.*H)*exp(1i*w.*t);
y(k) = (1/(2*pi))*trapz(t(1:k),Y(1:k));
end
disp (length(x))
disp (length(X))
disp (length(Y))
disp (length(y))
disp (y)
figure(1);
subplot(1,2,1),plot(t,real(y));grid on;
As I do not have enough reputation to directly post images, the actual output and desired output are as follows:
由于我没有足够的声誉直接发布图像,实际输出和所需的输出如下:
实际情节就是这样。
想要的情节是这样的。
My primary question is this: why is my plot not working?
我的主要问题是:为什么我的情节不起作用?
Secondarily: What is unneeded in this code? What could make this code more efficient?
其次:此代码中不需要什么?什么可以使这个代码更有效?
1 个解决方案
#1
2
I won't do your entire homework, but I'll give you a few clues:
我不会做你的整个作业,但我会给你一些线索:
Skip the if t < 0
part, it doesn't work. For your exam, try to understand why. If you can't figure it out, come with your best guess and you might get an explanation =)
跳过if t <0部分,它不起作用。对于您的考试,请尝试了解原因。如果你无法弄明白,请提出你最好的猜测,你可能会得到一个解释=)
Try the following instead (no loops or if
s needed:
请尝试以下方法(无需循环或ifs:
x = exp((-3.*t)+(-1i*pi.*t)).*(t>0);
And the same for h
. Try to understand what .*(t<0)
does in this context.
h同样如此。试着理解。*(t <0)在这种情况下的作用。
This one: Y = (X.*H)*exp(1i*w.*t);
should be outside the loop. Why? Make a guess and you might get guidance if you're wrong.
这一个:Y =(X. * H)* exp(1i * w。* t);应该在循环之外。为什么?做出猜测,如果你错了,你可能会得到指导。
Also Y
is a 101x101 matrix. I guess you want it to be 101x1? You probably need to transform one of the vectors in the expression used to create Y
. Before you do, you should figure out the difference between '
and .'
(an important difference in this case).
Y也是101x101矩阵。我想你想要它是101x1?你可能需要转换用于创建Y的表达式中的一个向量。在你做之前,你应该找出'和'之间的区别。 (在这种情况下的一个重要区别)。
You are using subplot
, but only plotting one graph. If you want to graphs in the same plot, use hold on
. If you want to plots beside each other, remember to plot the second one too.
您正在使用子图,但只绘制一个图。如果要在同一图中绘制图形,请使用hold on。如果你想在彼此旁边绘图,记得也绘制第二个。
And why use w = pi;
, when you can just as well use pi
in the equations?
为什么使用w = pi;,当你可以在方程式中使用pi时?
#1
2
I won't do your entire homework, but I'll give you a few clues:
我不会做你的整个作业,但我会给你一些线索:
Skip the if t < 0
part, it doesn't work. For your exam, try to understand why. If you can't figure it out, come with your best guess and you might get an explanation =)
跳过if t <0部分,它不起作用。对于您的考试,请尝试了解原因。如果你无法弄明白,请提出你最好的猜测,你可能会得到一个解释=)
Try the following instead (no loops or if
s needed:
请尝试以下方法(无需循环或ifs:
x = exp((-3.*t)+(-1i*pi.*t)).*(t>0);
And the same for h
. Try to understand what .*(t<0)
does in this context.
h同样如此。试着理解。*(t <0)在这种情况下的作用。
This one: Y = (X.*H)*exp(1i*w.*t);
should be outside the loop. Why? Make a guess and you might get guidance if you're wrong.
这一个:Y =(X. * H)* exp(1i * w。* t);应该在循环之外。为什么?做出猜测,如果你错了,你可能会得到指导。
Also Y
is a 101x101 matrix. I guess you want it to be 101x1? You probably need to transform one of the vectors in the expression used to create Y
. Before you do, you should figure out the difference between '
and .'
(an important difference in this case).
Y也是101x101矩阵。我想你想要它是101x1?你可能需要转换用于创建Y的表达式中的一个向量。在你做之前,你应该找出'和'之间的区别。 (在这种情况下的一个重要区别)。
You are using subplot
, but only plotting one graph. If you want to graphs in the same plot, use hold on
. If you want to plots beside each other, remember to plot the second one too.
您正在使用子图,但只绘制一个图。如果要在同一图中绘制图形,请使用hold on。如果你想在彼此旁边绘图,记得也绘制第二个。
And why use w = pi;
, when you can just as well use pi
in the equations?
为什么使用w = pi;,当你可以在方程式中使用pi时?