% Jon Newman
% Summarize effect of proportional and integral gain parameters on system
% stability

% Parameters
pre = 30;
post = 30;
dt = 1;

w = 6;
h = 1;
vs = 0.5;

% figure
close all
figure('unit','centimeter','position',[5 5 30 30])

% K
s = loadspk('2013-02-26_yam_22565_effect_of_K_02.spk',[],0);
p = loadpic('2013-02-26_yam_22565_effect_of_K_02.pic');

% Find relavant time points
ton = p.time(p.clon > 0);
t_start = ton(1);
t_stop  = ton(end);
p_start = t_start-pre;
p_stop = t_stop+post;

% Make ss object
ss = SqueakySpk([],s.meta.fs_Hz,1,s);
ss.RemoveUnit(0);

%%
a1 = axes('unit','centimeter','position',[5 15-0*vs-0*h w h]);
ss.ASDR(dt,[p_start p_stop],[],[],[],0)
ss.PlotUSDR(20,[],[],0)

set(a1,'XTick',[],'TickDir','out')
xlabel ''

%%
a2 = axes('unit','centimeter','position',[5 15-1*vs-1*h w h]);
b = p.time >= p_start & p.time <= p_stop;
plot(p.time(b)-pre,p.filt(b))

set(a2,'XTick',[],'TickDir','out','Xlim',[-pre p_stop-pre],'box','off')


%%
a3 = axes('unit','centimeter','position',[5 15-2*vs-2*h w h]);
plot(p.time(b)-pre,p.filt(b))

set(a3,'XTick',[],'Ylim',[4 6],'Xlim',[50 70],'TickDir','out','box','off')

%%
a4 = axes('unit','centimeter','position',[5 15-3*vs-3*h w h]);
hold on
plot((p.time(b)-pre)/60,p.out1(b),'b')
plot((p.time(b)-pre)/60,p.out2(b),'r')

set(a4,'XTick',[-0.5 0 0.5 1 1.5 2 2.5 3 3.5],'Ylim',[-0.1 1.1],'Xlim',[-pre p_stop-pre]/60,'TickDir','out','box','off')

%%
a5 = axes('unit','centimeter','position',[5 15-5*vs-4*h w h]);
hold on
plot(p.time(b)-pre,p.out1(b),'b')
plot(p.time(b)-pre,p.out2(b),'r')

set(a5,'XTick',[],'Ylim',[-0.1 1.1],'Xlim',[50 70],'box','off')

%% Ti
s = loadspk('2013-02-26_yam_22565_effect_of_Ti_02.spk',[],0);
p = loadpic('2013-02-26_yam_22565_effect_of_Ti_02.pic');

% Find relavant time points
ton = p.time(p.clon > 0);
t_start = ton(1);
t_stop  = ton(end);
p_start = t_start-pre;
p_stop = t_stop+post;

% Make ss object
ss = SqueakySpk([],s.meta.fs_Hz,1,s);
ss.RemoveUnit(0);

%%
a1 = axes('unit','centimeter','position',[5+w+4*vs 15-0*vs-0*h w h]);
ss.ASDR(dt,[p_start p_stop],[],[],[],0)
ss.PlotUSDR(20,[],[],0)

set(a1,'XTick',[],'TickDir','out')
xlabel ''

%%
a2 = axes('unit','centimeter','position',[5+w+4*vs 15-1*vs-1*h w h]);
b = p.time >= p_start & p.time <= p_stop;
plot(p.time(b)-pre,p.filt(b))

set(a2,'XTick',[],'TickDir','out','Xlim',[-pre p_stop-pre],'box','off')


%%
a3 = axes('unit','centimeter','position',[5+w+4*vs 15-2*vs-2*h w h]);
plot(p.time(b)-pre,p.filt(b))

set(a3,'XTick',[],'Ylim',[4 6],'Xlim',[50 70],'TickDir','out','box','off')

%%
a4 = axes('unit','centimeter','position',[5+w+4*vs 15-3*vs-3*h w h]);
hold on
plot((p.time(b)-pre)/60,p.out1(b),'b')
plot((p.time(b)-pre)/60,p.out2(b),'r')

set(a4,'XTick',[-0.5 0 0.5 1 1.5 2 2.5 3 3.5],'Ylim',[-0.1 1.1],'Xlim',[-pre p_stop-pre]/60,'TickDir','out','box','off')

%%
a5 = axes('unit','centimeter','position',[5+w+4*vs 15-5*vs-4*h w h]);
hold on
plot(p.time(b)-pre,p.out1(b),'b')
plot(p.time(b)-pre,p.out2(b),'r')

set(a5,'XTick',[],'Ylim',[-0.1 1.1],'Xlim',[50 70],'box','off')

%%
saveas(gcf,'instability_summary_00.fig')
export_fig('instability_summary_00','-pdf','-transparent',gcf)