Search code examples
matlabcamerasynchronizationdata-acquisitionnidaqmx

Can you synchronize the data acquisition toolbox and the image acquisition toolbox of Matlab?


I'd like to simultaneously get data from a camera (i.e. an image) and an analog voltage using matlab. For the camera I use the imaq toolbox, for reading the voltage I use the daq toolbox (reading NI-USB device), with a following code:

clear all
% Prepare camera
vid = videoinput('gentl', 1, 'Mono8');
src = getselectedsource(vid);
vid.FramesPerTrigger = 1;
vid.TriggerRepeat = Inf;
triggerconfig(vid, 'hardware', 'DeviceSpecific', 'DeviceSpecific');
src.FrameStartTriggerMode = 'On';
src.FrameStartTriggerActivation = 'RisingEdge';

% prepare DAQ
s=daq.createSession('ni');
s.addAnalogInputChannel('Dev1','ai1','Voltage');
fid = fopen('log.txt','w');
lh = s.addlistener('DataAvailable',@(src,event)SaveData(fid,event));
s.IsContinuous = true;

% Take data
s.startBackground();
start(vid)
N=10;
for ii=1:N
    im(:,:,ii)=getsnapshot(vid);
end


% end code
delete(lh );
fclose('all');
stop(vid)
delete(vid)

where the function SaveData is:

function SaveData(fid,event)
     time = event.TimeStamps;
     data = event.Data;
     fprintf(fid, '%f,%f\n ', [time data]);
end

I do get images and a log.txt file with the daq trace (time and data), but how can I use the external triggering (that trigger the camera) or some other clock to synchronize the two? For this example, the daq reads the camera triggering TTL signal (@ 50 Hz), so I want to assign each TTL pulse to an image.

Addendum: I've been searching and have found a few discussions (like this one) on the subject, and read the examples that are found in the Mathworks website, but haven't found an answer. The documentation shows how to Start a Multi-Trigger Acquisition on an External Event, but the acquisition discussed is only relevant for the DAQ based input, not a camera based input (it is also working in the foreground).


Solution

  • This will not entirely solve your problem, but it might be good enough. Since the synchronization signal you are after in at 50 Hz, you can use clock in order to create time stamps for both types of your data (camera image and analog voltage). Since the function clock takes practically no time (i.e. below 1e-7 sec), you can try edit to your SaveData function accordingly:

    fprintf(fid, '%f,%f\n ', [clock time data]);
    

    And in the for loop add:

    timestamp(i,:)=clock;