MATLAB Answers

Separate inhale and exhale duration

5 views (last 30 days)
Majd AG
Majd AG on 12 Apr 2020
Answered: Peter Perkins on 14 Apr 2020
Hello all,
using NI myRIO I obtained an array of intersection points between a signal and a threshold, every two values represent the rise and fall of an inhale and exhale, repectively.
I want to subtract each two values and store them in new arrays, inhale time, exhale time and breath time.
Now i know there's a problem in the loops I made but i don't know how to fix them, please help me. I'm getting the same values of duration, maybe the j is not updating
the attached picture is the signal where the values were obtained from.
breath=[903, 1979, 2313, 3550, 3870, 4947, 5232, 6517, 6867, 7818, 8181, 9512];
count=length(breath)/2 ; %count peaks (inhales and exhales)
%Find duration of inhale & exhale
for k=1:count/2 %durations of inhale
for j=1:4:length(breath)-4
INduration(k)= breath(j+1)-breath(j);
for n=1:count/2 %durations of exhale
for j=3:4:length(breath)-4
EXduration(n)= breath(j+1)-breath(j);
for r=1:count/2 %durations of whole breath
for j=1:4:length(breath)-4
BRduration(r)= breath(j+3)-breath(j);


Image Analyst
Image Analyst on 12 Apr 2020
Set a breakpoint on the lines inside the for j loop, and see if the program stops there and if indeed j is changing? I think you'll see it does change
Majd AG
Majd AG on 12 Apr 2020
Thank you for answering, I tried displaying the j inside the first loop and it alternates between two values 1&5 three times, what does that mean?

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 14 Apr 2020
You don't need any loops.
Your picture doesn't seem to match your description, but if each sret of four values corresponds to inhale start, inhale stop, exhale start, exhale stop, then do this:
>> breath = seconds([903, 1979, 2313, 3550, 3870, 4947, 5232, 6517, 6867, 7818, 8181, 9512])
breath =
1×12 duration array
903 sec 1979 sec 2313 sec 3550 sec 3870 sec 4947 sec 5232 sec 6517 sec 6867 sec 7818 sec 8181 sec 9512 sec
>> inhale = reshape(breath([1 2 5 6 9 10]),[2 3])
inhale =
2×3 duration array
903 sec 3870 sec 6867 sec
1979 sec 4947 sec 7818 sec
>> inhaleDuration = diff(inhale)
inhaleDuration =
1×3 duration array
1076 sec 1077 sec 951 sec
and similarly for the others.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by