Error creating array with evenly spaced elements

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 Comment

"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;

Sign in to comment.

 Accepted Answer

Udit06
Udit06 on 23 Sep 2024
Edited: Udit06 on 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 Comments

Thanks for an interesting approach. It not only works in this case, but also provides a future solution when I want to search an array with an error. It's clearly more general than finding an exact value.
"Thanks for an interesting approach"
This is not an "interesting approach", comparing the absolute difference against a tolerance is the reliable, robust, recommended approach.
The approach you accepted using ROUND is not recommend, for the reasons explained here:
I wish I found this sooner. It was explained very clearly:
https://www.mathworks.com/matlabcentral/answers/321709-strange-behaviour-of-matlab-find-command#comment_444258
I'm a newbie. Everything is new to me and all your recommended links really helped me learn a lot.
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
The tolerance option of the function also increases its applicability. Thanks for suggesting a useful new function.

Sign in to comment.

More Answers (2)

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 Comments

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.
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:

Sign in to comment.

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

[~,id] = min(abs(F - 62.3))

1 Comment

It's giving the expected result even if the function array contains errors. Thank you!

Sign in to comment.

Products

Release

R2024b

Asked:

on 23 Sep 2024

Commented:

on 24 Sep 2024

Community Treasure Hunt

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

Start Hunting!