MATLAB Answers

Why are there minor differences in the creation of two time series for signal generation?

1 view (last 30 days)
Shae Morgan
Shae Morgan on 20 Sep 2018
Commented: Star Strider on 20 Sep 2018
I'm making a 1000 Hz tone for presentation. MATLAB recommends creating the time series (t) this way:
amp=.01; %amplitude
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
but I've always done it this way (all other variables the same):
When I check whether or not they are equal, the value is 0 - they are not.
I ran a for-loop to investigate, and it appears that they are only unequal on a few points.
for i=1:length(t1)
What's the calculation difference between these two methods?


Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 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.


Shae Morgan
Shae Morgan on 20 Sep 2018
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 on 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!