Main Content

edr

Edit distance on real signals

Description

dist = edr(x,y,tol) returns the Edit Distance on Real Signals between sequences x and y. edr returns the minimum number of elements that must be removed from x, y, or both x and y, so that the sum of Euclidean distances between the remaining signal elements lies within the specified tolerance, tol.

example

[dist,ix,iy] = edr(x,y,tol) returns the warping path such that x(ix) and y(iy) have the smallest possible dist between them. When x and y are matrices, ix and iy are such that x(:,ix) and y(:,iy) are minimally separated.

[___] = edr(x,y,maxsamp) restricts the insertion operations so that the warping path remains within maxsamp samples of a straight-line fit between x and y. This syntax returns any of the output arguments of previous syntaxes.

example

[___] = edr(___,metric) specifies the distance metric to use in addition to any of the input arguments in previous syntaxes. metric can be one of 'euclidean', 'absolute', 'squared', or 'symmkl'.

edr(___) without output arguments plots the original and aligned signals.

  • If the signals are real vectors, the function displays the two original signals on a subplot and the aligned signals in a subplot below the first one.

  • If the signals are complex vectors, the function displays the original and aligned signals in three-dimensional plots.

  • If the signals are real matrices, the function uses imagesc to display the original and aligned signals.

  • If the signals are complex matrices, the function plots their real and imaginary parts in the top and bottom half of each image.

example

Examples

collapse all

Generate two real signals: a chirp and a sinusoid. Add a clearly outlying section to each signal.

x = cos(2*pi*(3*(1:1000)/1000).^2);
y = cos(2*pi*9*(1:399)/400);

x(400:410) = 7;
y(100:115) = 7;

Warp the signals so that the edit distance between them is smallest. Specify a tolerance of 0.1. Plot the aligned signals, both before and after the warping, and output the distance between them.

tol = 0.1;
edr(x,y,tol)

Figure contains 2 axes objects. Axes object 1 with title Original Signals contains 2 objects of type line. Axes object 2 with title Aligned Signals (Edit Distance: 617) contains 2 objects of type line.

ans = 
617

Change the sinusoid frequency to twice its initial value. Repeat the computation.

y = cos(2*pi*18*(1:399)/400);
y(100:115) = 7;

edr(x,y,tol);

Figure contains 2 axes objects. Axes object 1 with title Original Signals contains 2 objects of type line. Axes object 2 with title Aligned Signals (Edit Distance: 774) contains 2 objects of type line.

Add an imaginary part to each signal. Restore the initial sinusoid frequency. Align the signals by minimizing the sum of squared Euclidean distances.

x = exp(2i*pi*(3*(1:1000)/1000).^2);
y = exp(2i*pi*9*(1:399)/400);

x(400:405) = 5+3j;
x(405:410) = 7;

y(100:107) = 3j;
y(108:115) = 7-3j;

edr(x,y,tol,'squared');

Figure contains 2 axes objects. Axes object 1 with title Original Signals, ylabel real contains 2 objects of type line. Axes object 2 with title Aligned Signals (Edit Distance: 617), ylabel real contains 2 objects of type line.

Generate two signals consisting of two distinct peaks separated by valleys of different lengths. Plot the signals.

x1 = [0 1 0 1 0]*.95;
x2 = [0 1 0 0 0 0 0 0 1 0]*.95;

subplot(2,1,1)
plot(x1)
xlim([0 12])
subplot(2,1,2)
plot(x2)
xlim([0 12])

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

Compute the edit distance between the signals. Set a small tolerance so that the only matches are between equal samples.

tol = 0.1;

figure
edr(x1,x2,tol);

Figure contains 2 axes objects. Axes object 1 with title Original Signals contains 2 objects of type line. Axes object 2 with title Aligned Signals (Edit Distance: 5) contains 2 objects of type line.

The distance between the signals is 7. To align them, it is necessary to remove the seven central zeros of x2 or add seven zeros to x1.

Compute the D matrix, whose bottom-right element corresponds to the edit distance. For the definition of D, see Edit Distance on Real Signals.

