Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can I determine the current sleep stage?

Specifically, I want to take certain actions when a user is in REM, or at least light sleep. Is there an API that will allow me to access this information?

 

The Sleep API appears to be based on the log, once they are awake, but I want the current stage.

 

Thanks,

Best Answer
25 REPLIES 25

I was looking at this too. I want to make a Lucid Dreaming app, where the watch gives a vibration pattern as you enter REM so you can hopefully realise and hopefully go lucid.

I didn't see a way to do this, but I've only just started developing! My plan is to try and see if I can look at the logs of data and work out what things are linked to REM sleep phase, HR being one of them I assume.

At the minute I'm just getting to grips with a simple watch face, then a meditation timer app and then I should have enough know how to tackle the Lucid Dreaming app.

Love to know what you plan to do or if you find out how to do identify REM sleep.

Good luck

Best Answer

Unfortunately, there is no way to do this now: you can only access via the Sleep logs with the Web API once the data has been synchronized.

Best Answer
0 Votes

"...there is no way to do this now..." - is this something that we could request? 

It would open the device to be used for Lucid Dreaming or other sleep related issues.

Best Answer
0 Votes

You can vote for feature requests. I guess this one is what you are looking for: https://community.fitbit.com/t5/Feature-Suggestions/Enable-Sleep-data-from-fitbit-Device-API/idi-p/2...

Best Answer
0 Votes

Much obliged, vote is in!! Keep up the good work!

Best Answer

It's my understanding that the sleep stages (etc) are estimated on the basis of sensor data that is directly available through the device API. For example, the Fitbit doesn't actually detect eye movements (hence REM), but hypothesises REM sleep based on heart rate, wrist movement, etc. If you can come up with similar heuristics, you'll then have real-time sleep (pseudo-)data on the device itself.

 

Alas, I've found that my Fitbit can't tell the difference between sleep and watching TV. Maybe I need to watch more exciting TV shows.

Peter McLennan
Gondwana Software
Best Answer

I haven't read it yet(need to get more fitbit app dev under my belt before tackling a bigger project), but this might be of help...

Sleep Monitoring Based on a Tri-Axial Accelerometer and a Pressure Sensor --> https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4883440/

 

edit - a quick scan and I found "...it has been shown that the heart rate variability (HRV) spectral parameters exhibit significant differences between the different sleep stages and, in particular, they seem to discriminate well between REM and non-REM sleep."

Best Answer

I would love to create an alarm that determines when I am in light sleep or rem (and within 30 minutes of the alarm time) and start a gentle buzz to wake me more naturally. 

Best Answer
0 Votes

I am willing to help if someone wants to start a collective effort (thinking more like a library that implements some sleep detection heuristics to be used in many ways).

Best Answer

@paperinik I'm up for that. I was gonna work on it anyway so why not collab.

 

I'm a seasoned dev, but hardly used javascript and a total noob on fitbit. I suggest a Github repo outside of https://github.com/Fitbit/ossapps until something is working and then do a PR as a module?

 

My plan was try and identify sleep patterns from raw data against the sleep periods that fitbit identify. If that didn't reveal an obvious pattern then I was gonna do some AI using python and Tensorflow by chucking loads of raw data and try and identify sleep periods that way. Maybe we could allow people to upload anonymised data and use AI against that, the more data the better!

 

Instead of re-inventing the wheel we can see if there are any current algorithms already.

 

Searching for "method for identifying sleep phases based on heart rate and accelerometer" showed this study which was "...funded in full by Fitbit Inc."

 

"Estimation of sleep stages in a healthy adult population from optical plethysmography and accelerometer signals" - http://iopscience.iop.org/article/10.1088/1361-6579/aa9047

extract....

"While no single feature is simply predictive of sleep stages, by visually examining the time course of individual features versus sleep stage, we can provide some physiological insights. Our interpretation of the most significant features is as follows. Movement features correlate with previously known insights from actigraphy. For example, an activity count feature which includes the magnitude and duration of movement during the 30 s epoch is easily interpreted as being correlated with wake. Periods of near-constant heart rate and low movement are associated with deep sleep. Periods with a high degree of short-term heart rate variability (e.g. as seen in the LF and HF spectral features) and relatively little movement are associated with REM. The advantage of machine learning algorithms such as the linear discriminant classifier is that it can pull together these different aspects of the data which would be hard to do by inspection."

 

