-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSleepStageAlgorithm.cpp
More file actions
273 lines (228 loc) · 8.4 KB
/
SleepStageAlgorithm.cpp
File metadata and controls
273 lines (228 loc) · 8.4 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @file SleepStageAlgorithm.cpp
* @author clecoued <[email protected]>
* @version 1.0
*
*
* @section LICENSE
*
* Sleep Stage Algorithm
* Copyright (C) 2017 Aura Healthcare
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* @section DESCRIPTION
*
* refer to Overview.mkd to get a detailed description of the algorithm
*/
#include "SleepStageAlgorithm.hpp"
#include <boost/foreach.hpp>
#include <fftw3.h>
/**
* @brief process a batch of samples fetched on a specified interval [iIntervalStart, iIntervalEnd]
*
* @param[in] iIntervalStart The timestamp mesured at interval start
* @param[in] iIntervalEnd The timestamt mesured at interval end
* @param[in/out] ioCurrentSamples The current interval samples
*/
void SleepStageAlgorithm::processSamples(boost::posix_time::ptime iIntervalStart,
boost::posix_time::ptime iIntervalEnd,
std::vector<DataSamplePtr>& ioCurrentSamples)
{
removeExtremeValues(ioCurrentSamples);
std::vector<int> lResampledRRIntervals = resample(iIntervalStart,
iIntervalEnd,
RRIntervalSamplingFrequency,
ioCurrentSamples);
std::vector<float> lNormalizedRRIntervals = normalizeSignal(lResampledRRIntervals);
Features * lFeatures = extractFeatures(lNormalizedRRIntervals);
classifySleepState(lFeatures);
delete lFeatures;
}
/**
* @brief Filter extreme R-R interval values out of [MinRRIntervalValue, MaxRRIntervalValue]
*
* @param ioCurrentSamples The current interval samples
*/
void SleepStageAlgorithm::removeExtremeValues(std::vector<DataSamplePtr>& ioCurrentSamples)
{
int lCurrentRRInterval = 0;
std::vector<DataSamplePtr>::iterator it;
for(it = ioCurrentSamples.begin(); it != ioCurrentSamples.end(); it++)
{
lCurrentRRInterval = (*it)->getRrInterval();
if(lCurrentRRInterval < MinRRIntervalValue || lCurrentRRInterval > MaxRRIntervalValue)
{
ioCurrentSamples.erase(it);
it--;
}
}
}
/**
* @brief resampling the current interval samples to RRIntervalSamplingFrequency
*
* @param[in] iIntervalStart The timestamp mesured at interval start
* @param[in] iIntervalEnd The timestamt mesured at interval end
* @param[in] iSamplingFrequency The re-sampling frequency
* @param[in] iCurrentSamples The current interval samples
*
* @return the resampled data
*/
std::vector<int> SleepStageAlgorithm::resample(boost::posix_time::ptime iIntervalStart,
boost::posix_time::ptime iIntervalEnd,
float iSamplingFrequency,
const std::vector<DataSamplePtr>& iCurrentSamples)
{
float lSamplingStep = 1.f / iSamplingFrequency * 1000; //im ms
std::vector<int> oResampledRRInterval;
// do not resample interval with less than 2 data samples
if(iCurrentSamples.size() < 2)
{
return oResampledRRInterval;
}
boost::posix_time::time_duration lTd = iIntervalEnd - iIntervalStart;
int lNbOfSamples = lTd.total_milliseconds() * 1.0 / lSamplingStep;
std::vector<DataSamplePtr>::const_iterator it = iCurrentSamples.begin();
boost::posix_time::ptime t1 = (*it)->getTimestamp();
int i1 = std::floor( (t1 - iIntervalStart).total_milliseconds() * 1.f / lSamplingStep) + 1;
int rr1 = (*it)->getRrInterval();
for(int i = 0; i < i1; i++)
{
oResampledRRInterval.push_back(rr1);
}
boost::posix_time::ptime t2;
int i2 = 0, rr2 = 0;
int rr = 0;
boost::posix_time::ptime t;
it++;
for(; it != iCurrentSamples.end();it++)
{
t2 = (*it)->getTimestamp();
i2 = std::floor( (t2 - iIntervalStart).total_milliseconds() * 1.f / lSamplingStep);
rr2 = (*it)->getRrInterval();
float alpha = (rr2 - rr1) * 1.f /( (t2 - t1).total_milliseconds() );
for(int i = (i1 + 1); i <= i2; i++)
{
t = iIntervalStart + boost::posix_time::milliseconds(i * lSamplingStep);
rr = alpha * (t - t1).total_milliseconds() + rr1;
oResampledRRInterval.push_back(rr);
}
t1 = t2;
i1 = i2;
rr1 = rr2;
}
for(int i = i1; i < lNbOfSamples; i++)
{
oResampledRRInterval.push_back(rr1);
}
return oResampledRRInterval;
}
/**
* @brief normalize data - center values on 0
*
* @param[in] iResampledSamples The resampled data
*
* @return the normalized resampled data
*/
std::vector<float> SleepStageAlgorithm::normalizeSignal(const std::vector<int>& iResampledSamples)
{
double lRrAverage = 0;
BOOST_FOREACH(int lRr, iResampledSamples)
{
lRrAverage += lRr;
}
lRrAverage = lRrAverage / iResampledSamples.size();
std::vector<float> oNormalizedSamples;
// do not normalize signal on empty data interval
if(iResampledSamples.size() == 0)
{
return oNormalizedSamples;
}
oNormalizedSamples.reserve(iResampledSamples.size());
std::vector<int>::const_iterator it = iResampledSamples.begin();
for(;it != iResampledSamples.end(); it++)
{
oNormalizedSamples.push_back((*it) - lRrAverage);
}
return oNormalizedSamples;
}
/**
* @brief evaluate the sleep state based on previously extracted features
*
* @param iFeatures Textracted features
*/
Features* SleepStageAlgorithm::extractFeatures(const std::vector<float>& iNormalizedSamples)
{
// do not extract Features on empty data interval
if(iNormalizedSamples.size() == 0)
{
return new Features(0, 0);
}
// compute FFT
fftw_complex lSignal[FFTNumberOfSamples];
fftw_complex lResult[FFTNumberOfSamples];
fftw_plan lPlan = fftw_plan_dft_1d(FFTNumberOfSamples,
lSignal,
lResult,
FFTW_FORWARD,
FFTW_ESTIMATE);
// prepare input data
for(int i = 0; i < iNormalizedSamples.size(); i++)
{
// fill Real part
lSignal[i][0] = iNormalizedSamples.at(i);
// fill Imaginary part
lSignal[i][1] = 0;
}
for(int i = iNormalizedSamples.size(); i < FFTNumberOfSamples; i++)
{
// fill Real part
lSignal[i][0] = 0;
// fill Imaginary part
lSignal[i][1] = 0;
}
fftw_execute(lPlan);
// compute Low Freq Band Power and High Freq Band Power features
int lLowFreqMin = std::floor(Features::LowFrequencyBandMin * FFTNumberOfSamples / RRIntervalSamplingFrequency);
int lLowFreqMax = std::floor(Features::LowFrequencyBandMax * FFTNumberOfSamples / RRIntervalSamplingFrequency);
int lHighFreqMin = std::floor(Features::HighFrequencyBandMin * FFTNumberOfSamples / RRIntervalSamplingFrequency);
int lHighFreqMax = std::floor(Features::HighFrequencyBandMax * FFTNumberOfSamples / RRIntervalSamplingFrequency);
double lLowBandPower = 0.0;
double lHighBandPower = 0.0;
double lAmplitude = 0;
for(int i = lLowFreqMin + 1; i <= lLowFreqMax; i++ )
{
lAmplitude = std::sqrt(lResult[i][0]*lResult[i][0] + lResult[i][1]*lResult[i][1] );
lLowBandPower += lAmplitude;
}
for(int i = lHighFreqMin + 1; i <= lHighFreqMax; i++)
{
lAmplitude = std::sqrt(lResult[i][0]*lResult[i][0] + lResult[i][1]*lResult[i][1] );
lHighBandPower += lAmplitude;
}
double lTotalPower = lLowBandPower + lHighBandPower;
lLowBandPower = lLowBandPower / lTotalPower * 100; // in %
lHighBandPower = lHighBandPower / lTotalPower * 100; // in %
std::cout << lLowBandPower << " " << lHighBandPower << " " << lLowBandPower / lHighBandPower << std::endl;
fftw_destroy_plan(lPlan);
return new Features(lLowBandPower, lHighBandPower);
}
/**
* @brief evaluate the sleep state based on previously extracted features
*
* @param iFeatures Textracted features
*/
void SleepStageAlgorithm::classifySleepState(Features * iFeatures)
{
}