catalystneuro / dombeck-lab-to-nwb

NWB Conversion project for the Dombeck lab at Northwestern University.
MIT License
0 stars 0 forks source link

Time alignment for two photon #15

Closed weiglszonja closed 2 months ago

weiglszonja commented 2 months ago

DAQ file

weiglszonja commented 2 months ago

We received a MATLAB script from Shiva, based on that the imaging frames are aligned as:

vars: the data from the .mat file data: the data from the .dat file continuing timestamps, daq times, velocity and brake states data1: the data from the DAQ .dat file

%% get CPU, DAQ time elapsed per loop and DAQ time

daq_sampleRate = 1000;
t1 = data(:,1); % time elapsed per loop based on CPU clock (logged every ~30ms)
t2 = cumsum(data(:,2))/daq_sampleRate; % time elapsed per loop based on DAQ clock (logged every ~30ms)

% shift DAQ time so it starts the same as CPU time bc DAQ gets initialized 
% before tic starts so elapsed time is longer going into the loop
t = t2-(t2(1)-t1(1)); 

L_daq = size(data1,1);
t_daq = ((1:L_daq)/daq_sampleRate)-(t2(1)-t1(1)); % DAQ sampled at 1kHz, shifted so sampling starts the same as when CPU clock starts

%% find imaging bout

locs = find(data1(2:end,2)>.8 & data1(1:end-1,2)<.1);
locs = locs+1; % first high point of imaging pulse

% fake the frame times if there is no imaging data
% if isempty(locs)
%     fake_rate = 30; % Hz
%     locs = (1:round(vars.daq.sampleRate/fake_rate):L_daq)'; 
%     locs = locs+1;
% end

%% get frame_times

frame_times = t_daq(locs); % time of first point of the high pulse

% correct frame_times to center of imaging frame rather than the beginning
dt = mean(diff(frame_times)); 
img_times = frame_times + dt/2;