Why are there minor differences in the creation of two time series for signal generation?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shae Morgan
am 20 Sep. 2018
Kommentiert: Star Strider
am 20 Sep. 2018
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 Kommentare
Akzeptierte Antwort
Star Strider
am 20 Sep. 2018
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 Kommentare
Star Strider
am 20 Sep. 2018
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Multirate Signal Processing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!