clear;clc

% dataPaths{1} = 'D:\InVivo\Shank-Only\2022-05-13_Shank-Only-UCLA\';
dataPaths{1} = 'D:\InVivo\Shank-Only\2022-05-29_Shank-Only-UCLA\';
dataPaths{2} = 'D:\InVivo\Shank-Only\2022-05-31_Shank-Only-UCLA\';
dataPaths{3} = 'D:\InVivo\S1-Opto\2023-01-27_SoCoChR2-S1_Mouse2\6-whisker_230127_150002\';

topElectrodeDepth = [-50 0 -50];

% topElectrodeDepth = [-100 0 50 0];
electrodeSpacing = 50;
for pp= 1:length(dataPaths)
    electrodeZ{pp} = topElectrodeDepth(pp):electrodeSpacing:(electrodeSpacing*64);
    electrodeZ{pp} = electrodeZ{pp}(1:20);
end

Fs = 20000/20;

addpath('U:\Personal\MATLAB\Grids-Shanks')
load('U:\Personal\Data\UCLA-Shank-ChanMap.mat')
shankMap = chanMap;
J = customcolormap_preset('red-white-blue');

%% for spontaneous pre-stim activity for all neurons, calc the power spectrum at each depth
%windows for background noise, early response, and late window
stimWindow = round(0.400*Fs:1.000*Fs);
pc=0;
f1=figure;
for pp = 1:3
    %load all LFP
    load([dataPaths{pp} 'Shank-LFP-DS.mat'])
    %for trial and every electrode (use the "left" channel map), calc the power
    %spectrum in the baseline window
    numTrials = size(lowpassBand.Channel1,1);
%     numTrials= 100;
    for cc = 1:length(chanMapRight)
        depth = electrodeZ{pp}(cc);
        if(depth==200 || depth==250 || depth==600 || depth==650)
            for tt = 1:numTrials
                lfp = lowpassBand.(['Channel' num2str(chanMapLeft(cc))])(tt,stimWindow);            
                [w,f] = cwt(lfp,Fs,'FrequencyLimits',[1 100]);
                wavelet(:,:,tt) = abs(w);
            end
            AvgWavelet = mean(wavelet,3);
            pc=pc+1;
            subplot(3,4,pc);hold on
            t = (1:size(lfp,2))./Fs.*1000-100;
            imagesc(t,f,AvgWavelet./max(AvgWavelet(:)));title(['z = ' num2str(depth)])
            axis xy
            m = mean(lowpassBand.(['Channel' num2str(chanMapLeft(cc))])(:,stimWindow));
            plot(t,0.1.*m+50,'w','LineWidth',2)
            colormap(J)
        end
    end   
    clear wavelet
end
xlim([-100 500])
ylim([5 100])