I am trying to create a single vector from EEG sleep data at 256Hz that lists each sleep stage and event as they occur in chronological order. The goal is to be able to process sleep data as they occur in their individual stages, but by removing times when the individual is awake (so wake and arousal events need to be coded to be removed from analyses).
My problem is that my EEG program gives me events and stages in an overlapping type of format. For example, my readout of scored events lists Wake as starting at 0 and lasting 3270000000 (in microseconds). Then stage n1 might occur next and last for 3 second epochs (30000000). In the middle of this time period, we might have a microarousal that occurs at a very particular moment for a certain period of time and it will be listed after this sleep stage and then the next sleep stage or event will be listed.
Example of data in csv file: enter image description here You can see that the date and start time occurs in order and that the start time for sleep stages (marked by the hypnogram type) all start at 0 and the relative start time increases incrementally in relation to the duration. The duration for all sleep stages are in 30s increments (in microseconds). However, if we look at in the picture N2 lasts 360000000 (or 360ms/ or 12, 30s epochs). the relative time is noted that N2 starts at 20040000000 and N3 starts at 20400000000, exactly 360000000 µs later. the problem is in the arousals. They occur during N2 sleep with their own start times and durations.
So, I don't know how to effectivly insert all the time points chronologically into a single vector when there are overlapping events. Can anyone help me, please?
% Input CSV file
infile="C:\Users\jerom\OneDrive\Desktop\PSG_Scoring\101_C_Scoring.csv";
% Read the CSv file
[~,~,table]=xlsread(infile);
start_musec=cell2mat(table(2:end,5));
duration_musec=cell2mat(table(2:end,6));
events=table(2:end,10);
% Define event times course
event_name={'Wake' 'Lights' 'N1' 'N2' 'N3' 'REM' 'Arousal'};
sfreq_Hz=256;
tmax_samp=ceil(sfreq_Hz*(start_musec(end)+duration_musec(end))/1e6);
event_vectors=[];
event_vectors.time_sec=((1:tmax_samp)-1)/sfreq_Hz;
time_musec=(event_vectors.time_sec)*1e6;
for evt=1:numel(event_name)
event_vectors.(event_name{evt})=zeros(1,tmax_samp);
for line=1:numel(start_musec)
if contains(events(line),event_name{evt})
is_in_current_event=(time_musec >= start_musec(line)) & (time_musec <= start_musec(line)+duration_musec(line));
event_vectors.(event_name{evt})(is_in_current_event)=1;
end
end
end
i was able to convert the time points to frequencies, but I was expecting to be able to connect this into data points that could be concatenated into a single vector, but I end up with a table.