Finding Peaks after Event (in a time frame)
Show older comments
I'm trying to find peaks of a dataset that occur after an event. I've attached a figure to help me explain. I am trying to find the local maximum (red arrows for examples) right after each event (marked by the dotted lines). I think I can use findpeaks for this, but I'm not sure how to set the timeframe. I want to have the following data recorded: [Event label, event time, peak height, time from event to first peak, time of peak (should be the index of the peak)], and I need the electrode label recorded.
The files I actually want to use this on are much larger (like the picture I attached), but even the zip file of one of the datasets was too big to attach, so I'm using this dataset as an example.
%% Importing_and_Plotting_EEG_Example
close all; clear all; clc;
% look to 300 ms for peak after stimulation
% 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)
If possible, I'd like to use a for loop, similar to the one shown below, so I automate the process of finding the values for all 14 electrodes in certain sections, such as in EO, EC, CE, OM.
for i=2:width(T) %1 plot for each sensor
%finds the peaks for each sensor. The peak is currently defined as as
%point that is at least 100 uV above the values directly to the left
%and right of the peak value; this also plots the peaks
findpeaks(T{:,i},T.Timestamp, 'MinPeakProminence', 100)
xline(EvtMar,'--',{'EO','BE','BE','BE','EC','BE','CE','CE','BE','OM','OM'});
%creates ylabel as the sensor names, in bold font
ylabel(T.Properties.VariableNames(i), 'FontWeight', 'bold')
end %ends for loop
I have this section of code that I used to find the time between peaks, but it isn't quite what I'm trying to do.
%% 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',mnpkprom); [F7pk, F7index]=findpeaks(T.F7, T.Timestamp,'MinPeakProminence',mnpkprom);
[F3pk, F3index]=findpeaks(T.F3, T.Timestamp,'MinPeakProminence',mnpkprom); [FC5pk, FC5index]=findpeaks(T.FC5, T.Timestamp,'MinPeakProminence',mnpkprom);
[T7pk, T7index]=findpeaks(T.T7, T.Timestamp,'MinPeakProminence',mnpkprom); [P7pk, P7index]=findpeaks(T.P7, T.Timestamp,'MinPeakProminence',mnpkprom);
[O1pk, O1index]=findpeaks(T.O1, T.Timestamp,'MinPeakProminence',mnpkprom); [O2pk, O2index]=findpeaks(T.O2, T.Timestamp,'MinPeakProminence',mnpkprom);
[P8pk, P8index]=findpeaks(T.P8, T.Timestamp,'MinPeakProminence',mnpkprom); [T8pk, T8index]=findpeaks(T.T8, T.Timestamp,'MinPeakProminence',mnpkprom);
[FC6pk, FC6index]=findpeaks(T.FC6, T.Timestamp,'MinPeakProminence',mnpkprom); [F4pk, F4index]=findpeaks(T.F4, T.Timestamp,'MinPeakProminence',mnpkprom);
[F8pk, F8index]=findpeaks(T.F8, T.Timestamp,'MinPeakProminence',mnpkprom); [AF4pk, AF4index]=findpeaks(T.AF4, T.Timestamp,'MinPeakProminence',mnpkprom);
%creates tables of <sensor>Indices and <sensor> Peak Values - different
%table for each sensor
AF3tab=table(AF3index, AF3pk, 'VariableNames', {'AF3 Index (s)', 'AF3 Peak (uV)'});
F7tab=table(F7index, F7pk, 'VariableNames', {'F7 Index (s)', 'F7 Peak (uV)'});
F3tab=table(F3index, F3pk, 'VariableNames', {'F3 Index (s)', 'F3 Peak (uV)'});
FC5tab=table(FC5index, FC5pk, 'VariableNames', {'FC5 Index (s)', 'FC5 Peak (uV)'});
T7tab=table(T7index, T7pk, 'VariableNames', {'T7 Index (s)', 'T7 Peak (uV)'});
P7tab=table(P7index, P7pk, 'VariableNames', {'P7 Index (s)', 'P7 Peak (uV)'});
O1tab=table(O1index, O1pk, 'VariableNames', {'O1 Index (s)', 'O1 Peak (uV)'});
O2tab=table(O2index, O2pk, 'VariableNames', {'O2 Index (s)', 'O2 Peak (uV)'});
P8tab=table(P8index, P8pk, 'VariableNames', {'P8 Index (s)', 'P8 Peak (uV)'});
T8tab=table(T8index, T8pk, 'VariableNames', {'T8 Index (s)', 'T8 Peak (uV)'});
FC6tab=table(FC6index, FC6pk, 'VariableNames', {'FC6 Index (s)', 'FC6 Peak (uV)'});
F4tab=table(F4index, F4pk, 'VariableNames', {'F4 Index (s)', 'F4 Peak (uV)'});
F8tab=table(F8index, F8pk, 'VariableNames', {'F8 Index (s)', 'F8 Peak (uV)'});
AF4tab=table(AF4index, AF4pk, 'VariableNames', {'AF4 Index (s)', 'AF4 Peak (uV)'});
pkpm="Minimum peak prominence is " + mnpkprom;
%Exports peak data to excel document - single sheet (Name: Try 4 Peaks
%and Indices)
filename = 'Try 4 Peaks and Indices.xlsx';
writematrix(pkpm, filename,'Sheet',1,'Range','A1');
writetable(AF3tab,filename,'Sheet',1,'Range','A2');
writetable(F7tab, filename, 'Sheet',1, 'Range', 'C2');
writetable(F3tab, filename, 'Sheet',1, 'Range', 'E2');
writetable(FC5tab, filename, 'Sheet',1, 'Range', 'G2');
writetable(T7tab, filename, 'Sheet',1, 'Range', 'I2');
writetable(P7tab, filename, 'Sheet',1, 'Range', 'K2');
writetable(O1tab, filename, 'Sheet',1, 'Range', 'M2');
writetable(O2tab, filename, 'Sheet',1, 'Range', 'O2');
writetable(P8tab, filename, 'Sheet',1, 'Range', 'Q2');
writetable(T8tab, filename, 'Sheet',1, 'Range', 'S2');
writetable(FC6tab, filename, 'Sheet',1, 'Range', 'U2');
writetable(F4tab, filename, 'Sheet',1, 'Range', 'W2');
writetable(F8tab, filename, 'Sheet',1, 'Range', 'Y2');
writetable(AF4tab, filename, 'Sheet',1, 'Range', 'AA2');
I also attached the full matlab code I am using in the zip file, along with the files you need to run the code.
Thank You!
EDIT/ADDED:
Below is an image of what I'm trying to get. I want the data in an excel file as a table. I will add the subject # every time, but I want to automate everything else in the table, since I have 15 datasets of about 20 mins length each to go through.

Accepted Answer
More Answers (0)
Categories
Find more on EEG/MEG/ECoG in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!