/
index.html
246 lines (155 loc) · 11.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html lang="en-us">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Enable responsiveness on mobile devices-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>
Fig's Blog
</title>
<!-- CSS -->
<link rel="stylesheet" href="/public/css/poole.css">
<link rel="stylesheet" href="/public/css/syntax.css">
<link rel="stylesheet" href="/public/css/lanyon.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Serif:400,400italic,700%7CPT+Sans:400">
<!-- Icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-precomposed.png">
<link rel="shortcut icon" href="/public/favicon.ico">
<!-- RSS -->
<link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
</head>
<body>
<!-- Target for toggling the sidebar `.sidebar-checkbox` is for regular
styles, `#sidebar-checkbox` for behavior. -->
<input type="checkbox" class="sidebar-checkbox" id="sidebar-checkbox">
<!-- Toggleable sidebar -->
<div class="sidebar" id="sidebar">
<div class="sidebar-item">
<p>A blog to post about personal projects and interesting things I come across</p>
</div>
<nav class="sidebar-nav">
<a class="sidebar-nav-item active" href="/">Home</a>
<a class="sidebar-nav-item" href="/about/">About</a>
<a class="sidebar-nav-item" href="/tag/project">Projects</a>
<a class="sidebar-nav-item" href="http://github.com/alexfig">GitHub</a>
<!-- <span class="sidebar-nav-item">Currently v</span> -->
</nav>
<div class="sidebar-item">
<p>
© 2015. All rights reserved.
</p>
</div>
</div>
<!-- Wrap is the content to shift when toggling the sidebar. We wrap the
content to avoid any CSS collisions with our real content. -->
<div class="wrap">
<div class="masthead">
<div class="container">
<h3 class="masthead-title">
<a href="/" title="Home">Fig's Blog</a>
<small></small>
</h3>
</div>
</div>
<div class="container content">
<div class="posts">
<div class="post">
<h1 class="post-title">
<a href="/2015/01/10/testing-sleep-apnea/">
Testing for Sleep Apnea
</a>
</h1>
<span class="post-date">10 Jan 2015</span>
<script src="/public/audiojs/audio.min.js"></script>
<script>
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
</script>
<p>I've had problems with daytime sleepiness since I was a teenager. I used to attribute it to having a poor sleep schedule and just being a heavy sleeper. However I began to realize that even when I slept for 8+ hours, I would still get sleepy in the middle of the day. At the time, I was exercising regularly and had a fairly healthy diet so I couldn't think of a reason why I would get so tired. A couple years ago I woke up in the middle of the night, almost choking and struggling to breathe. I was only half awake so I didn't realize what was happening at first, but as soon as I did I just started breathing normally again like nothing happened and went back to sleep. This happened a few times more times afterwards, but then stopped occurring so I didn't think much of it. </p>
<p>Recently though, I stumbled upon the wiki page for a sleep disorder called <a href="http://en.wikipedia.org/wiki/Sleep_apnea">sleep apnea</a>, which is characterized by difficulty breathing during sleep. As soon as I read the description, it hit me that this might have been what I was experiencing in the past. I scheduled an appointment with a physician, but since it's not for a couple week, I wanted to see if I could build something to monitor my breathing while I slept. I had a couple microcontrollers collecting dust, so this gave me a reason to put one to use.</p>
<h2>Breath Monitor</h2>
<p>First I needed to find a sensor I could use to monitor my breathing. I tried searching for sensors that could measure airflow, but all I could find were either expensive, medical grade equipment or devices I wouldn't be able to use while I slept. Fortunately, I stumbled upon a <a href="http://forum.arduino.cc/index.php?topic=157802.msg1335958#msg1335958">forum post</a> describing how you can use a temperature sensor to monitor your breath. The poster had success with a DS18B20 digital thermometer so I just went with that. I ended up buying the waterproof version out of convenience, since it came prewired and I didn't have the materials to wire one myself. </p>
<p>Next I had to think of something that could hold the sensor in front of my nose/mouth while I slept. My initial idea was to just poke the sensor through a dust mask and wear that. While looking for dust masks to buy, I found some that had exhalation valves and figured they would probably be a better choice since the valve can be used to direct air over the sensor. I decided on <a href="http://www.amazon.com/3M-Particulate-Respirator-8233-N100/dp/B00OTZETMC/">this mask</a> because it looked the most comfortable and I needed to be able to sleep while wearing one. </p>
<h2>Putting It Together</h2>
<p>After receiving everything, I first wanted to test the sensor to make sure it was sensitive enough to detect whether I was inhaling or exhaling. The DS18B20 is a digital sensor and communicates using the 1-Wire protocol, so reading its output isn't as simple as it would be with an analog sensor. Luckily there are libraries that implement everything needed to use the sensor on an Arduino board, making things a lot easier. A tutorial that explains everything can be found <a href="http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html">here</a></p>
<p>To test the sensor, I continuously printed its output to the serial port. Initially it wasn't sensitive enough to detect my breath, however this was because of the waterproof casing. After removing the casing it was able to monitor my breath fairly accurately. When I inhaled, the temperature would drop, and when I exhaled, it would increase, even when my breath was shallow. </p>
<p>Finally, I had to figure out how to attach the sensor to the mask. After a bit of trail and error, the solution I came up with was to just twist tie it to the inside of the valve housing, as seen below. This forces the valve (the yellow flap) to stay open, which make it easier to breathe and lets air flow over the sensor when inhaling. If you allow the valve to close, then it won't cool down quick enough to detect when you inhale. </p>
<p><strong>Sensor attachment</strong>
<img src="/public/sleepdata/sensor.jpg" alt="Sensor Attachment"></p>
<p><strong>Putting everything together</strong>
<img src="/public/sleepdata/final.jpg" alt="Final Product" title="Final Product"></p>
<p><br></p>
<h2>Collecting and Analyzing the Data</h2>
<p>To collect the data I had my laptop log everything the Arduino printed to the serial port. I had the Arduino print out two columns of data, the time elapsed and the temperature. Here is the main loop of my code:</p>
<div class="highlight"><pre><code class="language-text" data-lang="text">void loop(void)
{
time = millis();
sensors.requestTemperatures();
Serial.print(time);
Serial.print(",");
Serial.println(sensors.getTempC(thermometer));
}
</code></pre></div>
<p>To analyze the data I used Octave. A plot of the raw data, as shown below, doesn't really tell us anything. We have to convert the data to something more useful. According to wikipedia, a common metric for determining an apnea is the time interval between breaths. An interval of 10 seconds or greater can be considered an apnea. </p>
<p><strong>Plot of raw data</strong>
<img src="/public/sleepdata/rawdata.jpg" alt="Raw Data Plot" title="Plot of raw data"></p>
<p>Measuring the interval between breaths is relatively simple since the output of the sensor is free of noise (i.e., temperature only increases when exhaling and decreases when inhaling). We can iterate through the temperature data and if the temperature is increasing we know were exhaling and if it decreasing, were inhaling. This most likely wouldn't work with an analog sensor. I wrote a function that iterates through the data, calculating the time between every inhale.</p>
<div class="highlight"><pre><code class="language-text" data-lang="text">beginInhaleTime = data(1,1);
currentTemp = data(1,2);
inhaling = true;
for i=2:size(data,1),
if(inhaling)
if(data(i,2) < currentTemp)
currentTemp = data(i,2);
elseif(data(i,2) > currentTemp)
inhaling = false;
currentTemp = data(i,2);
end;
else
if(data(i,2) > currentTemp)
currentTemp = data(i,2);
elseif(data(i,2) < currentTemp)
inhaling = true;
breathIntervals = [breathIntervals; data(i,1) - beginInhaleTime ];
timeOfBreath = [timeOfBreath; beginInhaleTime];
beginInhaleTime = data(i,1);
end;
end;
end;
</code></pre></div>
<p>With the breath intervals calculated, I then plotted the data along with a couple more metrics to help determine the quality of my sleep.</p>
<p><strong>Breath interval plot</strong>
<img src="/public/sleepdata/plot.JPG" alt="Breath Interval Plot" title="Final Plot"></p>
<p>Looking at the plot, there seems to be evidence that I have sleep apnea. According to wikipedia, 5 - 14.9 events per hour of sleep is considered mild. The data shows I had around 6.8 so that's on the low end, however there is a cluster of 15 apneas at the 5-hour mark. </p>
<h2>Further Analysis</h2>
<p>To gain some more insight on how I was breathing and confirm that the monitor was accurate, I placed a small mic under the mask to record myself while I slept. Using the plot I found the time at which one of the longer apneas occurred and listened to the audio to see if I was actually having trouble breathing. Here's what it sounded like:</p>
<p><audio src="/public/sleepdata/apnea1.mp3" preload="auto" /></p>
<p>It starts off with a normal breath and then you can hear me start struggling a bit. At around the 20 second mark I basically stop breathing for almost 40 seconds, before moving around and then finally exhaling. So it's looking like I may have sleep apnea. I'll have to wait until I get a sleep study done to get a real diagnosis though. Hopefully it's nothing too serious.</p>
</div>
</div>
<div class="pagination">
<span class="pagination-item older">Older</span>
<span class="pagination-item newer">Newer</span>
</div>
</div>
</div>
<label for="sidebar-checkbox" class="sidebar-toggle"></label>
<script>
(function(document) {
var toggle = document.querySelector('.sidebar-toggle');
var sidebar = document.querySelector('#sidebar');
var checkbox = document.querySelector('#sidebar-checkbox');
document.addEventListener('click', function(e) {
var target = e.target;
if(!checkbox.checked ||
sidebar.contains(target) ||
(target === checkbox || target === toggle)) return;
checkbox.checked = false;
}, false);
})(document);
</script>
</body>
</html>