Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

dtw

動的タイム ワーピングを使用した信号間の距離

説明

dist = dtw(x,y) は、2 つのベクトル xy を、対応する点間のユークリッド距離の和 dist が最小となる時点の共通セット上に引き伸ばします。入力を引き伸ばすために、dtwxy の各要素を必要な回数だけ繰り返します。xy が行列の場合、dist は列の繰り返しにより引き伸ばしを行います。その場合、xy の行数は同じでなければなりません。

[dist,ix,iy] = dtw(x,y) は、時点の共通セット、すなわち "ワーピング パス" を返し、それらの間で distx(ix) と y(iy) が取り得る最小のものになります。

ベクトル ix とベクトル iy は同じ長さです。それぞれに、対応する信号 x または y の要素に対するインデックスが必要な回数だけ繰り返される単調増加数列が含まれます。

xy が行列の場合、ixiyx(:,ix)y(:,iy) の分離間隔が最小限となる値になります。

[___] = dtw(x,y,maxsamp) は、ワーピング パスが xy 間の直線近似の maxsamp サンプル内になるように制限します。この構文は、前述の構文の出力引数のいずれかを返します。

[___] = dtw(___,metric) は、前述の構文の入力引数のいずれかに加えて使用する距離計量を指定します。

出力引数を指定しない dtw(___) は、元の信号と整列後の信号をプロットします。

  • 信号が実数ベクトルの場合、関数は元の 2 つの信号を 1 つのサブプロットに表示し、整列後の信号をその下のサブプロットに表示します。

  • 信号が複素数ベクトルの場合、関数は元の信号と整列後の信号を 3 次元プロットに表示します。

  • 信号が実数行列の場合、関数は imagesc を使用して元の信号と整列後の信号を表示します。

  • 信号が複素行列の場合、関数は実数部と虚数部を各イメージの上半分と下半分にプロットします。

すべて折りたたむ

チャープと正弦波の 2 つの実信号を生成します。

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

動的タイム ワーピングを使用して、点の間のユークリッド距離の和が最小となるように信号を整列させます。整列後の信号と距離を表示します。

dtw(x,y);

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 (Euclidean Distance: 21.593428) contains 2 objects of type line.

正弦波周波数を初期値の 2 倍に変更します。計算を繰り返します。

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

dtw(x,y);

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 (Euclidean Distance: 169.301757) contains 2 objects of type line.

各信号に虚数部を追加します。初期の正弦波周波数に戻します。動的タイム ワーピングを使用し、ユークリッド距離の二乗和を最小化することによって信号を整列させます。

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

dtw(x,y,'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 (Squared Euclidean Distance: 2.113124), ylabel real contains 2 objects of type line.

初期のコンピューターの出力に似た書体を考案します。それを使用して MATLAB® という単語を書きます。

chr = @(x)dec2bin(x')-48;

M = chr([34 34 54 42 34 34 34]);
A = chr([08 20 34 34 62 34 34]);
T = chr([62 08 08 08 08 08 08]);
L = chr([32 32 32 32 32 32 62]);
B = chr([60 34 34 60 34 34 60]);

MATLAB = [M A T L A B];

ランダムな文字の列を繰り返し、間隔を変えて単語を壊します。元の単語と 3 つの壊れたバージョンを表示します。再現可能な結果が必要な場合は、乱数発生器をリセットします。

rng('default')

c = @(x)x(:,sort([1:6 randi(6,1,3)]));

subplot(4,1,1,'XLim',[0 60])
spy(MATLAB)
xlabel('')
ylabel('Original')

for kj = 2:4
    subplot(4,1,kj,'XLim',[0 60])
    spy([c(M) c(A) c(T) c(L) c(A) c(B)])
    xlabel('')
    ylabel('Corrupted')
end

Figure contains 4 axes objects. axes object 1 with ylabel Original contains a line object which displays its values using only markers. axes object 2 with ylabel Corrupted contains a line object which displays its values using only markers. axes object 3 with ylabel Corrupted contains a line object which displays its values using only markers. axes object 4 with ylabel Corrupted contains a line object which displays its values using only markers.

壊れたバージョンの単語をさらに 2 つ生成します。動的タイム ワーピングを使用してこの 2 つを位置合わせします。

one = [c(M) c(A) c(T) c(L) c(A) c(B)];
two = [c(M) c(A) c(T) c(L) c(A) c(B)];

[ds,ix,iy] = dtw(one,two);

onewarp = one(:,ix);
twowarp = two(:,iy);

位置合わせしていない単語と位置合わせした単語を表示します。

figure

subplot(4,1,1)
spy(one)
xlabel('')
ylabel('one')

subplot(4,1,2)
spy(two,'r')
xlabel('')
ylabel('two')

subplot(4,1,3)
spy(onewarp)
xlabel('')
ylabel('onewarp')

subplot(4,1,4)
spy(twowarp,'r')
xlabel('')
ylabel('twowarp')

Figure contains 4 axes objects. axes object 1 with ylabel one contains a line object which displays its values using only markers. axes object 2 with ylabel two contains a line object which displays its values using only markers. axes object 3 with ylabel onewarp contains a line object which displays its values using only markers. axes object 4 with ylabel twowarp contains a line object which displays its values using only markers.

dtw の組み込み機能を使用して計算を繰り返します。

dtw(one,two);

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.

異なる長さの谷で隔てられた 2 つの明確なピークから構成される信号を 2 つ生成します。信号をプロットします。

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)

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

ワーピング パスを制限せずに信号を整列させます。完全な整列を行うには、関数が、短い方の信号の 1 つのサンプルのみを反復する必要があります。

figure
dtw(x1,x2);

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 (Euclidean Distance: 0.000000) contains 2 objects of type line.

2 つの信号の間のワーピング パスと直線近似をプロットします。整列を達成するため、関数によってピーク間のトラフを十分に広げます。

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

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

Figure contains an axes object. The axes object contains 2 objects of type line.

計算を繰り返しますが、今度は、ワーピング パスが最大 3 つの要素において直線近似から逸脱するように制約を付けます。引き伸ばした信号とワーピング パスをプロットします。

[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)])

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

