Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
2,735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.01 KB | None | 0 0
  1. function [dist,ix,iy] = dtw(x,y,varargin)
  2. %DTW    Distance between signals via Dynamic Time Warping
  3. %   DIST = DTW(X,Y) computes the total distance, DIST, as the minimum sum
  4. %   of Euclidean distances between the samples of vectors X and Y, where
  5. %   samples in either X or Y may repeat consecutively any number of times.
  6. %   If X and Y are matrices, then X and Y must have the same number of rows
  7. %   and DTW minimizes the total Euclidean distances between the column
  8. %   vectors of X and Y, allowing columns of X and Y to consecutively
  9. %   repeat.
  10. %
  11. %   [DIST,IX,IY] = DTW(X,Y) additionally returns the warping path, IX and
  12. %   IY, that minimizes the total Euclidean distance between X(IX) and Y(IY)
  13. %   when X and Y are vectors and between X(:,IX) and Y(:,IY) when X and Y
  14. %   are matrices.
  15. %
  16. %   [DIST,IX,IY] = DTW(X,Y,MAXSAMP) additionally restricts IX and IY so
  17. %   that they must be within MAXSAMP samples of a straight-line fit between
  18. %   X and Y.  If MAXSAMP is unspecified, then no restriction will be placed
  19. %   upon IX or IY.
  20. %
  21. %   [DIST,IX,IY] = DTW(X,Y,...,METRIC) will return in DIST the summed
  22. %   distance between the corresponding entries of X and Y according to the
  23. %   distance metric defined by METRIC.  The default is 'euclidean'.
  24. %      'absolute'  the sum of the absolute (manhattan) differences
  25. %      'euclidean' the root sum squared differences
  26. %      'squared'   the squared Euclidean distance
  27. %      'symmkl'    the symmetric Kullback-Leibler distance
  28. %   When data and signal are matrices, the distances are taken between
  29. %   corresponding column vectors; when data and signal are vectors,
  30. %   distances are taken between the corresponding elements.  Note that when
  31. %   data and signal are vectors, 'absolute' and 'euclidean' are equivalent.
  32. %
  33. %   DTW(...) without output arguments plots the original and aligned
  34. %   signals.  DTW displays the alignment of X and Y via a line plot when
  35. %   they are vectors, and via horizontally aligned images when they are
  36. %   matrices.  If the matrices are complex, the real and imaginary
  37. %   portions appear in the top and bottom half of each image, respectively.
  38. %  
  39. %   % Example 1:
  40. %   %   Compute and plot the best Euclidean distance between real
  41. %   %   chirp and sinusoidal signals using dynamic time warping.
  42. %   x = chirp(0:999,0,1000,1/100);
  43. %   y = cos(2*pi*5*(0:199)/200);
  44. %   dtw(x,y)
  45. %
  46. %   % Example 2:
  47. %   %   Compute and plot the best Euclidean distance between complex
  48. %   %   chirp and sinusoidal signals using dynamic time warping.
  49. %   x = exp(2i*pi*(3*(1:1000)/1000).^2);
  50. %   y = exp(2i*pi*9*(1:399)/400);
  51. %   dtw(x,y)
  52. %
  53. %   % Example 3:
  54. %   %   Align handwriting samples along the x-axis.
  55. %   load blockletterex
  56. %   dtw(MATLAB1,MATLAB2);
  57. %
  58. %   See also EDR, ALIGNSIGNALS, FINDSIGNAL, FINDDELAY, XCORR.
  59.  
  60. %   References:
  61. %   * H. Sakoe and S. Chiba, "Dynamic Programming Algorithm Optimization
  62. %     for Spoken Word Recognition" IEEE Transactions on Acoustics, Speech
  63. %     and Signal Processing, Vol. ASSP-26, No. 1, Feb 1978, pp. 43-49.
  64. %   * K.K. Paliwal, A. Agarwal and S.S. Sinha, "A Modification over Sakoe
  65. %     and Chiba's dynamic time warping algorithm for isolated word
  66. %     recognition" IEEE International Conference on ICASSP 1982., Vol. 7,
  67. %     pp. 1259-1261
  68.  
  69. %   Copyright 2015 The MathWorks, Inc.
  70.  
  71. if nargin > 2
  72.     [varargin{:}] = convertStringsToChars(varargin{:});
  73. end
  74.  
  75. narginchk(2,4);
  76.  
  77. needsTranspose = iscolumn(x);
  78.  
  79. if iscolumn(x) && isvector(y)
  80.   x = x.';
  81. end
  82.  
  83. if iscolumn(y) && isvector(x)
  84.   y = y.';
  85. end
  86.  
  87. validateattributes(x,{'single','double'},{'nonnan','2d','finite'},'dtw','X',1);
  88. validateattributes(y,{'single','double'},{'nonnan','2d','finite'},'dtw','Y',2);
  89.  
  90. if size(x,1) ~= size(y,1)
  91.   error(message('signal:dtw:RowMismatch'));
  92. end
  93.  
  94. [metric, varargin] = getmutexclopt({'absolute','euclidean','squared','symmkl'},'euclidean',varargin);
  95. if strcmp(metric,'symmkl')
  96.   if ~isreal(x) || ~isreal(y) || any(x(:)<0) || any(y(:)<0)
  97.     error(message('signal:dtw:MustBeRealPositive'))
  98.   end
  99. end
  100. chkunusedopt(varargin);
  101.  
  102. if ~isempty(x) && ~isempty(y)
  103.   if ~isempty(varargin)
  104.     r = varargin{1};
  105.     validateattributes(r,{'numeric'},{'finite','integer','positive','scalar','real'},'dtw','MAXSAMP',3)
  106.     C = constrainedCumulativeDistance(x, y, double(r), metric);
  107.   else
  108.     C = unconstrainedCumulativeDistance(x, y, metric);
  109.   end
  110.   dist=C(size(x,2),size(y,2));
  111.   [ix,iy] = traceback(C);
  112. else
  113.   dist = NaN;
  114.   ix = zeros(1,0);
  115.   iy = zeros(1,0);
  116. end
  117.  
  118. if nargout==0
  119.   dtwplot(x, y, ix, iy, dist, metric)
  120. elseif needsTranspose
  121.   ix = ix';
  122.   iy = iy';
  123. end
  124.  
  125. %-------------------------------------------------------------------------
  126. function C = unconstrainedCumulativeDistance(x, y, metric)
  127. if isreal(x) && isreal(y)
  128.   C = dtwmex(x, y, metric);
  129. else
  130.   C = dtwmex([real(x); imag(x)], [real(y); imag(y)], metric);
  131. end
  132.  
  133. %-------------------------------------------------------------------------
  134. function C = constrainedCumulativeDistance(x, y, r, metric)
  135. m = size(x,2);
  136. n = size(y,2);
  137. if m<n
  138.   if isreal(x) && isreal(y)
  139.     C = dtwmex(y, x, r, metric)';
  140.   elseif ~isreal(x) && ~isreal(y)
  141.     C = dtwmex([real(y); imag(y)], [real(x); imag(x)], r, metric)';
  142.   end
  143. elseif isreal(x) && isreal(y)
  144.   C = dtwmex(x, y, r, metric);
  145. else
  146.   C = dtwmex([real(x); imag(x)], [real(y); imag(y)], r, metric);
  147. end
  148.  
  149. %-------------------------------------------------------------------------
  150. function [ix,iy] = traceback(C)
  151. m = size(C,1);
  152. n = size(C,2);
  153.  
  154. % pre-allocate to the maximum warping path size.
  155. ix = zeros(m+n,1);
  156. iy = zeros(m+n,1);
  157.  
  158. ix(1) = m;
  159. iy(1) = n;
  160.  
  161. i = m;
  162. j = n;
  163. k = 1;
  164.  
  165. while i>1 || j>1
  166.   if j == 1
  167.     i = i-1;
  168.   elseif i == 1
  169.     j = j-1;
  170.   else
  171.     % trace back to the origin, ignoring any NaN value
  172.     % prefer i in a tie between i and j
  173.     cij = C(i-1,j-1);
  174.     ci = C(i-1,j);
  175.     cj = C(i,j-1);
  176.     i = i - (ci<=cj | cij<=cj | cj~=cj);
  177.     j = j - (cj<ci | cij<=ci | ci~=ci);
  178.   end
  179.   k = k+1;
  180.   ix(k) = i;
  181.   iy(k) = j;
  182. end
  183.  
  184. ix = ix(k:-1:1);
  185. iy = iy(k:-1:1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement