Authored by Olivier David

Add an automatic threshold detection to ImaGIN_StimDetect

... ... @@ -100,6 +100,10 @@ for i0=1:size(Filename,1)
Data=D(bipole(1,:),:,:)-D(bipole(2,:),:,:);
%in case monopolar is kept on some electrodes
Data(find(bipole(1,:)==bipole(2,:)),:,:)=D(bipole(1,find(bipole(1,:)==bipole(2,:))),:,:);
if strcmp(Name{end},'ecg')
Data(end,:)=D(bipole(1,end),:,:);
end
... ...
... ... @@ -44,6 +44,9 @@ switch Job
try
D.OffsetEnd=P.OffsetEnd;
end
try
D.FileOut=P.FileOut;
end
ImaGIN_CropEpoch(D);
case{'Manual'}
... ... @@ -185,6 +188,10 @@ function ImaGIN_CropEpoch(D)
end
OffsetEndName=OffsetEnd;
OffsetEnd=ceil(OffsetEnd*D.fsample);
try
FileOut=D.FileOut;
end
n=0;
DD=D;
... ... @@ -238,7 +245,12 @@ function ImaGIN_CropEpoch(D)
%Save as a newfile
Prefix=[EventRefName '_' sprintf('%d-%d',round(OffsetStartName),round(OffsetEndName))];
ntrials=length(EventRef);
Dnew = clone(D, [Prefix '_' fname(D)], [D.nchannels OffsetStart+OffsetEnd+1, ntrials]);
try
newname=FileOut;
Dnew=clone(D,newname, [D.nchannels OffsetStart+OffsetEnd+1, ntrials]);
catch
Dnew = clone(D, [Prefix '_' fname(D)], [D.nchannels OffsetStart+OffsetEnd+1, ntrials]);
end
for i1=1:ntrials
if (EventRef(i1)-OffsetStart>=1) && (EventRef(i1)-OffsetStart<=DD.nsamples) && (EventRef(i1)+OffsetEnd>=1) && (EventRef(i1)+OffsetEnd<=DD.nsamples)
n=n+1;
... ... @@ -248,7 +260,8 @@ function ImaGIN_CropEpoch(D)
d = D(:, Index, 1);
Dnew(:, :, i1) = d;
Dnew = events(Dnew, i1, select_events(D.events,[Time(1)/D.fsample Time(2)/D.fsample]));
Dnew = timeonset(Dnew, Time(1)./D.fsample+D.timeonset);
% Dnew = timeonset(Dnew, Time(1)./D.fsample+D.timeonset);
Dnew = timeonset(Dnew, -OffsetStart/D.fsample);
if isfield(DD,'spike')
for i2=1:length(DD.spike.timings)
tmp=find(DD.spike.timings{i2}>=time(EventRef(i1))-OffsetStart/D.fsample&DD.spike.timings{i2}<=time(EventRef(i1))+OffsetEnd/D.fsample);
... ...
... ... @@ -141,7 +141,8 @@ for c=1:length(KeepEvent) % Navigate all stim events
noteName = char(strrep(noteName,numZ(1), num2str(str2double(numZ(1)))));
noteName = char(strrep(noteName,numZ(2), num2str(str2double(numZ(2)))));
end
noteName = strrep(noteName,'.0',''); noteName = strrep(noteName,'.','');
% noteName = strrep(noteName,'.0','');
% noteName = strrep(noteName,'.',''); %OD for EXCITATOR
%% check if stim electr numbers are concatenated without space or -
numbr = regexp(noteName,'\d*','Match');
... ... @@ -393,7 +394,10 @@ for c=1:length(KeepEvent) % Navigate all stim events
% stimFq = round(StimulationFreqU);
% else
stimStep = stimTime(2:end) - stimTime(1:end-1);
stimFq = round(median(stimStep)); % median frequency within event : %OD corrected to median
stimFq = 1/median(stimStep); % median frequency within event : %OD corrected to median
if stimFq>0.95
stimFq=round(stimFq);
end
% end
% --------------------------------------------------
... ... @@ -402,8 +406,10 @@ for c=1:length(KeepEvent) % Navigate all stim events
FileName = strcat(stimTimeOut,'_1.mat');
stimHz = regexp(FileName,rxp2,'match');
strFq = strcat(num2str(stimFq),'Hz');
if ~strcmp(stimHz,strFq) && ~strcmp(strFq,'0Hz')
FileName = char(strrep(FileName,stimHz,strFq));
if stimFq>=1
if ~strcmp(stimHz,strFq) && ~strcmp(strFq,'0Hz')
FileName = char(strrep(FileName,stimHz,strFq));
end
end
% Check if the file exists i.e repeated stim
if exist(fullfile(DirOut, FileName), 'file') ~= 2
... ...
... ... @@ -71,6 +71,9 @@ if isempty(Start)
Start=1;
else
Start=min(indsample(D,Start));
if isnan(Start)
Start=1;
end
end
if isempty(End)
End=nsamples(D);
... ... @@ -126,8 +129,18 @@ if 1==1
% [tmp1,tmp2]=max(d);
% Index=find(d>tmp1/4);
Index=find(d>2);
Index=Index(find(Index>ceil(Stim/2)+1&Index<length(d)-ceil(Stim/2)-1));
Th=2;
ok=1;
while ok
EstimatedStim=(length(d)-ceil(Stim))./ceil(Stim);
Index=find(d>Th);
Index=Index(find(Index>ceil(Stim/2)+1&Index<length(d)-ceil(Stim/2)-1));
if length(Index)/5>EstimatedStim
Th=Th+1;
else
ok=0;
end
end
if ~isempty(Index)
IndexClust=zeros(size(Index));
IndexClust(1)=1;
... ...
... ... @@ -49,14 +49,14 @@ catch
S.D = D;
end
D = spm_eeg_load(D);
try
FileOut=S.FileOut;
catch
FileOut=[S.pre D.fname];
FileOut=fullfile(D.path,[S.pre D.fname]);
end
D = spm_eeg_load(D);
%-Get parameters
%--------------------------------------------------------------------------
... ...