How can sync a video to the plot?

7 次查看(过去 30 天)
I am trying to sync the video to the plot such that when the video plays(slicing a potato on a pressure sensor) the corresponding pressure signals are plotted.
But I am not able to sync the speed of the video to the speed of the plot.
I have also attached the video and the excel file of the pressure data to this .
Could anyone help me to solve this?
rng default
Fs = 24;
xs = potatoslicetimesync.Force;
ts = (0:length(xs)-1/Fs);
%plot(ts,xs)
%% Setup the subplots
ax1 = subplot(2,1,1); % For video
ax2 = subplot(2,1,2); % For pressure plot
%%% Setup VideoReader object
filename = 'slicesync.avi';
v = VideoReader(filename);
frameratevideo=v.FrameRate;
s = VideoWriter('C:\Users\Suj\Desktop\Master Thesis\Videos\slicesync.avi');
s.FrameRate = 500;
nFrames = v.Duration*v.FrameRate; % Number of frames
% Display the first frame in the top subplot
vidFrame = readFrame(v);
v.CurrentTime = 0;
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
%%% Load the pressure data
t = ts; % Cooked up for this example, use your actual data
y = xs;
nDataPoints = length(t); % Number of data points
step = round((nFrames/nDataPoints));
index = 1:0.55:nDataPoints;
i = 2;
% Diplay the plot corresponds to the first frame in the bottom subplot
h = plot(ax2,t(1:index(i)),y(1:index(i)),'-k');
% Fix the axes
ax2.XLim = [t(1) t(end)];
ax2.YLim = [min(y) max(y)];
%%% Animate
while hasFrame(v)
pause(1/v.FrameRate);
%pause(0.01)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
i = i + 1;
set(h,'YData',y(1:index(i)), 'XData', t(1:index(i)))
end
%%% Load the pressure data
t = ts; % your actual data
y = xs;
nDataPoints = length(t); % Number of data points
step = round((nFrames/nDataPoints));
index = 1:step:nDataPoints;
i = 2;
% Diplay the plot corresponds to the first frame in the bottom subplot
h = plot(ax2,t(1:index(i)),y(1:index(i)),'-k');
% Fix the axes
ax2.XLim = [t(1) t(end)];
ax2.YLim = [min(y) max(y)];
%%% Animate
while hasFrame(v)
pause(1/s.FrameRate);
%pause(0.01)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
i = i + 1;
set(h,'YData',y(1:index(i)), 'XData', t(1:index(i)))
end

回答(1 个)

Swastik Sarkar
Swastik Sarkar 2025-6-20
There appear to be a few inconsistencies in the current implementation. The following adjustments may help improve synchronization between the video and the pressure plot:
Sampling time
In the section:
ts = (0:length(xs)-1/Fs);
This expression does not correctly compute the sampling time intervals. The division by Fs is applied only to the last element due to operator precedence. To generate a proper time vector corresponding to the sampling frequency, consider using:
ts = (0:length(xs)-1) / Fs;
Index Vector and Interpolation
In the section:
step = round((nFrames/nDataPoints));
index = 1:0.55:nDataPoints;
This approach may not ensure consistent alignment between the video frames and the pressure data. A more reliable method would be to interpolate the pressure values to match the number of video frames. This can be done as follows:
t_interp = linspace(ts(1), ts(end), nFrames);
xs_interp = interp1(ts, xs, t_interp, 'linear');
This ensures that each video frame corresponds to a pressure value, maintaining temporal consistency. Additional information on interp1 can be found in the MATLAB documentation.
Since the pressure data is now interpolated to match the video frame count, the two separate animation loops are no longer necessary. The logic can be consolidated into a single loop that updates both the video frame and the corresponding plot point-by-point.
Hope this helps synchronize the plots !

Community Treasure Hunt

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

Start Hunting!

Translated by