www.gusucode.com > signal 案例源码程序 matlab代码 > signal/ConstrainedWarpingPathExample.m

    %% Constrained Warping Path
% Generate two signals consisting of two distinct peaks separated by
% valleys of different lengths. Plot the signals.

%%

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

subplot(2,1,1)
plot(x1)
xl = xlim;
subplot(2,1,2)
plot(x2)
xlim(xl)

%%
% Align the signals with no restriction on the warping path. To produce
% perfect alignment, the function needs to repeat only one sample of the
% shorter signal.

figure
dtw(x1,x2);

%%
% Plot the warping path and the straight-line fit between the two signals.
% To achieve alignment, the function expands the trough between the peaks
% generously.

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

figure
plot(i1,i2,'o-',[i1(1) i1(end)],[i2(1) i2(end)])

%%
% Repeat the computation, but now constrain the warping path to deviate at
% most three elements from the straight-line fit. Plot the stretched
% signals and the warping path.

[dc,i1c,i2c] = dtw(x1,x2,3);

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

%%
% The constraint precludes the warping from concentrating too much on a
% small subset of samples, at the expense of alignment quality. Repeat the
% calculation with a one-sample constraint.

dtw(x1,x2,1);