%% Extract time points from automatic program based on thesholding TFPlot 
% 
clear all, close all
%% Input Parameters
TargetFreq=10; % Target Frequency for tresholding
ThresholdLevel= 1; % threshold value
%BoundaryTime=0; % in sec. (e.g. if BoundaryTime=1, On time= 122 sec, off time= 144 sec, if will generate [123,143] window
TargetTFPlot='PSDY2'; % could be PSDY1, PSDY2, Coh

TimeResolution=1000; % in ms. for most of my TFP, the shifting window is 1 sec.
Int=1000; % in ms, minimal time to treat two OnOff sets as one. 
MinDur=1;% % in sec, minimal duration of a OnOff set to be valid. 

%% save parameters
save temp.mat;
a_EventExtactParameters=load('temp.mat');
delete temp.mat;
%% Main funciton
F=1000/TimeResolution;
load('Coherence_PSD_Profiles&Results.mat','FreqCoh',TargetTFPlot);
A=find(FreqCoh==TargetFreq);
B=sprintf('(%d,:)',A);
eval(['Data=',TargetTFPlot,B,';']);

TargetWindow=A_MotionDetect(Data,ThresholdLevel,Int,MinDur,F,1);
EventWindow=TargetWindow.MoveInt;
%EventWindow(:,1)=EventWindow(:,1)+BoundaryTime*1000/TimeResolution;
%EventWindow(:,2)=EventWindow(:,2)-BoundaryTime*1000/TimeResolution;

save('EventListTFPlot.mat','EventWindow','a_EventExtactParameters');
