Error creating array with evenly spaced elements

조회 수: 4 (최근 30일)
Luan Vuong
Luan Vuong 2024년 9월 23일
댓글: Luan Vuong 2024년 9월 24일
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
Stephen23
Stephen23 2024년 9월 23일
"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;

댓글을 달려면 로그인하십시오.

채택된 답변

Udit06
Udit06 2024년 9월 23일
편집: Udit06 2024년 9월 23일
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
Paul
Paul 2024년 9월 23일
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 2024년 9월 24일
The tolerance option of the function also increases its applicability. Thanks for suggesting a useful new function.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Shashi Kiran
Shashi Kiran 2024년 9월 23일
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
Luan Vuong
Luan Vuong 2024년 9월 23일
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 2024년 9월 23일
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:

댓글을 달려면 로그인하십시오.


Voss
Voss 2024년 9월 23일

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

[~,id] = min(abs(F - 62.3))
  댓글 수: 1
Luan Vuong
Luan Vuong 2024년 9월 23일
It's giving the expected result even if the function array contains errors. Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by