function dst=delta(t_ref, t_targ)
% function dst=delta(t_ref, t_targ)
% computes the distortion between a reference time series t_ref and a target time series t_targ
% as the root mean square of the time-scale step function tau linking t_ref and t_targ
%
% Author: Vincent Aubanel
%
% Supplementary Material for the Manuscript:
% "Is isochronous speech special? On the relative role of isochrony and natural timing in French and English"
% submitted for Publication to Scientific Reports, Jul. 2020
% authored by Vincent Aubanel and Jean-Luc Schwartz

if length(t_ref) ~= length(t_targ)
  error('Reference and target time series must be the same length.');
end


% I. compute the time-scale step function

% compute successive durations (reference)
d_ref=diff(t_ref);

% compute successive durations (target)
d_targ=diff(t_targ);

% tau: time scale factors
tau = d_targ ./ d_ref;


% II. compute the root mean square

% log transform
log_tau = log2(tau);

% weight by interval duration (Weighted Squared Log-transformed tau)
wsl_tau = (log_tau .* log_tau) .* d_ref; 

% divide by total duration and take the square root
dst = sqrt( sum(wsl_tau) / sum(d_ref) );
