Nov 9, 2015
Updated the code with correct license.
|
|
|
2 |
* Copyright (c) 2015 University of Lübeck |
|
3 |
* |
|
4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
5 |
* of this software and associated documentation files (the "Software"), to deal |
|
6 |
* in the Software without restriction, including without limitation the rights |
|
7 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
8 |
* copies of the Software, and to permit persons to whom the Software is |
|
9 |
* furnished to do so, subject to the following conditions: |
|
10 |
* |
|
11 |
* The above copyright notice and this permission notice shall be included in |
|
12 |
* all copies or substantial portions of the Software. |
|
13 |
* |
|
14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
20 |
* THE SOFTWARE. |
|
21 |
* |
|
22 |
* AUTHORS: Michael Schellenberger Costa: mschellenbergercosta@gmail.com |
|
23 |
* |
|
24 |
* Based on: A thalamocortical neural mass model of the EEG during NREM sleep and its response |
|
25 |
* to auditory stimulation. |
|
26 |
* M Schellenberger Costa, A Weigenand, H-VV Ngo, L Marshall, J Born, T Martinetz, |
|
27 |
* JC Claussen. |
Sep 13, 2016
Major cleanup and moderinization
|
|
|
203 |
/* Check if stimulation should start */ |
|
204 |
switch (mode) { |
|
205 |
|
|
206 |
/* No stimulation */ |
|
207 |
default: |
|
208 |
break; |
|
209 |
|
|
210 |
/* Semi-periodic stimulation */ |
|
211 |
case 1: |
|
212 |
/* Check if stimulation time is reached */ |
|
213 |
if(time == time_to_stimuli) { |
|
214 |
/* Switch stimulation on */ |
|
215 |
stimulation_started = true; |
|
216 |
Thalamus->set_input(strength); |
|
217 |
|
|
218 |
/* Add marker for the first stimuli in the event */ |
|
219 |
if(count_stimuli == 1) { |
|
220 |
marker_stimulation.push_back(time - onset_correction); |
|
221 |
} |
|
222 |
|
|
223 |
/* Check if multiple stimuli should be applied */ |
|
224 |
if (count_stimuli < number_of_stimuli) { |
|
225 |
/* Update the timer with respect to time between stimuli */ |
|
226 |
time_to_stimuli += time_between_stimuli; |
|
227 |
count_stimuli++; |
|
228 |
} |
|
229 |
/* After last stimulus in event update the timer with respect to (random) ISI*/ |
|
230 |
else { |
|
231 |
time_to_stimuli += (ISI_range==0)? ISI : Uniform_Distribution(); |
|
232 |
|
|
233 |
/* Reset the stimulus counter for next stimulation event */ |
|
234 |
count_stimuli = 1; |
|
235 |
} |
|
236 |
} |
|
237 |
break; |
|
238 |
|
|
239 |
/* Phase dependent stimulation */ |
|
240 |
case 2: |
|
241 |
/* Search for threshold */ |
|
242 |
if(!stimulation_started && !minimum_found && !threshold_crossed && time>onset_correction && !stimulation_paused) { |
|
243 |
if(Cortex->Vp[0]<=threshold) { |
|
244 |
threshold_crossed = true; |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
/* Search for minimum */ |
|
249 |
if(threshold_crossed) { |
|
250 |
if(Cortex->Vp[0]>Vp_old) { |
|
251 |
threshold_crossed = false; |
|
252 |
minimum_found = true; |
|
253 |
Vp_old = 0; |
|
254 |
} else { |
|
255 |
Vp_old = Cortex->Vp[0]; |
|
256 |
} |
|
257 |
} |
|
258 |
|
|
259 |
/* Wait until the stimulation should start */ |
|
260 |
if(minimum_found) { |
|
261 |
/* Start stimulation after time_to_stimuli has passed */ |
|
262 |
if(count_to_start==time_to_stimuli + (count_stimuli-1) * time_between_stimuli) { |
|
263 |
stimulation_started = true; |
|
264 |
Thalamus->set_input(strength); |
|
265 |
|
|
266 |
/* Add marker for the first stimuli in the event */ |
|
267 |
if(count_stimuli == 1) { |
|
268 |
marker_stimulation.push_back(time - onset_correction); |
|
269 |
} |
|
270 |
|
|
271 |
/* Check if multiple stimuli should be applied */ |
|
272 |
if (count_stimuli < number_of_stimuli) { |
|
273 |
/* Update the number of stimuli */ |
|
274 |
count_stimuli++; |
|
275 |
} else { |
|
276 |
/* After last stimulus in event pause the stimulation */ |
|
277 |
minimum_found = false; |
|
278 |
stimulation_paused = true; |
|
279 |
count_to_start = 0; |
|
280 |
|
|
281 |
/* Reset the stimulus counter for next stimulation event */ |
|
282 |
count_stimuli = 1; |
|
283 |
} |
|
284 |
} |
|
285 |
/* Update counter */ |
|
286 |
count_to_start++; |
|
287 |
} |
|
288 |
break; |
|
289 |
} |
|
290 |
|
|
291 |
/* Actual stimulation protocols */ |
|
292 |
if(stimulation_started) { |
|
293 |
/* Wait to switch the stimulation off */ |
|
294 |
if(count_duration==duration) { |
|
295 |
stimulation_started = false; |
|
296 |
burst_started = true; |
|
297 |
count_duration = 0; |
|
298 |
count_bursts = 0; |
|
299 |
Thalamus->set_input(0.0); |
|
300 |
} |
|
301 |
|
|
302 |
count_duration++; |
|
303 |
count_bursts++; |
|
304 |
|
|
305 |
/* Switch stimulation on and off wrt burst parameters */ |
|
306 |
if(burst_enabled) { |
|
307 |
if(burst_started) { |
|
308 |
if(count_bursts%burst_length==0) { |
|
309 |
count_bursts = 0; |
|
310 |
burst_started = false; |
|
311 |
Thalamus->set_input(0.0); |
|
312 |
} |
|
313 |
} else { |
|
314 |
if(count_bursts%burst_ISI==0) { |
|
315 |
count_bursts = 0; |
|
316 |
burst_started = true; |
|
317 |
Thalamus->set_input(strength); |
|
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
/* Wait if there is a pause between stimulation events */ |
|
324 |
if(stimulation_paused) { |
|
325 |
if(count_pause == ISI) { |
|
326 |
stimulation_paused = false; |
|
327 |
count_pause = 0; |
|
328 |
} |
|
329 |
count_pause++; |
|
330 |
} |