cnd = (abs(x1'-x2))>tol;
D = zeros(length(x1)+1,length(x2)+1);
D(1,2:end) = 1:length(x2);
D(2:end,1) = 1:length(x1);

for h = 2:length(x1)+1
    for k = 2:length(x2)+1
        D(h,k) = min([D(h-1,k)+1 ...
            D(h,k-1)+1 ...
            D(h-1,k-1)+cnd(h-1,k-1)]);
    end
end

D
D = 6×11

     0     1     2     3     4     5     6     7     8     9    10
     1     0     1     2     3     4     5     6     7     8     9
     2     1     0     1     2     3     4     5     6     7     8
     3     2     1     0     1     2     3     4     5     6     7
     4     3     2     1     1     2     3     4     5     5     6
     5     4     3     2     1     1     2     3     4     5     5

Compute and display the warping path that aligns the signals.

[d,i1,i2] = edr(x1,x2,tol);

E = zeros(length(x1),length(x2));

for k = 1:length(i1)
    E(i1(k),i2(k)) = NaN;
end

E
E = 5×10

   NaN     0     0     0     0     0     0     0     0     0
     0   NaN     0     0     0     0     0     0     0     0
     0     0   NaN   NaN   NaN   NaN   NaN   NaN     0     0
     0     0     0     0     0     0     0     0   NaN     0
     0     0     0     0     0     0     0     0     0   NaN

Repeat the computation, but now constrain the warping path to deviate at most two elements from the diagonal. Plot the stretched signals and the warping path. In the second plot, set the matrix columns to run along the x-axis.

[dc,i1c,i2c] = edr(x1,x2,tol,2);

subplot(2,1,1)
plot([x1(i1c);x2(i2c)]','.-')
title(['Distance: ' num2str(dc)])
subplot(2,1,2)
plot(i2c,i1c,'o-',[i2(1) i2(end)],[i1(1) i1(end)])
axis ij
title('Warping Path')

Figure contains 2 axes objects. Axes object 1 with title Distance: 4 contains 2 objects of type line. Axes object 2 with title Warping Path contains 2 objects of type line.

The constraint results in a smaller edit distance but distorts the signals. If the constraint cannot be met, then edr returns NaN for the distance. See this by forcing the warping path to deviate at most one element from the diagonal.

[dc,i1c,i2c] = edr(x1,x2,tol,1);

subplot(2,1,1)
plot([x1(i1c);x2(i2c)]','.-')
title(['Distance: ' num2str(dc)])
subplot(2,1,2)
plot(i2c,i1c,'o-',[i2(1) i2(end)],[i1(1) i1(end)])
axis ij
title('Warping Path')

Figure contains 2 axes objects. Axes object 1 with title Distance: 6 contains 2 objects of type line. Axes object 2 with title Warping Path contains 2 objects of type line.

The files MATLAB1.gif and MATLAB2.gif contain two handwritten samples of the word "MATLAB®." Load the files. Add outliers by blotching the data.

samp1 = 'MATLAB1.gif';
samp2 = 'MATLAB2.gif';

x = double(imread(samp1));
y = double(imread(samp2));

x(15:20,54:60) = 4000;
y(15:20,84:96) = 4000;

Align the handwriting samples along the x-axis using the edit distance. Specify a tolerance of 450.

edr(x,y,450);

Figure contains 6 axes objects. Axes object 1 with title Overlaid Aligned Signals contains an object of type image. Axes object 2 with title Aligned Signal (Y) contains an object of type image. Axes object 3 with title Aligned Signal (X) contains an object of type image. Axes object 4 with title Overlaid Original Signals contains an object of type image. Axes object 5 with title Original Signal (Y) contains an object of type image. Axes object 6 with title Original Signal (X) contains an object of type image.

Input Arguments

collapse all

Input signal, specified as a real or complex vector or matrix.

Data Types: single | double
Complex Number Support: Yes

Input signal, specified as a real or complex vector or matrix.

Data Types: single | double
Complex Number Support: Yes

Tolerance, specified as a positive scalar.

Data Types: single | double

Width of adjustment window, specified as a positive integer.

Data Types: single | double

Distance metric, specified as 'euclidean', 'absolute', 'squared', or 'symmkl'. If X and Y are both K-dimensional signals, then metric prescribes dmn(X,Y), the distance between the mth sample of X and the nth sample of Y.

  • 'euclidean' — Root sum of squared differences, also known as the Euclidean or 2 metric:

    dmn(X,Y)=k=1K(xk,myk,n)*(xk,myk,n)

  • 'absolute' — Sum of absolute differences, also known as the Manhattan, city block, taxicab, or 1 metric:

    dmn(X,Y)=k=1K|xk,myk,n|=k=1K(xk,myk,n)*(xk,myk,n)

  • 'squared' — Square of the Euclidean metric, consisting of the sum of squared differences:

    dmn(X,Y)=k=1K(xk,myk,n)*(xk,myk,n)

  • 'symmkl' — Symmetric Kullback-Leibler metric. This metric is valid only for real and positive X and Y:

    dmn(X,Y)=k=1K(xk,myk,n)(logxk,mlogyk,n)

Output Arguments

collapse all

Minimum distance between signals, returned as a positive real scalar.

Warping path, returned as vectors of indices. ix and iy have the same length. Each vector contains a monotonically increasing sequence in which the indices to the elements of the corresponding signal, x or y, are repeated the necessary number of times.

More About

collapse all

References

[1] Chen, Lei, M. Tamer Özsu, and Vincent Oria. "Robust and Fast Similarity Search for Moving Object Trajectories." Proceedings of 24th ACM International Conference on Management of Data (SIGMOD ‘05). 2005, pp. 491–502.

[2] Paliwal, K. K., Anant Agarwal, and Sarvajit S. Sinha. "A Modification over Sakoe and Chiba’s Dynamic Time Warping Algorithm for Isolated Word Recognition." Signal Processing. Vol. 4, 1982, pp. 329–333.

[3] Sakoe, Hiroaki, and Seibi Chiba. "Dynamic Programming Algorithm Optimization for Spoken Word Recognition." IEEE® Transactions on Acoustics, Speech, and Signal Processing. Vol. ASSP-26, No. 1, 1978, pp. 43–49.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2016b