Why are there minor differences in the creation of two time series for signal generation?
1 次查看(过去 30 天)
显示 更早的评论
I'm making a 1000 Hz tone for presentation. MATLAB recommends creating the time series (t) this way:
amp=.01; %amplitude
freq=1000
fs=24414; %sample frequency
T=1/fs; %sample period
D=5; %duration in seconds
d=D*fs %array of sample points = length of the signal
t1=(0:d-1)*T;
Y_MATLAB=amp*sin(2*pi*freq*t)
but I've always done it this way (all other variables the same):
t21=0:T:(D-T);
Y_MYWAY=amp*sin(2*pi*freq*t)
When I check whether or not they are equal, the value is 0 - they are not.
isequal(t1,t2)
I ran a for-loop to investigate, and it appears that they are only unequal on a few points.
for i=1:length(t1)
isequal(t1(i),t2(i))
end
What's the calculation difference between these two methods?
0 个评论
采纳的回答
Star Strider
2018-9-20
It is most likely due to the way the colon (link) operator works, and to floating-point approximation error. See: Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link) for an extended discussion.
2 个评论
Star Strider
2018-9-20
Correct. They simply perform what is essentially the same operation (using colon) with different initial, step, and end values.
The differences are negligible:
t1 = (0:d-1)*T;
t2 = 0:T:(D-T);
dif_t1_t2 = t1 - t2;
t1_t2_stats = [mean(dif_t1_t2), std(dif_t1_t2)]
t1_t2_stats =
6.53710085344128e-17 1.90828707721311e-16
I prefer to use the linspace (link) function to create vectors (unless I want integer values), since it produces much more reliable results. The difference between it and colon is that linspace produces a vector with the desired length, and colon produces a vector with the desired step.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!