Excel Tables from EEG Data - Nearest peak value after event, global average values, local average values

5 views (last 30 days)
I am trying to get the nearest peak value after an event, global average values, and local average values. Below I have examples of the tables I am trying to get. I want each table in a different excel file and I can provide the subject numbers.
I have attached a zip folder with the files you should need.
%% Importing_and_Plotting_EEG_Example
close all; clear all; clc;
% look to 300 ms for peak after stimulation
% add folder with csv file to path
addpath 'C:\Users\chris\Documents\College\Sixth Year\DAGSI\Data\EMOTIV Recordings\Event Marker Try 4'
% change current directory to where this Matlab file is
cd 'C:\Users\chris\Documents\College\Sixth Year\DAGSI\Data\EMOTIV Recordings'
% stops Matlab from rounding numbers
format long;
% Importing csv data
T=readtable('Try 4.csv'); %This reads the csv data into a table named T
timestampOrig=T.Timestamp; %this makes timestampOrig equal to the original Timestamp
T.Timestamp=T.Timestamp-T.Timestamp(1); %This changes the official Timestamp column so it is 0 at the start of the recording
% Changes the sensor names from EEG_<sensor> to <sensor>
T.Properties.VariableNames(2:end)=extractAfter(T.Properties.VariableNames(2:end),'_');
%T5=head(T, 5) %shows first 5 rows of table T
Int=readtable('Event Marker Try 4_EPOCX_126368_2021.06.04T15.42.31.04.00_intervalMarker.csv');
EvtMar=Int.latency;
EvtMarLab=Int.type;
EvtLab=readtable('Try 4 Event Labels.xlsx');
table(EvtLab)
I think I can use findpeaks, but I'm having trouble getting the peaks narrowed down and getting the data organized correctly.
%% Time Between Peaks
%Assigns the peak values and indices of peak values to variables
%<sensor>pk and <sensor>index, respectively
[AF3pk, AF3index]=findpeaks(T.AF3, T.Timestamp,'MinPeakProminence',100); [F7pk, F7index]=findpeaks(T.F7, T.Timestamp,'MinPeakProminence',100);
[F3pk, F3index]=findpeaks(T.F3, T.Timestamp,'MinPeakProminence',100); [FC5pk, FC5index]=findpeaks(T.FC5, T.Timestamp,'MinPeakProminence',100);
[T7pk, T7index]=findpeaks(T.T7, T.Timestamp,'MinPeakProminence',100); [P7pk, P7index]=findpeaks(T.P7, T.Timestamp,'MinPeakProminence',100);
[O1pk, O1index]=findpeaks(T.O1, T.Timestamp,'MinPeakProminence',100); [O2pk, O2index]=findpeaks(T.O2, T.Timestamp,'MinPeakProminence',100);
[P8pk, P8index]=findpeaks(T.P8, T.Timestamp,'MinPeakProminence',100); [T8pk, T8index]=findpeaks(T.T8, T.Timestamp,'MinPeakProminence',100);
[FC6pk, FC6index]=findpeaks(T.FC6, T.Timestamp,'MinPeakProminence',100); [F4pk, F4index]=findpeaks(T.F4, T.Timestamp,'MinPeakProminence',100);
[F8pk, F8index]=findpeaks(T.F8, T.Timestamp,'MinPeakProminence',100); [AF4pk, AF4index]=findpeaks(T.AF4, T.Timestamp,'MinPeakProminence',100);
I also want it to say either N/A or 0 if there is no peak in that section. I am trying to automate this prcocess since I have 15 subjects and about 20 minutes of data each (So, over 137,200 data points each).
Any help would be appreciated!
Note: I posted one of these as a different question (https://www.mathworks.com/matlabcentral/answers/1574933-finding-peaks-after-event-in-a-time-frame) but I didn't describe what I was trying to get well enough.

Answers (1)

Christina Diersing
Christina Diersing on 10 Nov 2021
I found a way to get the event-to-peak table (though the setup is different than I originally planned, I like the new setup better). I got a fair amount of the code from the other question I mentioned earlier (https://www.mathworks.com/matlabcentral/answers/1574933-finding-peaks-after-event-in-a-time-frame). Here is the code I used for my table:
%% Event_to_First_Peak_Try_4
close all; clear all; clc;
% add folder with csv file to path
addpath 'C:\Users\chris\Documents\College\Sixth Year\DAGSI\Data\EMOTIV Recordings\Event Marker Try 4'
% change current directory to where this Matlab file is
cd 'C:\Users\chris\Documents\College\Sixth Year\DAGSI\Data\EMOTIV Recordings'
format long;
% Importing csv data
T=readtable('Try 4.csv'); %This reads the csv data into a table named T
timestampOrig=T.Timestamp; %this makes timestampOrig equal to the original Timestamp
T.Timestamp=T.Timestamp-T.Timestamp(1); %This changes the official Timestamp column so it is 0 at the start of the recording
% Changes the sensor names from EEG_<sensor> to <sensor>
T.Properties.VariableNames(2:end)=extractAfter(T.Properties.VariableNames(2:end),'_');
%T5=head(T, 5) %shows first 5 rows of table T
Int=readtable('Event Marker Try 4_EPOCX_126368_2021.06.04T15.42.31.04.00_intervalMarker.csv');
EventTime=Int.latency; %Imports the latency from the file
EventMarkerLabel=Int.type; %Imports the event type/label from the file
% EvtLab=readtable('Try 4 Event Labels.xlsx'); %Import event markers and abbreviations used for plot
% %EvtLab{2,2} = {'Blink Eye'};
% table(EvtLab) %View legend (in table form) of abbreviations used in the plot
% Preallocate variable
Event_Labels = cell(length(EventTime),1);
%Event Labels
Event_Labels=EventMarkerLabel;
%% Plots all together
%creates plot
p=plot(T.Timestamp,T{:,2:width(T)});
xline(EventTime,'--',Event_Labels);%creates vertical lines marking events
% Preallocate variables
Firstpeakafterevent = zeros([width(T)-1, size(EventTime)]);
timeoffirstpeakafterevent = zeros([width(T)-1, size(EventTime)]);
timefromeventtofirstpeak = zeros([width(T)-1, size(EventTime)]);
% Plot
for i=2:width(T) %for the EEG electrode data
%find peaks and locations/times of peaks
[pks,locs] = findpeaks(T{:,i},T.Timestamp, 'MinPeakProminence', 100);
for ii = 1:length(EventTime) %for the experiment length
index = find(locs>EventTime(ii),1); %finds the location(peak) after the event, but closest to the event
if ~isempty(index)
Firstpeakafterevent(i-1,ii,:) = pks(index); %stores peak values in array Firstpeakafterevent
timeoffirstpeakafterevent(i-1,ii,:) = locs(index); %Stores peak times in the array timeoffirstpeakafterevent
timefromeventtofirstpeak(i-1,ii,:) = locs(index)-EventTime(ii);%time from event to first peak
end
end
x = timeoffirstpeakafterevent(i-1,:);
y = Firstpeakafterevent(i-1,:);
x(x==0) = [];
y(y==0) = [];
hold on
scatter(x,y,'v','filled','MarkerFaceColor',p(i-1).Color) %Creates scatterplot for the first peak after each event
end
grid on;
legend(p,T.Properties.VariableNames(2:width(T))) %view legend
%% Data Table
%transposing data so it fits table
timefromeventtofirstpeak = timefromeventtofirstpeak';
timeoffirstpeakafterevent = timeoffirstpeakafterevent';
Firstpeakafterevent = Firstpeakafterevent';
%Filename to print table to
filename='Try 4 Event to Peak Times and Amplitudes.xlsx';
%Create table t with event labels, latency, time of peak, and peak
%amplitudes
t=table(Event_Labels, timefromeventtofirstpeak(:,1), timeoffirstpeakafterevent(:,1), Firstpeakafterevent(:,1),Firstpeakafterevent(:,2),Firstpeakafterevent(:,3),Firstpeakafterevent(:,4),Firstpeakafterevent(:,5),Firstpeakafterevent(:,6),Firstpeakafterevent(:,7),Firstpeakafterevent(:,8),Firstpeakafterevent(:,9),Firstpeakafterevent(:,10),Firstpeakafterevent(:,11),Firstpeakafterevent(:,12),Firstpeakafterevent(:,13),Firstpeakafterevent(:,14));
%Label columns
t.Properties.VariableNames = {'Event','Latency after Event','Time of Peak','AF3 Pk','F7 Pk','F3 Pk','FC5 Pk','T7 Pk','P7 Pk','O1 Pk','O2 Pk','P8 Pk','T8 Pk','FC6 Pk','F4 Pk','F8 Pk','AF4 Pk'};
%Write to excel file
writetable(t,filename,'Sheet',1,'Range','A1');
I am still working on a code for the number of peaks in a section.

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!