Error creating array with evenly spaced elements
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
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;
Akzeptierte Antwort
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
am 23 Sep. 2024
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))
F(id)
format long
F(id)
Weitere Antworten (2)
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:
- round: https://www.mathworks.com/help/matlab/ref/double.round.html
- Floating point numbers: https://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html
Hope this solves your query.
2 Kommentare
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:
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))
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!