Sync Video TTL pulses to another signal

20 views (last 30 days)
Deepika Gupta
Deepika Gupta on 29 Apr 2023
Answered: Sathvik on 3 May 2023
I have a signal (neural recording) and a video recording of the experiment. The camera captures frames at 30 fps and can send a trigger to a device every single time it captures a frame. I have recorded both the signal and the camera trigger on the same DAQ (which is recording at 12k Hz). I would like to:
  • downsample my signal using the camera trigger (each frame is represented as a TTL pulse which is ~5ms, drops to zero and then rises again for the next frame). Ideally, I want the program to average the signal over a single TTL pulse and return the average signal and the mid point (time stamp) of that TTL pulse, and do this for the entire recording so by the end of it all I will have the same number of signal data points as the frames in my video.
I am really lost because while I can do this with a for loop, the file is too large (50GB). I would like to use some basic matrix methods that can do the job quicker.

Answers (1)

Sathvik
Sathvik on 3 May 2023
Hi Deepika
You can start off by creating an array of the indices of rising edges of the TTL Pulses. Your code could look something like this
rising_edges = find(diff(trigger > threshold) == 1);
Calculate the start and end indices of each TTL Pulse which will then help you reshape the signal into a matrix.
pulse_starts = rising_edges;
pulse_ends = [rising_edges(2:end)-1; length(trigger)];
num_pulses = length(pulse_starts);
pulse_length = pulse_ends(1) - pulse_starts(1) + 1;
signal_matrix = reshape(signal(pulse_starts(1):pulse_ends(end)), pulse_length, num_pulses)';
Once the signal is in form of a matrix, you can use the ‘mean’ function to calculate average signal of the TTL Pulse and then obtain the mid point by taking the mean of the start and end indices arrays and multiplying this with dt (time between samples)
avg_signal = mean(signal_matrix, 2);
dt = 1/sample_rate;
mid_points = (pulse_starts + pulse_ends)/2 * dt;
Hope this helps

Categories

Find more on Signal Generation, Manipulation, and Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!