

inhib = [18, 20, 23, 24, 25,31,33,37, 38, 39 41,42,43, 44, 47, 58, 60, 61, 62, 63, 77,79, 82, 88, 92, 95, 100, 102, 105, 108, 113];
loc = [40:61];
sz = 'TJ049sz2';

%'ISOZ' =inhibitory seizure onset zone
%'ESOZ' = excitatory seizure onset zone
%'INOZ' = inhibitory not seizure onset zone
%'ENOZ' = excitatory not seizure onset zone
%loc = [41 42 43 44 47 58 60 61];
Fs= 32556;
spikephase = {};
freqFilt = [20:30];

for cc = loc
    %patNum = 17;
    disp(cc)
    longest = 41.9;

    %loc = find([DBfinal.patient{:}] == 17);
    lvf = cell2mat(DBfinal.T_LVF(1,cc));
    addon = longest - diff(lvf);
    
    
    chan = DBfinal.chan_num(1,cc);
    raster = cell2mat(DBfinal.Raster(1,cc));
    SOZ = cell2mat(DBfinal.SOZ_code);
    good_unit = cell2mat(DBfinal.long_unit(1,cc));
    

   load(strcat('./TJ049sz2-LVFclips/TJ049_sz2_14:14:4-14:23:4__', num2str(cell2mat(chan)), '.mat'));

    
    data = data(1,Fs*lvf(1):Fs*lvf(2));
    clear eeg
   % A = figure;
    %plot(1/Fs:1/Fs:length(data)/Fs,data); hold on
    raster = raster(1,10000*lvf(1):10000*lvf(2));
    
     %figure; plot(1/Fs:1/Fs:length(data)/Fs,data);hold on; plot(1/10000:1/10000:length(raster)/10000,raster*10000);

    pos = find(raster);
    pos = round(pos*Fs/10000);

    %process data
    freqHFO = [80 600];
    
    FIRfilt = fir1(500,[freqFilt(1)/(Fs/2) freqFilt(2)/(Fs/2)], 'bandpass');       
    data = filtfilt(FIRfilt, 1, data);
    %plot(1/Fs:1/Fs:length(data)/Fs,data); hold on
    %plot(1/10000:1/10000:length(raster)/10000,raster*10000);
    

    Hildata = hilbert(data);
    sigphase = angle(Hildata);
    if good_unit == 0
        if SOZ(cc) == 1 && length(inhib(inhib ==cc)) > 0
            label = 'ISOZ';
        elseif SOZ(cc) == 1 && length(inhib(inhib ==cc)) == 0
            label =  'ESOZ';
        elseif SOZ(cc) == 0 && length(inhib(inhib ==cc)) > 0
            label = 'INOZ';
        else 
            label = 'ENOZ';
        end
        spikephase(:, end+1) = {sigphase(1,pos); label};
        %saveas(A, strcat(sz,'__',num2str(cell2mat(chan)), '.eps'),'eps')
    else
        spikephase(:, end+1) = {[]; 'long'};
    end
    %else
      %  spikephase{1, end+1} = sigphase(1,pos);
      %  spikephase{2,1} = label;
    %end
    
end
filename = strcat(sz,'_PA(20-30).mat');
save(filename, 'spikephase')
clear data

ISOZ = {};
ESOZ = {};
INOZ = {};
ENOZ = {};

for ss = 1:length(spikephase)
    if spikephase{2,ss} == 'ISOZ'
        disp(1)
         ISOZ{1,end+1} = cell2mat(spikephase(1,ss));
    elseif  spikephase{2,ss} ==  'ESOZ'
        disp(2)
         ESOZ{1,end+1} = cell2mat(spikephase(1,ss));
    elseif spikephase{2,ss} == 'INOZ'
        disp(3)
         INOZ{1,end+1} = cell2mat(spikephase(1,ss));
    else 
        disp(4)
         ENOZ{1,end+1} = cell2mat(spikephase(1,ss));
    end
end

if length(ISOZ) > 0
    A = figure;
    rose(cell2mat(ISOZ))
    title(strcat(sz,' ',' Seizure Onset Zone Inhibitory(20-30Hz)'))
 print(strcat(sz,'__','ISOZ(20-30Hz)'), '-dpng', '-r300')
end

if  length(ESOZ) > 0
    A = figure;
    rose(cell2mat(ESOZ))
    title(strcat(sz,' ',' Seizure Onset Zone Excitatory(20-30Hz)') )
 print(strcat(sz,'__','ESOZ(20-30Hz)'), '-dpng', '-r300') 
end

if length(INOZ) > 0
    A = figure;
    rose(cell2mat(INOZ))
    title(strcat(sz,' ',' Outside Seizure Onset Zone Inhibitory(20-30Hz)') )
 print(strcat(sz,'__','INOZ(20-30Hz)'), '-dpng', '-r300')
end

if length(ENOZ) > 0
    A = figure;
    rose(cell2mat(ENOZ))
   title(strcat(sz,' ',' Outside Seizure Onset Zone Excitatory(20-30Hz)')  )
 print(strcat(sz,'__','ENOZ(20-30Hz)'), '-dpng', '-r300') 
end

