% clear all;
% close all;
n_cutoff = 30;

%% Get Waveforms from Defined Group

% load('C:\Users\mingfaifong\Documents\Upscaling Paper\cultureID_TTXvsTTXCTZvsCtrl.mat');
% load('C:\Users\mingfaifong\Documents\Upscaling Paper\cultureID_CNQXvsCtrl.mat');
load('C:\Users\mingfaifong\Documents\Upscaling Paper\cultureID_CNQXvsCNQXstim1XvsCtrl.mat');
group = cultureID.ctrl; c_group = 'k'; i_subplot = 1;
% group = cultureID.TTX; c_group = 'g'; i_subplot = 2;
% group = cultureID.TTXCTZ; c_group = 'm'; i_subplot = 3;
% group = cultureID.CNQX; c_group = 'r'; i_subplot = 2;
% group = cultureID.CNQXstim1x; c_group = 'b'; i_subplot = 3;

waveform.value = [];
for i = 1:length(group)
    load(['C:\Users\mingfaifong\Documents\Upscaling Paper\waveform_rise50\' group{i} '_waveform_rise50.mat']);
%     load(['C:\Users\mingfaifong\Documents\Upscaling Paper\waveform\' group{i} '_waveform.mat']);
    curr(:,2) = repmat({group{i}},[length(curr),1]);
    waveform.value = [waveform.value; curr];
end


%% Sample Waveforms

sampdata = [];
for i_dat = 1:length(waveform.value)
    n_minis = size(waveform.value{i_dat,1},1);
    if n_minis >= n_cutoff
        i_samp = randsample(n_minis,n_cutoff);
        sampdata = [sampdata; waveform.value{i_dat,1}(i_samp,:)];
    end
end

%% Plot Average
subplot(1,3,i_subplot);
hold off;
plot(time,mean(sampdata,1),'-','Color',c_group,'LineWidth',2);
% hold on;
% plot(time,mean(sampdata,1)+std(sampdata,1),':','Color',c_group);
% plot(time,mean(sampdata,1)-std(sampdata,1),':','Color',c_group);
axis([10 45 -25 5])

title(['peak: ' num2str(min(mean(sampdata,1))) ' pA'])

%%
hold on;
plot([30 30 40],[-15 -20 -20],'-k','LineWidth',3)
axis off;