Measuring chronic physiological stress with the Apple Watch and the Swift language

Tibo Bruneel
7 min readJan 26, 2021
Photo by Angus Gray on Unsplash

I am a computer science engineering student with no medical background. For my bachelor thesis, I did a research project on how to measure the stress levels of a person using the Apple Watch with the focus on the Swift language and the current possibilities of the Apple Watch. Since I was working with medical data, I had to do some medical research as well and I will mention the information I found with its sources. I do not have any further expertise in the medical field.

In this article I will cover the following subjects:

  • Stress and Heart Rate Variability
  • The Apple Watch accuracy and samples
  • Accessing Apple Watch records
  • Measuring the chronic physiological stress level with HealthKit
  • Conclusion on the current possibilities

Stress and Heart Rate Variability

Prior to measuring chronic physiological stress, it is important to have a good understanding of what stress actually is. The human autonomic nervous system (ANS) is divided into the sympathetic and parasympathetic nervous systems. The sympathetic division prepares the body to fight by accelerating bodily functions and is associated with stress. The parasympathetic division does the opposite and is involved in relaxation and recovery. These divisions function simultaneously in opposition to each other.

Stress is an increased activation level of the body where the sympathetic division dominates the ANS over the parasympathetic division. On the opposite side, we have recovery, where the parasympathetic division dominates over sympathetic division. - Source

Heart Rate Variability (HRV) is the physiological phenomenon of the variation in time between consecutive heartbeats, expressed in milliseconds. HRV has been a proven indicator of the autonomous nervous system activity, this has been confirmed by studies. Stress can be associated with a decrease in the HRV. Recovery can be associated with an increase in the HRV. With that knowledge, it proves that heart rate variability can be really useful for measuring the physiological stress level. - Source

The Apple Watch accuracy and samples

The accuracy of the collected data by the Apple Watch is really important and has to be a great medical representation, even more, if the data is needed to measure the stress level. In a research by the University of Zaragoza, Spain, the accuracy of the Apple Watch compared to the Polar H7 band showed very good results. And supports the potential use of the Apple Watch for stress monitoring based on HRV.

Photo by Daniel Korpai on Unsplash

The Apple Watch automatically records a few HRV samples every day. The amount of samples is an average of 4–5 samples each day. The timestamps of the samples are different each day and taken at random times. There is only one way of manually forcing the Apple Watch to record an HRV sample, this is through the Apple Watch Breathe app, where the user has to breathe deep in and out for a certain amount of time, once this Breathe or Mindful session is finished, it stores an HRV sample (most of the times). It is also possible to manually add samples through the Health app on a connected iOS device, but should not be added if the data is not confirmed by a high-level measuring device.

Accessing Apple Watch records

The Apple Watch stores the health records securely in the HealthKit store. The data from the Apple Watch is synchronized with the health store on the iOS device and is purged from the Apple Watch after 8–9 days. The historical data can all be accessed on the iOS device through HealthKit, on the Apple Watch only the locally available data, which is not enough long-term data to do useful measurements with. I wrote another article on how to develop an app using the HealthKit store and authorization.

HealthKit provides a central repository for health and fitness data on iPhone and Apple Watch. With the user’s permission, apps communicate with the HealthKit store to access and share this data. - Apple Documentation

The data can be read through queries and long-running queries, there are many queries fit for all kinds of use cases.

Photo by Oliur on Unsplash

Measuring the chronic physiological stress level with HealthKit

After research on how to measure the stress level, it was clear that there were only two options. I developed both these measurement types in Swift and will cover my findings on these in the conclusion. These measurements can not be combined into one measurement. - Source

Night measurement

The first type of measurement is the night measurement, where the user is in a resting and recovery phase. With all the data stored on the Apple Watch, developing this measurement can be done easily. First, the data of when the user was sleeping has to be retrieved, there is a specific data identifier in HealthKit called sleepAnalysis. When the periods where the user was asleep are known, all the Heart Rate Variability samples can be retrieved that were in the sleeping periods. The samples can further be used to create maximum, average and minimum baselines.

Morning measurement

The second type of measurement is the morning measurement. Since the Apple watch takes samples at random times, the measurement can not be done without a manually taken sample, through the Breathe app on the Watch. And this Breathe session has to be recorded when the user just wakes up when there are no stressors influencing the samples yet. The Breathe data can also be retrieved from HealthKit, there is a data identifier called mindfulSession where it is possible to retrieve the Breathe sessions recorded with the Apple Watch. Then it is possible to retrieve the Heart Rate Variability sample that was recorded at the same time as the Breathe session. And again, with historical data it is possible to create a baseline with all the morning measurements. But the morning measurement forces the user to take that Breathe session every morning, in the context of an app, it could be useful to send push notifications to remind the user.

Photo by Simon Daoudi on Unsplash

Defining the stress category

With these measurements, it is not really possible to just use a percentage as stress level, because it would just not be accurate and correct. So in my research, I decided to just use the following stress categories: Low, Medium and High level. Both measurements result in baselines based on the historical data, with these baselines it is possible to compare it to the data of a certain period. For example, the measurement of today compared to the average baseline of the past 120 days. And based on the difference it is possible to add a stress category to it.

Conclusion on the current possibilities

Night measurement

The night measurement pure on the few and randomly taken samples is not a great way to measure the stress level using the Apple Watch. Although it is possible to retrieve long-term and historical data, the amount of samples is just not enough. The samples taken depend too much on the sleep stage where the user is in, but can also have larger influences from the day before. It would be a great way of measuring the data if the Apple Watch took way more samples, the best possible scenario is where the Watch is just monitoring the full night.

Morning measurement

The morning measurement is at this moment still the best option, since the Watch most of the times stores an HRV sample after a Breathe session and the data can be requested pretty easily with the HealthKit query possibilities. But once again, the user has to take a Breathe session as many days possible to have accurate measurements.

Realtime stress measurements

With the little HRV data available at this point, it is not possible to measure acute stress levels at a point in the day. It is possible to force an HRV measurement at a random moment, but in that case, there is just no accurate historical data to compare it to. The only way it would be possible is through constant live monitoring, which the Apple Watch is not able to do (for now).

Photo by Anh Nguyen on Unsplash

Final conclusions

At this point in time, the measurements are being held back too much by the current possibilities and data recordings of the Apple Watch. This might change in the future, but until then, there are better options.

In case you are interested in HRV readings, I strongly advise the HRV4 Training App. In my opinion, they are the best option available right now and they are very open on the work they do. Reading Marco Altini’s (Founder of HRV4 Training) articles helped me a lot through my process of measuring the stress, allowing me to explore more on the technical research of my thesis, I strongly advice reading Marco’s great work as well.

--

--