Error creating array with evenly spaced elements

9 visualizaciones (últimos 30 días)
Luan Vuong
Luan Vuong el 23 de Sept. de 2024
Comentada: Luan Vuong el 24 de Sept. de 2024
I am trying to create an array with evenly spaced elements to serve as frequency values ​​before spectrum analysis.
The sampling rate is 1000sps and my dataset length is 20000 samples.
% Matlab online (Basic) 2024b
FSamp= 1000; %sampling frequency
ns= 20000; %num of sample
F= ((FSamp/2)*linspace(0,1,ns/2+1))'; %Frequency vector
% F= 0.05*round(F/0.05);
id= find(F==62.3)
The frequency vector should be an array of numbers increasing in increments of 0.05. However, when I check their actual values ​​(e.g. 62.2 and 62.3), the values ​​are 62.19999.... and 62.300000000000004, respectively.
This prevents me from doing the following steps (e.g. finding the amplitude corresponding to the frequency 62.3)
I tried rounding F to a multiple of 0.05, but nothing changed. Please explain and help me fix this array creation error
  1 comentario
Stephen23
Stephen23 el 23 de Sept. de 2024
"Please explain and help me fix this array creation error"
It is very simple: you generated two different values by using two different algorithms. Two different values are not equal.
This is an entirely expected behavior of binary floating point numbers:
This is worth reading as well:
Instead of incorrectly assuming exact equivalence of binary floating point numbers, compare the absolute difference against a tolerance:
tol = 1e-10; % you select this to suit your data
idx = abs(A-B)<tol;

Iniciar sesión para comentar.

Respuesta aceptada

Udit06
Udit06 el 23 de Sept. de 2024
Editada: Udit06 el 23 de Sept. de 2024
The issue that you are facing is due to the floating point error. As you also pointed out that the exact value is not 62.3 but 62.300000000000004, which is why the "find" function is giving an empty result. The reason for this behavior is mentioned in the following MathWorks documentation:
A workaround to resolve the issue that you are facing is to add a tolerance value while finding the value from the array as shown below:
% Define the target frequency and tolerance
target_frequency = 62.3;
tolerance = 1e-5; % Small tolerance for floating-point comparison
% Find the index of the frequency close to 62.3
id = find(abs(F - target_frequency) < tolerance);
I hope this helps.
  5 comentarios
Paul
Paul el 23 de Sept. de 2024
There is a new function isapprox that may be considered.
FSamp= 1000; %sampling frequency
ns= 20000; %num of sample
F= ((FSamp/2)*linspace(0,1,ns/2+1))'; %Frequency vector
% F= 0.05*round(F/0.05);
id= find(isapprox(F,62.3))
id = 1247
F(id)
ans = 62.3000
format long
F(id)
ans =
62.300000000000004
Luan Vuong
Luan Vuong el 24 de Sept. de 2024
The tolerance option of the function also increases its applicability. Thanks for suggesting a useful new function.

Iniciar sesión para comentar.

Más respuestas (2)

Shashi Kiran
Shashi Kiran el 23 de Sept. de 2024
I understand that you are encountering an issue with id = find(F == 62.3) not working as expected. This is due to floating-point precision.
Here is a way to handle this issue:
F = round(F,2); % Rounding the values in F to 2 decimal digits: Change accordingly
id = find(F == 62.3)
Refer to the following documentations for more details about the functions:
  1. round: https://www.mathworks.com/help/matlab/ref/double.round.html
  2. Floating point numbers: https://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html
Hope this solves your query.
  2 comentarios
Luan Vuong
Luan Vuong el 23 de Sept. de 2024
So simple, but it actually works. Many thanks.
I still don't understand why my rounding:
F= 0.01*round(F/0.01)
doesn't give the expected result even though they are the same. I tried it and it works with a simple array like rand(1,5). Maybe my method still has problems with floating point.
Shashi Kiran
Shashi Kiran el 23 de Sept. de 2024
Hey Luan,
The issue arises because multiplying by 0.01 after rounding F causes the number of decimal places to increase again, and the rounding operation is not being accounted for properly.
This can be illustrated as follows:

Iniciar sesión para comentar.


Voss
Voss el 23 de Sept. de 2024

Find the index of the element in F that is closest to 62.3:

[~,id] = min(abs(F - 62.3))
  1 comentario
Luan Vuong
Luan Vuong el 23 de Sept. de 2024
It's giving the expected result even if the function array contains errors. Thank you!

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by