Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
tutorial:sleep [2018/10/21 14:58]
42.49.180.224 [Extracting the brain state and events from continuous sleep EEG]
tutorial:sleep [2018/04/20 15:35] (current)
robert [Summary and suggested further reading]
Line 113: Line 113:
 </​code>​ </​code>​
  
-====== ​Extracting ​the brain state and events from continuous sleep EEG  ​======+===== Detect wake periods using EMG & EOG ===== 
 + 
 +To identify periods of wake (including brief arousals), non-REM and REM states during sleep we use all available modalities of a polysomnogram. We can use information about movement reflected in the level of EMG activity. In order to detect movement artifacts we filter the EMG channel in the 20 to 45 Hz range and apply a Hilbert envelope around the signal and smooth it. 
 + 
 +<code matlab>​ 
 +  cfg                              = []; 
 +  cfg.continuous ​                  = '​yes';​ 
 +  cfg.artfctdef.muscle.interactive = '​yes';​ 
 + 
 +  % channel selection, cutoff and padding 
 +  cfg.artfctdef.muscle.channel ​    = '​EMG';​ 
 +  cfg.artfctdef.muscle.cutoff ​     = 4; % z-value at which to threshold (default = 4) 
 +  cfg.artfctdef.muscle.trlpadding ​ = 0; 
 + 
 +  % algorithmic parameters 
 +  cfg.artfctdef.muscle.bpfilter ​   = '​yes';​ 
 +  cfg.artfctdef.muscle.bpfreq ​     = [20 45];% typicall [110 140] but sampling rate is too low for that 
 +  cfg.artfctdef.muscle.bpfiltord ​  = 4; 
 +  cfg.artfctdef.muscle.bpfilttype ​ = '​but';​ 
 +  cfg.artfctdef.muscle.hilbert ​    = '​yes';​ 
 +  cfg.artfctdef.muscle.boxcar ​     = 0.2; 
 +</​code>​ 
 + 
 +<​note>​ 
 +Please note that due to the reduced sample rate of 128 Hz we cannot use the typical frequency range to better detect EMG, however this should suffice for our case. 
 +</​note>​ 
 + 
 +We now use  **[[reference:​ft_artifact_muscle|ft_artifact_muscle]]** for automated artifact rejection according to some threshold (for more information on the active mode of this function see also the [[http://​www.fieldtriptoolbox.org/​tutorial/​automatic_artifact_rejection?​s[]=ft&​s[]=artifact&​s[]=muscle#​jump_artifact_detection | automatic artifact rejection tutorial]]). 
 + 
 +We want to be very cautious with excluding and exclude more than we must since EMG artifacts point towards movement or wake periods have happened probably some seconds earlier and later. 
 +   
 +<code matlab>​ 
 +  % conservative rejection intervals around EMG events 
 +  cfg.artfctdef.muscle.pretim ​ = 10; % pre-artifact rejection-interval in seconds 
 +  cfg.artfctdef.muscle.psttim ​ = 10; % post-artifact rejection-interval in seconds 
 + 
 +  % keep a copy for the exercise 
 +  cfg_muscle_epoched = cfg; 
 + 
 +  % feedback, explore the right threshold for all data (one trial, th=4 z-values) 
 +  cfg = ft_artifact_muscle(cfg,​ data_continuous);​ 
 + 
 +  % make a copy of the samples where the EMG artifacts start and end, this is needed further down 
 +  EMG_detected = cfg.artfctdef.muscle.artifact;​ 
 +</​code>​ 
 + 
 +=== Exercise 1 === 
 +<note exercise>​ 
 +Explore and find the best cutoff in z-values (i.e. standard deviations) to exclude most of the artifacts for this subject! 
 +Typically EMG higher than 100 microVolts are best excluded. Note that we exclude the data on the whole data length, i.e. as one big trial.  
 +</​note>​ 
 + 
 +{{ :​tutorial:​sleep:​ft_artifact_muscle_gui.png?​800 |}} 
 +**//Figure 3: Interactive figure of ft_artifact_muscle. The left panel shows the z-score of the processed data. Suprathreshold data points are marked in red. The lower right panel shows one trial, which in our case is the same as on the left panel, as we inspect the data all in one.//** 
 +  
 +  
 +  
 +We can now look at the detected "​artifacts"​ using **[[reference:​ft_databrowser|ft_databrowser]]** 
 + 
 +<code matlab>​ 
 +  cfg_art_browse ​            = cfg; 
 +  cfg_art_browse.continuous ​ = '​yes';​ 
 +  cfg_art_browse.viewmode ​   = '​vertical';​ 
 +  cfg_art_browse.blocksize ​  = 30*60; % view the data in 10-minute blocks 
 +  ft_databrowser(cfg_art_browse,​ data_continuous);​ 
 +</​code>​ 
 + 
 +For the epoched data we can do a similar EMG artifact identification as above: 
 + 
 +<code matlab>​ 
 +cfg_muscle_epoched.continuous ​                  = '​no';​ 
 +cfg_muscle_epoched.artfctdef.muscle.interactive = '​yes';​ 
 +cfg_muscle_epoched = ft_artifact_muscle(cfg_muscle_epoched,​ data_epoched);​ 
 +</​code>​ 
 + 
 +=== Exercise 2 === 
 +<note exercise>​ 
 + ​Compare the artifact begin and end samples when detected in the continuous data and when detected in the epoched data. What do you notice? 
 +</​note>​ 
 + 
 +Another indicator of wake periods (or REM sleep) is eye movement. In the EOG we observe different types of eye movements (blinks, normal, slow, and rapid eye movements). Therefore, we will next detect periods with increased EOG: 
 +<code matlab>​ 
 +  cfg = []; 
 +  cfg.continuous ​               = '​yes';​ 
 +  cfg.artfctdef.eog.interactive = '​yes';​ 
 + 
 +  % channel selection, cutoff and padding 
 +  cfg.artfctdef.eog.channel ​    = '​EOG';​ 
 +  cfg.artfctdef.eog.cutoff ​     = 2.5; % z-value at which to threshold (default = 4) 
 +  cfg.artfctdef.eog.trlpadding ​ = 0; 
 +  cfg.artfctdef.eog.boxcar ​     = 10; 
 + 
 +  % conservative rejection intervals around EOG events 
 +  cfg.artfctdef.eog.pretim ​     = 10; % pre-artifact rejection-interval in seconds 
 +  cfg.artfctdef.eog.psttim ​     = 10; % post-artifact rejection-interval in seconds 
 +</​code>​ 
 + 
 +=== Exercise 3 === 
 +<note exercise>​ 
 +Explore the right threshold for detecting all EOG artifacts. Again, the data is displayed as one single long trial. 
 +</​note>​ 
 + 
 +<code matlab>​ 
 +  cfg = ft_artifact_eog(cfg,​ data_continuous);​ 
 + 
 +  % make a copy of the samples where the EOG artifacts start and end, this is needed further down 
 +  EOG_detected = cfg.artfctdef.eog.artifact;​ 
 +</​code>​ 
 + 
 +To exclude these epochs with artifacts ​from analysis we use **[[reference:​ft_rejectartifact|ft_rejectartifact]]** and replace all the artifactual data points in all channels of the continuous sleep data with zeros by using the option cfg.artfctdef.reject '​value'​ and setting it to 0. 
 + 
 +<code matlab>​ 
 +  % replace the artifactual segments with zero 
 +  cfg []; 
 +  cfg.artfctdef.muscle.artifact ​EMG_detected;​ 
 +  cfg.artfctdef.eog.artifact ​   ​EOG_detected;​ 
 +  cfg.artfctdef.reject ​         ​'​value';​ 
 +  cfg.artfctdef.value ​          0; 
 +  data_continuous_clean ​ft_rejectartifact(cfg,​ data_continuous);​ 
 +  data_epoched_clean ​   = ft_rejectartifact(cfg,​ data_epoched);​ 
 +</​code>​ 
 + 
 +Let us view the data in 2-hour blocks again after excluding the parts with EMG or EOG artifacts:​ 
 + 
 +<code matlab>​ 
 +  cfg             = []; 
 +  cfg.continuous ​ = '​yes';​ 
 +  cfg.viewmode ​   = '​vertical';​ 
 +  cfg.blocksize ​  = 60*60*2; % view the data in blocks 
 +  ft_databrowser(cfg,​ data_continuous_clean);​ 
 +</​code>​ 
 +{{:​tutorial:​sleep_figure_emg_and_eog_artifact_databrowser.png?​800|}} 
 ===== Estimating frequency-represtation over sleep ===== ===== Estimating frequency-represtation over sleep =====
  
Line 822: Line 954:
  
   * [[faq:​how_can_i_read_all_channels_from_an_edf_file_that_contains_multiple_sampling_rates|How can I read all channels from an EDF file that contains multiple sampling rates?]]   * [[faq:​how_can_i_read_all_channels_from_an_edf_file_that_contains_multiple_sampling_rates|How can I read all channels from an EDF file that contains multiple sampling rates?]]
 +
 +