Error creating array with evenly spaced elements

9 Ansichten (letzte 30 Tage)
Luan Vuong
Luan Vuong am 23 Sep. 2024
Kommentiert: Luan Vuong am 24 Sep. 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 Kommentar
Stephen23
Stephen23 am 23 Sep. 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;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Udit06
Udit06 am 23 Sep. 2024
Bearbeitet: Udit06 am 23 Sep. 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 Kommentare
Paul
Paul am 23 Sep. 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 am 24 Sep. 2024
The tolerance option of the function also increases its applicability. Thanks for suggesting a useful new function.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Shashi Kiran
Shashi Kiran am 23 Sep. 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 Kommentare
Luan Vuong
Luan Vuong am 23 Sep. 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 am 23 Sep. 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:

Melden Sie sich an, um zu kommentieren.


Voss
Voss am 23 Sep. 2024

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

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by