%% plot frequency

clear all; close all;
load worm_alldata

n_cutoff = 30;
n_firstCTZ = 10;

figure(1); hold off;

for i = 1:length(dat)
    tmp_freq = dat{i}(:,19);
    tmp_freq = tmp_freq(isfinite(tmp_freq));
    subplot(2,1,1);
    h = plot(i*ones(size(tmp_freq))+0.15*randn(size(tmp_freq)),tmp_freq,'.k');
    hold on;
    if i >= n_firstCTZ
        set(h,'Color','r');
    end
    freq.mean(i) = nanmean(tmp_freq);
    freq.n(i) = length(tmp_freq);
    freq.stderr(i) = std(tmp_freq)/sqrt(freq.n(i));
    disp([fidlist{i},': ',num2str(freq.mean(i)),' +/- ', ...
        num2str(freq.stderr(i)), ' pA', ...
        '   (n=',num2str(freq.n(i)),')']);
end
ylabel('Frequency (Hz)','FontSize',14);

freqMean(:,1) = freq.mean;
freqMean(:,2) = freq.stderr;

subplot(2,1,2); hold off;
errorbar(1:n_firstCTZ-1,freqMean(1:n_firstCTZ-1,1),freqMean(1:n_firstCTZ-1,2),'ko'); hold on;
errorbar(n_firstCTZ:size(freqMean,1),freqMean(n_firstCTZ:end,1),freqMean(n_firstCTZ:end,2),'ro');


freqMean_cutoff(:,1) = freq.mean(freq.n>=n_cutoff);
freqMean_cutoff(:,2) = freq.stderr(freq.n>=n_cutoff);
n_CTZ_inc = 1;

subplot(2,1,1);
plot([0.5,size(freqMean,1)+0.5],[mean(freqMean_cutoff(1:n_firstCTZ-1-n_CTZ_inc,1)) mean(freqMean_cutoff(1:n_firstCTZ-1-n_CTZ_inc,1))],'k--');
plot([0.5,size(freqMean,1)+0.5],[mean(freqMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1)) mean(freqMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1))],'r--');

subplot(2,1,2);
plot([0.5,size(freqMean,1)+0.5],[mean(freqMean_cutoff(1:n_firstCTZ-1-n_CTZ_inc,1)) mean(freqMean_cutoff(1:n_firstCTZ-1-n_CTZ_inc,1))],'k--');
plot([0.5,size(freqMean,1)+0.5],[mean(freqMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1)) mean(freqMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1))],'r--');

ylabel('Frequency (Hz)','FontSize',14);

