How to keep one signal fixed in dynamic time warping (dtw)?
Show older comments
Consider two discrete time uniformly sampled signals to be analyzed offline: the reference signal r(k) and the measured signal y(k), where k is the time index. Assume that r(k) and y(k) are equal except for a nonconstant delay in time. More precisely: r(k) = y(k + d(k)), where d(k) is the delay of y(k) versus r(k) at time k. I am interested in determinig d(k) for all k.
To my understanding,
[dist, kr, ky] = dtw(r, y)
stretches both signals such that the distance between r(kr) and y(ky) is minimal. However, I do not want to stretch the reference signal r(k) at all, it shall remain untouched. More precisely, I want to enforce: kr = 1:length(r) such that i can infer d(k) from ky.
Do you know a way to accomplish this enforcement?
Also, I'm not restricted to use the dtw function in this context. In case you know about a better approach to the problem stated above, please feel free to mention it (as an alternative, I will look into a cross correlation based approach)!
From what I understand, the finddelay and alignsignals function only determines a constant delay.
I highly appreciate your efforts!
Answers (1)
hello
in case that may answer the question this is a home made buffered xcorr approach
hope it helps !
clc
clearvars
samples = 1000;
dt = 0.01;
Fs = 1/dt;
t = (0:samples-1)*dt;
%% sinus waveforms
% reference signal
f = 3;
ph_ref = 2*pi*f*t+1;
sig_ref = sin(ph_ref);
% introduce some variable time delay in second signal
td = 1.5*sin(t/max(t)*pi)/(2*pi*f);
sig_meas = sin(ph_ref+td*2*pi*f);
%--------------------------------------------------------------------------
% using xcorrr-function to fit signals on x-axis
%--------------------------------------------------------------------------
% do the xcorr on data buffer to get a delay trend vs time
buffer = 64;
Overlap = 0.95;
[time,t_a] = myxcorr(sig_ref,sig_meas, Fs, buffer, Overlap);
t_aa = interp1(time,t_a,t,'linear','extrap'); % variable time delay must be computed for all time values
%--------------------------------------------------------------------------
% plotting the results
%--------------------------------------------------------------------------
figure(1)
subplot(211),plot(t, sig_ref,'b',t, sig_meas,'r')
legend('ref', 'measure')
subplot(212),plot(t,td,'b', t, t_aa,'r')
title('time delay')
legend('theoretical', 'measured')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [time,t_a] = myxcorr(x,y, Fs, buffer, Overlap)
% compute running xcorr with overlap
samples = length(x);
offset = fix((1-Overlap)*buffer);
segments = 1+ fix((samples-buffer)/offset); % Number of windows
for ci=1:segments
start = 1+(ci-1)*offset;
stop = start+buffer-1;
[c_a, lag_a] = xcorr(x(start:stop),y(start:stop));
% % c_a = c_a/max(c_a); % not needed
[~, i_a] = max(c_a);
t_a(ci) = lag_a(i_a)/Fs; % lag in seconds
end
% time vector
% time stamps are defined in the middle of the buffer
time = ((0:segments-1)*offset + round(buffer/2))/Fs;
end
1 Comment
Mathieu NOE
on 2 Apr 2025
hello again
problem solved ?
Categories
Find more on Descriptive Statistics 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!