% Jon Newman
% GA Tech
% Generate Frquency Modulated Current Chirp signal for HEKA Patchmaster current clamp input
% May 17 2010

%% Custom stimulus parameters
Fs = 20e3; % Hz
onDur = 1; % seconds
offDur = 0.25; % seconds
numsweeps = 10;
p2pamp = 50; %starting peak to peak amplitude in pA
increment = 50; % pA increment per sweep
tplbase = 'CFChirp';

%% Create TPL files
figure()
for i = 1:numsweeps
    
    amp = (p2pamp+(i-1)*increment)/2*(0.001/1); % convert amplitude with ****pA*10mV/pA SCALING!****
    ton = 0:1/Fs:onDur;      % 1 second chirp @ 20kHz sample rate
    toff = ton(end)+1/Fs:1/Fs:ton(end)+offDur+1/Fs; % 1 second of silence between sweeps
    fo = 1; f1 = 2000;   % Start at 1Hz, go up to 2000Hz
    y = amp*chirp(ton,fo,Dur,f1,'logarithmic',-90);
    y = [y zeros(size(toff))];
    
    hold all
    plot([ton toff],y)
    xlabel t
    ylabel pA
    
    %% Create .TPL file
    fname = strcat([tplbase '_' num2str(i) '_1.tpl']); % one file per sweep
    fid = fopen(fname, 'w');
    fwrite(fid, y','single');
    fclose(fid);
end
