Code

From Control Systems Technology Group
Jump to navigation Jump to search

Back to main page: PRE2015_3_Groep4

To the input: Input

To the output: Output

to the experiments: Experiments

Gathering data from the arduino

In the figure below, the arduino-code is shown for gathering data. See input for more information about the script and see experiments for the made trade-offs.

Workingvarfreq.JPG

Making sleepphase graph

short explanation of the matlab script where the sleepphase is calculated

Determining moment of waking up

From our measurements we got information about respiration during your sleep. Out of this information, we get a global line of of your sleepphase throughout the night. Out of these line, we have to determine what's the best moment to wake someone between a given interval. The best moment to wake someone is just after the REM-sleep. So this is the first or second nREM-sleep stage. (see literature, add reference).

In our algorithm we have developed two methods which can determine at which moment the app should wake you. The first method is looking at the slope of the line. During REM-sleep, the respiration frequency is at its highest. So we can detect the end of the REM-phase with a drop in frequency. Another method is looking at the current frequency relative to the mean frequency of the night. With this method, we can detect a 'good' moment, i.e. when the frequency is below the mean of the past night. But the mean is probably too low, so we have to increase the value with a small factor of the variation.

However, during the REM-sleep, the respiration frequency varies a lot (see literature, add reference), so we need to be sure that a decline, or a value below the mean, is really the end of the REM-sleep and not just a variation during the REM-sleep. This can be done by looking for two or three consecutive decreasing frames. (two/three frames is around 8/12 minutes).

Both methods are tested, before we choose the best one. But the both methods are also tested together. This would mean that it will be a good moment ov waking up when the line is declining and below the frequency threshold. The last option, both methods together, gave the best results. So, this method is implemented in our final script. See results for further explanation.

Type of alarm

  • Alarm1: We will make a distinction between two type of alarms. The first alarm is the alarm which will start before the out-and-outer wake up time. This alarm will build op in volume. The first option isn't only a stardart alarm. It also includes the lights.
  • Alarm2: The second alarm is the alarm which will start when the out-and-outer wake up time is reached, in the cases the person is still asleep. This alarm will cause a much more abrupt waking up. But the person have to wake up to be on time at his appointment.

Time

There won't be a time as output value anymore. (this was the case in the test phases) Now, during the night, the script will every minute run the Complete_script-file, which checked if there is a good moment to wake the person. If there is a good moment, this script will put the value of alarm on true. otherwise, the value stays false. If the value of alarm is true, the output script (func_output) will be start, which will play the sound and turn the light on.

Explanation MATLAB-scripts

For the processing of data and determining the ideal moment to wake up the user MATLAB is used. Three scripts were created. The first script, runscript.m, is used for determining when to call the other two scripts and contains all the alarm-clock settings. Every four minutes, the second script is called, based on its return value, the user is awakend or MATLAB waits another four minutes and tries again.

The second script calculates when it's time to wake up the user. It starts with importing recorded data and fixing bad samples, then the respiration frequency is calculated. Finally, the script checks whether it's time to wake up the user and returns this as a boolean value.

The third and last script contains the wake up cycle. From this script, a sound is played with increasing volume and the LED's on the Arduino are controlled.

runscript.m

Runscript.m uses the timer and clock functions to keep track of time passage. When the start delay has passed a while loop is started to check whether it's time to wake the user. If it is, the output function is called.

Clock returns a 1x6 matrix containing the current year, month, day, hour, minute and second, this is multiplied by a 6x1 matrix (times) containing the amount of seconds in each of the aforementioned time-units, to create a scalar time in seconds.

The wake up time, interval and delay are entered using this same format, with preset variables for the number of seconds before the start of this day and the next.

The timer works through two functions, tic and toc. tic starts or resets the timer, toc returns the current value. This timer is reset before calling the complete_script function. If the user needn't be awoken, the while loop restarts and complete_script is called again after 4 minutes have passed, evaluating toc every 5 seconds. if the alarm has not been set when the wake up interval has almost passed, the alarm is called anyway. This is done just before the wake up time, so you never wake up too late. Not even a second.

Then in the end OutputFunc is called, starting the wake up cycle.

Appendix

Matlab-scripts:

This script has to be runned; here, the user have to fill in the wake-up time and interval:

File:Runscript.m

This script is a function which will process the gathered data. This function will also calculate out of these data the sleepphase. Finally, there will be checked if there is a good moment to wake-up within the interval.

File:Complete script.m

This script is a function which will control the output. So the alarm will be build up in volume and the LED will be more glow more over the time.

File:Func output.m