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 !