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

1 visualización (últimos 30 días)
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?

Respuesta aceptada

Star Strider
Star Strider el 20 de Sept. de 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 comentarios
Shae Morgan
Shae Morgan el 20 de Sept. de 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 el 20 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by