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?

채택된 답변

Star Strider
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
Shae Morgan
Shae Morgan 2018년 9월 20일
Ok, so both ways are acceptable and (theoretically) performing similar operations? I just want to make sure my signal isn't going to be any different depending on the method I choose.
Star Strider
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 CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by