制約によってワーピングがサンプルの小さなサブセットに集中しすぎることはなくなりますが、整列の質が低下します。1 サンプルの制約を付けて計算を繰り返します。

dtw(x1,x2,1);

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 (Euclidean Distance: 7.600000) contains 2 objects of type line.

Fs=7418Hz でサンプリングされた音声信号を読み込みます。ファイルには、"MATLAB®" という単語を発声している女性の録音音声が含まれています。

load mtlb

% To hear, type soundsc(mtlb,Fs)

2 つの /æ/ の音素のインスタンスに対応する 2 つのセグメントを抽出します。最初のものはおおよそ 150 ms から 250 ms の間に発生し、2 番目は 370 ms から 450 ms の間に発生します。2 つの波形をプロットします。

a1 = mtlb(round(0.15*Fs):round(0.25*Fs));
a2 = mtlb(round(0.37*Fs):round(0.45*Fs));

subplot(2,1,1)
plot((0:numel(a1)-1)/Fs+0.15,a1)
title('a_1')
subplot(2,1,2)
plot((0:numel(a2)-1)/Fs+0.37,a2)
title('a_2')
xlabel('Time (seconds)')

Figure contains 2 axes objects. Axes object 1 with title a indexOf 1 baseline contains an object of type line. Axes object 2 with title a indexOf 2 baseline, xlabel Time (seconds) contains an object of type line.

% To hear, type soundsc(a1,Fs), pause(1), soundsc(a2,Fs)

信号間のユークリッド距離が最小となるように時間座標軸を歪めます。歪められた信号の共有の "持続時間" を計算してプロットします。

[d,i1,i2] = dtw(a1,a2);

a1w = a1(i1);
a2w = a2(i2);

t = (0:numel(i1)-1)/Fs;
duration = t(end)
duration = 0.1297
subplot(2,1,1)
plot(t,a1w)
title('a_1, Warped')
subplot(2,1,2)
plot(t,a2w)
title('a_2, Warped')
xlabel('Time (seconds)')

Figure contains 2 axes objects. Axes object 1 with title a_1, Warped Warped contains an object of type line. Axes object 2 with title a_2, Warped Warped, xlabel Time (seconds) contains an object of type line.

% To hear, type soundsc(a1w,Fs), pause(1), sound(a2w,Fs)

単語全体で実験を繰り返します。女性と男性が発声する「strong」という単語が含まれているファイルを読み込みます。この信号は 8 kHz でサンプリングされています。

load('strong.mat')

% To hear, type soundsc(her,fs), pause(2), soundsc(him,fs)

信号間の絶対距離が最小となるように時間座標軸を歪めます。元の信号と変換後の信号をプロットします。それらの歪められた共有 "持続時間" を計算します。

