MATLAB 中的向量大小不匹配 [英] Vector size mismatch in MATLAB
问题描述
我一直在尝试为 LTI 系统写一个代码,其中响应是从输入 x(t)
和脉冲 h(t)
响应计算出来的.
I have been trying to write down a code for an LTI system where the response is calculated from an input x(t)
and impulse h(t)
response.
对于下面的代码部分:
y = conv(x,h).*steps;
ty = 0:steps:7;
plot(ty,y);
我收到以下错误消息:
错误使用plot
向量的长度必须相同.
Error using
plot
Vectors must be the same length.
我使用 ty = 0:steps:7;
因为 h(t)
被定义为 exp(-t/2).*((t>=2)-(t>=7))
(因为它扩展到 t=7
).
I'm using ty = 0:steps:7;
as h(t)
is defined as exp(-t/2).*((t>=2)-(t>=7))
(since it extends upto t=7
).
究竟是什么决定了ty
?
推荐答案
Convolution Using Anonymous Functions
执行此过程的一种方法是使用由 @()
指示的匿名函数/函数句柄,在这种情况下,t
保存输入参数.要创建截断信号,可以在 t
上实现条件语句.要使信号范围从 t=2
到 t=7
秒截断可以通过元素方式乘以 ((t>=2) &(t<=7))
.用于绘制最终结果的 t
向量必须具有一个时间范围,即卷积过程中使用的信号的时间长度之和.我相信 ty
是绘制输出的时间向量.在这种情况下,您必须确保 ty
与输出 y
的长度相同.
Convolution Using Anonymous Functions
One way to do this process is to use anonymous functions/functions handles which are indicated by the @()
that holds the input parameters in this case time, t
. To create truncated signals conditional statements on t
can be implemented. To have a signal ranging from t=2
to t=7
seconds truncation can be done by element-wise multiplying by ((t>=2) & (t<=7))
. The t
vector used to plot the final result must have a time range that is the sum of the lengths of time of the signals used in the convolution process. I believe ty
is the time vector to plot the output against. In this case you must ensure ty
has the same length as the output y
.
结果长度 = 系统响应长度 + 输入信号长度
在以下情况下:
x(t) → 长度 = 1s
h(t) → 长度 = 5s(2s 到 7s)
y(t) → 长度 = 1s + 5s = 6s(2s 到 8s)
In the case below:
x(t) → length = 1s
h(t) → length = 5s (2s to 7s)
y(t) → length = 1s + 5s = 6s (2s to 8s)
Step_Size = 0.1;
Start_Time = 0; End_Time = 7;
t = Start_Time: Step_Size: End_Time;
%Input into system x(t)%
x = @(t) 1.0.*(t >= 0 & t <= 1);
subplot(3,1,2); fplot(x);
title('x(t): Input into system');
xlabel('Time (s)'); ylabel('Amplitude');
xlim([0 10]);
ylim([0 1.1]);
%System impulse response h(t)%
h = @(t) exp(-t/2).*((t>=2) & (t<=7));
subplot(3,1,1); fplot(h);
title('h(t): System impulse response');
xlabel('Time (s)'); ylabel('Amplitude');
xlim([0 10]);
y = conv(x(t),h(t)).*Step_Size;
t = linspace(0,2*max(t),length(y));
subplot(3,1,3); plot(t,y);
title('y(t): Output/System Response');
xlabel('Time (s)'); ylabel('Amplitude');
这篇关于MATLAB 中的向量大小不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!