Best Answer
0 Votes

The feature request to have a gentle alarm app for the fitbit tracker is older than 3 years. So, I guess it will not happen in the near future. 

 

Based on fitbit‘s help file the fitbit App will use 

- movement pattern and

- heart-rate pattern 

from last night to determine sleep phases. 

 

For the gentle wake app you „only“ have to track (let‘s say) 1 hour prior your alarm set the heart rate and movement sensor. 

 

 

If (((deepsleepphase== false)&& (currenttime>=(alarmtime-30min))) || (currenttime >=alarmtime)) then vibrate tracker. 

 

The difficulty is probably to determine whether the deepsleepphase is true or false. 

 

Here are more details how fitbit determines the sleep phase (copy & paste from here: https://help.fitbit.com/articles/en_US/Help_article/2163 😞

 

„... When you haven’t moved for about an hour, your tracker or watch assumes that you’re asleep.
Additional data—such as the length of time your movements are indicative of sleep behavior (such as rolling over, etc.)—
help confirm that you’re asleep. While you’re sleeping, your device tracks the beat-to-beat changes
in your heart rate, known as heart rate variability (HRV), which fluctuate as you transition between light sleep,
deep sleep, and REM sleep stages. When you sync your device in the morning,
we use your movement and heart rate patterns to estimate your sleep cycles from the previous night. ...“

 

 

 

 

 

 

 

Best Answer
0 Votes

I am thinking that another limitation that will affect this is that for now you cannot have background apps running.

So either this happens in a watchface or nothing. 

Best Answer
0 Votes

I did my thesis a few years ago about determining sleep stages based on HRV alone. It's a bummer that fitbit currently doesn't provide this information, however, there is a technological limitation to all this: frequency of ECG signals acquisition. The spectral parameters mentioned in some articles before refer to the low frequency and high frequency variability (ie, the heart rate changing faster or slower). This is hard to achieve with a device that measures heart rate every 1 to 3 seconds, ie 0.33Hz to 1Hz. Literature says that 250Hz (once every 0.004 seconds, somewhat continuous).

 

It's possible to assume a fair estimate of the sleep stage based on limited spectral parameters. Deep sleep if I recall correctly, has a unique pattern. But I suspect that accelerometer data is still the prevailing indicator.

 

I never delved with the Fitbit API (I only know - knew! - MATLAB) but if we can have ECG data at a reasonable frequency it's a starting point. I came to this forum looking for info on this because I wanted to process HRV data. Let me know when that GitHub platform is set up, I'd love to join in. 

Best Answer
0 Votes

Fitbit devices don't actually do ECG. They use reflected light from the wrist, so ECG-based research may not be directly relevant. Moreover, since apps are written in an interpreted language, getting anything done at 250Hz could be a problem.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

You are correct, I hadn't investigated it properly. It's something similar to PPG - the tracking of blood flow through the wrist. Still, the sampling frequency may be an issue - ECG signals are then processed to get the peak of the heart beat and the time difference between these peaks gives us the HRV. For this we need to capture all heart beats. I'm not sure this can be accomplished with a 1Hz sampling frequency - but I may be missing something in the sensor technology with respect to data acquisition. 

Best Answer
0 Votes

While the time of each heartbeat is probably known deep within the bowels of the watch, that info isn't made available through the API. You could submit a feature request (if there isn't one already), but I'd be surprised if it gets up any time soon.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Sorry for being lazy, but do you happen to know if Fitbit disclosed the algorithm (non-API related) used for sleep staging? From what I saw in the PurePulse description this is indeed a form of PPG - similar to the device they strap to your finger on the hospital to measure heart rate.

 

I confess that I'm excited about the possibility to draw upon Fitbit sensors to extract HRV data but it's as you say, even a feature request may not amount to anything. It may be that it simply isn't possible.

Best Answer
0 Votes

I know of no info on Fitbit's sleep algorithms, and I doubt that Fitbit will make direct access to HR sensor data available.

 

During 'activity' periods, devices can report HR every second. Since that's about once per beat, with a bit of cunning you could deduce approximate HR variability. Problems: some maths would be involved, activity period data may not be available in real-time to apps, and the device may apply some smoothing (eg, running average) to the values it reports.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Hey hey! Did you ever make any progress on accessing phases of sleep live?

Best Answer
0 Votes