% Jon Newman
% Multi-parameter activity assay for cultures transfected with
% optical exciters and silencers

% File name
close all
figure
fid = 'area-preserving-triangle_2013-12-16_hoss-r3_smiley_22568_01';
s = loadspk([fid '.spk']);
d = loaddig([fid '.dig']);

%% Decode dig file
b = dec2bin(d.state,8);
u = bin2dec(b(:,1:end-3));
c = bin2dec(b(:,end-2:end-1));
t = bin2dec(b(:,end));

stm.time = d.time(logical(t));
stm.channel = u(logical(t));
% stm.type = u(logical(t));

uu = unique(u(c == 0));
uu = uu(uu~=0);

% Pulse parameters
N = 100;      % Number of trials for each parameter set
Vmax = 5;    % Volts
dt = 0.01; % DAC sample res. (ms)
A = 10; % Total light power per pulse, mW*ms/mm^2
T = [1.33 2 3 4 5 10 20 30 40 50]; % Triangle widths, ms
aoff = -0.1; % Off voltage

% Create triangle waveforms to apply
[tri, tri_time] = make_area_triangles_plot(T,A,Vmax,dt);

%% Make SS object
ss = SqueakySpk(fid,s.meta.fs_Hz,1,s,stm);
ss.RemoveChannel([1 8 33 57 64 35]);
ss.RemoveUnit(0);
ss.PeriStimHistogram(1,[-25 100])

%% Plot responses
close all
f = figure('Units','centimeters','Position',[5 5 20 20]);
col = [
[236 117 117];
[220 000 000];
[255 102 000];
[255 205 041];
[182 236 000];
[000 170 000];
[000 170 212];
[000 100 255];
[150 000 212];
[085 000 212];
[102 102 102]
]/255;


a1 = axes('Units','centimeters','Position',[5 15 10 4]);
hold on
for i = 1:length(tri)
    this_tri =  tri{i};
    this_tri(this_tri < 0 ) = 0;
    plot([-25 1000*(tri_time{i}) 100],[0 this_tri 0],'Color',col(i,:)); % - max(tri_time{i})
end
ylabel('V_{CTL} (volts)')
set(a1,'Xlim',[-25 100],'TickDir','out')



a = axes('Units','centimeters','Position',[5 7 10 6]);
hold on
for i = 1:length(tri)
   
   plot(1000*ss.psh.t,ss.psh.hist(i,:)/numel(unique(ss.unit)),'Color',col(i,:))
    
end
ylabel('Hz/unit')
xlabel('Time (ms)')
set(a,'Xlim',[-25 100],'TickDir','out')


saveas(gcf,[fid '_PSH'])
export_fig('-pdf','-transparent',[fid '_PSH'])



