% Jon Newman
% 2012 08 31
% Concatinate long-recordings into a big object

sf = dir('*spk');

% starting seconds for each recording in the directory
start_sec = [];

for f = 1:length(sf)
    m = loadspk(sf(f).name,'head');
    m.date;
    pat = '-';
    s = regexp(m.date,pat,'split');
    s = cellfun(@str2num,s);
    s = s(end:-1:1);
    start_sec = [start_sec; s(1)/1000 + s(2) + 60*s(3) + 3600*s(4) + 24*3600*s(5)];
end

% get the load order
[start_sec idx] = sort(start_sec);

% make relative times
t_rel_sec = start_sec - start_sec(1);

%% Make the monster
x.time = [];
x.unit = [];
x.channel = [];
% x.waveform = [];

for i = 1:length(idx)
    s = loadspk(sf(idx(i)).name,[],0);
    x.time = [x.time;s.time + t_rel_sec(i)];
    x.channel = [x.channel;s.channel];
    x.unit = [x.unit;s.unit];
end

%% SS object and plot
ss = SqueakySpk('test',s.meta.fs_Hz,1,x);
ss.ASDR(300);
%%
close all
figure('unit','centimeter','position',[5 5 30 5])
hold on
usdr = ss.asdr.asdr/max(ss.unit);
stairs(ss.asdr.bin/3600-t_rel_sec(3)/3600,usdr)
plot([0 0],[0 max(usdr)],'r')
plot([24 24],[0 max(usdr)],'r')
set(gca,'Xtick',-24:2:48,'TickDir','out')
xlim([-24 48])
xlabel 'Time (hrs)'
ylabel 'Hz/Unit'
title 'Fatty 358 Test (100 \muM CTZ)'
export_fig -pdf -transparent fatty358_asdr

