Why is HR here giving me NaN as an answer

조회 수: 2 (최근 30일)
Ibon Galiano Crespo
Ibon Galiano Crespo 2023년 6월 9일
댓글: Image Analyst 2023년 6월 13일
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
John D'Errico
John D'Errico 2023년 6월 9일
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.

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

채택된 답변

Nathan Hardenberg
Nathan Hardenberg 2023년 6월 12일
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
Ibon Galiano Crespo
Ibon Galiano Crespo 2023년 6월 13일
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 2023년 6월 13일
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:

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by