
iscell_firstcolumn = iscell(:,1);
iscell_id = find(iscell_firstcolumn);
Fcell = F(iscell_id,:);
Fneu2 = Fneu(iscell_id,:);

k = 0.7;

Fcorr = Fcell - k*Fneu2;

nr =1;
figure(1000+nr)
plot(Fcell(nr,:))
hold on
plot(Fneu2(nr,:))
plot(Fcorr(nr,:))



nCells = size(Fcorr,1);
nTrials = 4;
nDir = 8;

preStim = 2; 
stim = 3;
poststim = 2;
baseline = 1;

frame_rate = 30.95; %Hz
frame_time = 1/frame_rate; %sec

preStim_fr  = round( preStim*frame_rate );
stim_fr     = round( stim*frame_rate );
poststim_fr = round( poststim*frame_rate );
baseline_fr = round( baseline*frame_rate );
trial_fr = baseline_fr + stim_fr;


Ftrial = nan(trial_fr, nDir, nTrials, nCells);

for c = 1 : nCells
    for r = 1 : nTrials
    
        for d = 1 : nDir
            
%             ix_onset  = preStim_fr + (r-1)*(nDir*(stim_fr+poststim_fr)) + (d-1)*(stim_fr+poststim_fr) + 1;
%             ix_offset = preStim_fr + (r-1)*(nDir*(stim_fr+poststim_fr)) + (d-1)*(stim_fr+poststim_fr) + stim_fr;
            ix_onset  = preStim_fr + round( (r-1)*(nDir*(stim+poststim))*frame_rate + (d-1)*(stim+poststim)*frame_rate ) + 1;
            ix_offset = preStim_fr + round( (r-1)*(nDir*(stim+poststim))*frame_rate + (d-1)*(stim+poststim)*frame_rate ) + stim_fr;

            Fstim_tmp     = Fcorr(c,ix_onset:ix_offset);
            Fbaseline_tmp = Fcorr(c,ix_onset-baseline_fr:ix_onset-1);
            Ftrial_tmp = [Fbaseline_tmp, Fstim_tmp];
            
            Ftrial(:,d,r,c) = Ftrial_tmp;
            
            dFoF(:,d,r,c)
            
            dFoF_mean(d,r,c) = mean( dFoF(baseline_fr+1:baseline_fr+stim_fr,d,r,c) );
            
        end
    end
    
end

nr = 1;

figure(2000+nr)

for d = 1 : nDir
    subplot(1,nDir,d);
    hold on
    for r = 1 : nTrials
        plot( Ftrial(:,d,r,nr) )
    end
end


