Why is HR here giving me NaN as an answer

2 Ansichten (letzte 30 Tage)
Ibon Galiano Crespo
Ibon Galiano Crespo am 9 Jun. 2023
Kommentiert: Image Analyst am 13 Jun. 2023
Im doing a heart rate monitor and i have to calculate the heart rate in a matlab app but HR is giving me NaN values and I dont know how to fix it because the code seems correct and I have no errors in the code.
This is the code to calculate heart rate the formula is (60 / time between 2 pulses)
[~, locs]=findpeaks(fn,"MinPeakHeight",0.6,"MinPeakProminence",0.5);
med=[];
for n=1:length(locs)-1
med(n)=locs(n+1)-locs(n);
end
HeartRate=(60*app.Fs)/mean(med);
When I call HR later I get an error in the command window that says:
Warning: Error occurred while executing the listener callback for event DataAvailable defined for class daq.ni.Session:
Error using matlab.ui.control.internal.model.AbstractNumericComponent/set.Value (line 185)
'Value' must be a double scalar within the range of 'Limits'.
Error in appWIP/HRsaver (line 87)
app.HeartRateEditField.Value=HeartRate;
Does anyone know why it happens? Thanks for answering
  1 Kommentar
John D'Errico
John D'Errico am 9 Jun. 2023
LoL. If you really have no errors in your code, and it is perfect as written, then there would be no problem.
So yes, you do have an error in your code, in that your code is probably not robust to problematic data. We don't know why that is, since you have not posted your data for anyone to test.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Nathan Hardenberg
Nathan Hardenberg am 12 Jun. 2023
As @John D'Errico suggests, your data is most likely the problem here. You have to have at least two peaks in you data. Otherwise the result is NaN (see example below with one peak in the data).
fn = [0,1,0,0,0,0,0]; % data with one peak
[~, locs]=findpeaks(fn,"MinPeakHeight",0.6,"MinPeakProminence",0.5);
med=[];
for n=1:length(locs)-1 % loop does not get executed at all!
med(n)=locs(n+1)-locs(n);
end
HeartRate=(60)/mean(med)
HeartRate = NaN
Since "med" stays unchanged, the following gets executed and the result is for rather obvious reasons NaN.
mean([])
ans = NaN
Now it's on you to find out why your data does not work right. Maybe you calculate the mean on every frame of the app and only have the datapoints from 1/30 of a second (just a guess). A Buffer should resolve the problem in this case.
It is also a good idea to check the length of the "locs"-vector before calculating the heartrate. If it is smaller than 2 you should skip the calculation for example.
  2 Kommentare
Ibon Galiano Crespo
Ibon Galiano Crespo am 13 Jun. 2023
Yeah my problem was that the signal being sent had only one peak because one of my filters had the wrong limits, and since that wasn't causing errors because the filter was still valid it took a while until I found the solution. I also added this so it wouldn cause any error messages even if it had no output.
if ~isempty(med)
HeartRate=(60*app.Fs)/mean(med);
else
HeartRate = HeartRate;
end
Thanks for the response
Image Analyst
Image Analyst am 13 Jun. 2023
This was difficult to answer since you provided no data. You didn't even provide a screenshot of your data plotted. So what can people do?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Time Series Events finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by