dtw(her,him,'absolute');
legend('her','him')

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 (Absolute Distance: 163.663272) contains 2 objects of type line. These objects represent her, him.

[d,iher,ihim] = dtw(her,him,'absolute');
duration = numel(iher)/fs
duration = 0.8394
% To hear, type soundsc(her(iher),fs), pause(2), soundsc(him(ihim),fs)

ファイル MATLAB1.gifMATLAB2.gif には、"MATLAB®" という単語の 2 つの手書き文字のサンプルが含まれています。ファイルを読み込み、動的タイム ワーピングを使用して x 軸に沿ってそれらを配置します。

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

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

dtw(x,y);

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.

入力引数

すべて折りたたむ

実数または複素数のベクトルまたは行列として指定される入力信号。

データ型: single | double
複素数のサポート: あり

実数または複素数のベクトルまたは行列として指定される入力信号。

データ型: single | double
複素数のサポート: あり

調整ウィンドウの幅。正の整数で指定します。

データ型: single | double

距離計量。'euclidean''absolute''squared'、または 'symmkl' で指定します。XY が両方とも K 次元の信号である場合、metricX の m 番目のサンプルと Y の n 番目のサンプルの間の距離 dmn(X,Y) を規定します。dmn(X,Y) の詳細については、動的タイム ワーピングを参照してください。

  • 'euclidean' — 距離の二乗和根。ユークリッド計量または 2 計量とも呼ばれます。

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

  • 'absolute' — 差の絶対値の総和。マンハッタン計量、街区計量、タクシー計量または 1 計量とも呼ばれます。

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

  • 'squared' — ユークリッド計量の 2 乗。距離の二乗和で構成されます。

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

  • 'symmkl' — 対称カルバック・ライブラー距離。この計量は、正の実数の XY に対してのみ有効です。

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

出力引数

すべて折りたたむ

信号間の最小距離。正の実数のスカラーとして返されます。

1 番目の信号のワーピング パス。インデックスのベクトルまたは行列として返されます。

2 番目の信号のワーピング パス。インデックスのベクトルまたは行列として返されます。

詳細

すべて折りたたむ

動的タイム ワーピング

同じ順序に並んだ同等の特徴をもつ 2 つの信号が、セクションの持続時間の差によって非常に異なって見える場合があります。動的タイム ワーピングはこれらの持続時間を歪めて、対応する特徴が共通の時間軸上の同じ位置に現れるようにするため、信号間の類似点が強調されます。

次の 2 つの K 次元信号を考えます。

X=[x1,1x1,2x1,Mx2,1x2,2x2,MxK,1xK,2xK,M]

および

Y=[y1,1y1,2y1,Ny2,1y2,2y2,NyK,1yK,2yK,N],

これらの信号には、それぞれ M 個と N 個のサンプルがあります。metric で指定された X の m 番目のサンプルと Y の n 番目のサンプルの間の距離 dmn(X,Y) が与えられた場合、distXY を、大域的な信号間の距離尺度が最小となるような時点の共通セット上に引き伸ばします。

最初に、この関数は dmn(X,Y) の取りうるすべての値を次の形状のラティスに配置します。

次に、dist は、ラティス内 (これは同一長の 2 つのシーケンス ixiy でパラメーター化されています) で以下が最小となるパスを探します。

d=mixniydmn(X,Y)

条件に合う dist パスとは、d11(X,Y) から始まり dMN(X,Y) で終わる、一連の "チェスのキング" の動きです。

  • 垂直方向の動き: (m,n) → (m + 1,n)

  • 水平方向の動き: (m,n) → (m,n + 1)

  • 対角方向の動き: (m,n) → (m + 1,n + 1)

この構造では、条件に合うパスがいずれも、サンプルをスキップしたり、信号の特徴を反復したりせず、すべての信号を揃えることが確保されます。さらに、望ましいパスは、d11(X,Y) と dMN(X,Y) の間に伸びる対角方向のラインの近くを通ります。maxsamp 引数によって調整されるこの追加の制約により、ワーピングによって類似した長さのセクションが比較され、外れ値の特徴が過剰適合されることがなくなります。

以下は格子内で取りうるパスです。

次のパスは許可されていません。

信号全体が揃っていないサンプルをスキップ元の方向に戻り、特徴を反復

参照

[1] 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.

[2] 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.

拡張機能

C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。

バージョン履歴

R2016a で導入