Main Content

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

findsignal

類似性検索を使用した信号の位置の検出

説明

[istart,istop,dist] = findsignal(data,signal) は、検索配列 signal と最もよく一致するデータ配列 data の開始と終了のインデックスを返します。最もよく一致するセグメントは、dist (セグメントと検索配列の間のユークリッド距離の二乗) が最小のものです。datasignal が行列の場合、findsignal によって、signal に最もよく一致する data の開始と終了の列が検出されます。その場合、datasignal の行数は同じでなければなりません。

[istart,istop,dist] = findsignal(data,signal,Name,Value) は、名前と値の組の引数を使用して追加オプションを指定します。オプションには、適用する正規化、レポートされるセグメント数、使用する距離計量などがあります。

出力引数なしの findsignal(___) は、data をプロットし、signal の識別されたすべてのインスタンスを強調表示します。

  • 配列が実数ベクトルの場合、この関数によって data がサンプル数の関数として表示されます。

  • 配列が複素数ベクトルの場合、この関数によって data がアルガン図に表示されます。

  • 配列が実数行列の場合、この関数によって imagesc が使用されて signal がサブプロット上に表示され、data が別のサブプロット上に強調表示された領域と共に表示されます。

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

すべて折りたたむ

0.5 秒間 1 kHz のサンプル レートで、50% の帯域幅をもち 5 Hz のガウス パルスで構成されるデータ セットを生成します。

fs = 1e3;

t = 0:1/fs:0.5;
data = gauspuls(t,5,0.5);

10 Hz の正弦波の 1.5 サイクルで構成される信号を生成します。データ セットと信号をプロットします。

ts = 0:1/fs:0.15;
signal = cos(2*pi*10*ts);

subplot(2,1,1)
plot(t,data)
title('Data')
subplot(2,1,2)
plot(ts,signal)
title('Signal')

Figure contains 2 axes objects. Axes object 1 with title Data contains an object of type line. Axes object 2 with title Signal contains an object of type line.

信号へのユークリッド距離の二乗が最小となるデータのセグメントを検出します。データをプロットし、セグメントを強調表示します。

figure
findsignal(data,signal)

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

明らかに値が外れている 2 つのセクションをデータ セットに追加します。最小の絶対距離という観点で信号に最も近いセグメントを検出します。

dt = data;
dt(t>0.31&t<0.32) = 2.1;
dt(t>0.32&t<0.33) = -2.1;

findsignal(dt,signal,'Metric','absolute')

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

引き伸ばしにより最も近いデータ セグメントと信号との間の距離の絶対値がより小さくなる場合、x 軸を引き延ばします。

findsignal(dt,signal,'TimeAlignment','dtw','Metric','absolute')

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

値が外れている 2 つのセクションをさらにデータ セットに追加します。

dt(t>0.1&t<0.11) = 2.1;
dt(t>0.11&t<0.12) = -2.1;

findsignal(dt,signal,'TimeAlignment','dtw','Metric','absolute')

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

信号に最も近い 2 つのデータ セグメントを検索します。

findsignal(dt,signal,'TimeAlignment','dtw','Metric','absolute', ...
    'MaxNumSegments',2)

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 2 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signals.

1 つのセグメントの検索に戻ります。'edr'x 軸の引き伸ばしの基準として選択します。編集距離の許容誤差に 3 を選択します。一致しないサンプル間の編集距離は、実際の距離の影響を受けないため、'edr' は外れ値に対してロバストになります。

findsignal(dt,signal,'TimeAlignment','edr','EDRTolerance',3, ...
    'Metric','absolute')

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

計算を繰り返します。ただし、今度はデータと信号を正規化します。

  • 各データおよび信号点のいずれかの側に 10 サンプルの移動ウィンドウを定義します。

  • ウィンドウ内のデータの平均で減算し、局所標準偏差で除算します。

正規化された信号への絶対距離が最小となる正規化されたデータ セグメントを検出します。正規化されていないデータと信号、および正規化されたデータと信号を表示します。

findsignal(dt,signal,'TimeAlignment','edr','EDRTolerance',3, ...
    'Normalization','zscore','NormalizationLength',21, ...
    'Metric','absolute','Annotate','all')

Figure contains 4 axes objects. Axes object 1 with title Signal contains an object of type line. Axes object 2 with title Normalized Signal contains an object of type line. Axes object 3 with title Data contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal. Axes object 4 with title Normalized Data contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

次の条件でランダムなデータ配列を生成します。

  • 平均値は 7 つの各領域内では一定だが、領域から領域にかけて急激に変化する。

  • 標準偏差は、5 つの各領域内で一定であり、領域から領域にかけて急激に変化する。

lr = 20;

mns = [0 1 4 -5 2 0 1];
nm = length(mns);

vrs = [1 4 6 1 3]/2;
nv = length(vrs);

v = randn(1,lr*nm*nv);

f = reshape(repmat(mns,lr*nv,1),1,lr*nm*nv);
y = reshape(repmat(vrs,lr*nm,1),1,lr*nm*nv);

t = v.*y+f;

作成の手順を強調しながらデータをプロットします。各領域の平均値と標準偏差を表示します。

subplot(2,2,1)
plot(v)
title('Original')
xlim([0 700])

subplot(2,2,2)
plot([f;v+f]')
title('Means')
xlim([0 700])
text(lr*nv*nm*((0:1/nm:1-1/nm)+1/(2*nm)),-7*ones(1,nm),num2str(mns'), ...
    'HorizontalAlignment',"center")

subplot(2,2,3)
plot([y;v.*y]')
title('STD')
xlim([0 700])
text(lr*nv*nm*((0:1/nv:1-1/nv)+1/(2*nv)),-7*ones(1,nv),num2str(vrs'), ...
    'HorizontalAlignment',"center")

subplot(2,2,4)
plot(t)
title('Final')
xlim([0 700])

Figure contains 4 axes objects. Axes object 1 with title Original contains an object of type line. Axes object 2 with title Means contains 9 objects of type line, text. Axes object 3 with title STD contains 7 objects of type line, text. Axes object 4 with title Final contains an object of type line.

平均値 0 と標準偏差 1/2 のランダムな信号を作成します。信号に最もよく一致するデータ配列のセグメントを検出および表示します。

sg = randn(1,2*lr)/2;

findsignal(t,sg)

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

平均値 0 と標準偏差 2 のランダムな信号を作成します。信号に最もよく一致するデータ配列のセグメントを検出および表示します。

sg = randn(1,2*lr)*2;

findsignal(t,sg)

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

平均値 2 と標準偏差 2 のランダムな信号を作成します。信号に最もよく一致するデータ配列のセグメントを検出および表示します。

sg = randn(1,2*lr)*2+2;

findsignal(t,sg)

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

平均値 -4 と標準偏差 3 のランダムな信号を作成します。信号に最もよく一致するデータ配列のセグメントを検出および表示します。

sg = randn(1,2*lr)*3-4;

findsignal(t,sg)

Figure contains 2 axes objects. Axes object 1 with title signal contains an object of type line. Axes object 2 with title signal matches found: 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

計算を繰り返します。ただし、今度は信号とデータの両方から平均値を減算します。

findsignal(t,sg,'Normalization','zscore','Annotate','all')

Figure contains 4 axes objects. Axes object 1 with title Signal contains an object of type line. Axes object 2 with title Normalized Signal contains an object of type line. Axes object 3 with title Data contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal. Axes object 4 with title Normalized Data contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, signal.

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

rng default

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 つの壊れたバージョンを表示します。

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

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.

壊れたバージョンの単語をさらに 1 つ生成します。文字 "A" のノイズが含まれるバージョンを検索します。検索配列と、最も近いデータ セグメントの間の距離を表示します。横軸が厳格なため、セグメントは "T" まではみ出ます。

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

sgn = c(A);

[ist,ind,dst] = findsignal(corr,sgn);

clf
subplot(2,1,1)
spy(sgn)
subplot(2,1,2)
spy(corr)
chk = zeros(size(corr));
chk(:,ist:ind) = corr(:,ist:ind);
hold on
spy(chk,'*k')
hold off

Figure contains 2 axes objects. axes object 1 with xlabel nz = 26 contains a line object which displays its values using only markers. Axes object 2 with xlabel nz = 17 contains 2 objects of type line. One or more of the lines displays its values using only markers

dst
dst = 11

横軸の引き伸ばしを許可します。最も近いセグメントは、検索配列と "A" の最初のインスタンスの交差部分です。セグメントと配列の間の距離は、0 となります。

[ist,ind,dst] = findsignal(corr,sgn,'TimeAlignment','dtw');

subplot(2,1,1)
spy(sgn)
subplot(2,1,2)
spy(corr)
chk = zeros(size(corr));
chk(:,ist:ind) = corr(:,ist:ind);
hold on
spy(chk,'*k')
hold off

Figure contains 2 axes objects. axes object 1 with xlabel nz = 26 contains a line object which displays its values using only markers. Axes object 2 with xlabel nz = 16 contains 2 objects of type line. One or more of the lines displays its values using only markers

dst
dst = 0

findsignal の組み込み機能を使用して計算を繰り返します。局所的な平均で除算し、データと信号を正規化します。対称カルバック・ライブラー距離を使用します。

findsignal(corr,sgn,'TimeAlignment','dtw', ...
    'Normalization','power','Metric','symmkl','Annotate','all')

Figure contains 4 axes objects. Axes object 1 with title Signal contains an object of type image. Axes object 2 with title Normalized Signal contains an object of type image. Axes object 3 with title Data contains 3 objects of type image, patch. Axes object 4 with title Normalized Data contains 3 objects of type image, patch.

入力引数

すべて折りたたむ

データ配列。ベクトルまたは行列として指定します。

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

検索配列。ベクトルまたは行列として指定します。

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

名前と値の引数

オプションの引数のペアを Name1=Value1,...,NameN=ValueN として指定します。ここで、Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後に指定しなければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用して名前と値をそれぞれ区切り、Name を引用符で囲みます。

例: 'MaxNumSegments',2,'Metric','squared','Normalization','center','NormalizationLength',11 は、検索信号に対してユークリッド距離の最小二乗値をもつデータ配列の 2 つのセグメントを検出します。データと信号は、スライディング ウィンドウの平均を差し引くことによって正規化されます。ウィンドウには、各点の両側に 5 つのサンプルがあり、全長は 5 + 5 + 1 = 11 サンプルになります。

正規化統計量。'Normalization' と次の値のいずれかで構成されるコンマ区切りペアとして指定します。

  • 'none' — 正規化しません。

  • 'center' — 局所的な平均を減算します。

  • 'power' — 局所的な平均で除算します。

  • 'zscore' — 局所的な平均で減算し、局所的な標準偏差で除算します。

正規化長。'NormalizationLength' と整数スカラーとで構成されるコンマ区切りペアとして指定します。この値は、データと信号の両方で各サンプルを正規化する対象サンプルの最小数を表します。信号が行列である場合、'NormalizationLength' は列数を表します。

データ型: single | double

最大セグメント距離。'MaxDistance' と正の実数のスカラーとで構成されるコンマ区切りペアとして指定します。'MaxDistance' を指定した場合、findsignal によって、signal からの距離がどちらも局所的最小値で、'MaxDistance' よりも小さい、data のすべてのセグメントの開始と終了のインデックスが返されます。

データ型: single | double

返されるセグメントの最大数。'MaxNumSegments' と正の整数スカラーとで構成されるコンマ区切りのペアとして指定します。'MaxNumSegments' を指定した場合、findsignal によって、信号からの距離が局所的最小値である data のすべてのセグメントが検出され、最小距離をもつセグメントが 'MaxNumSegments' 個まで返されます。

データ型: single | double

時間の調節手法。'TimeAlignment' と次の値のいずれかで構成されるコンマ区切りペアとして指定します。

  • 'fixed' — 引き伸ばしやサンプルの繰り返しによる距離の最小化を行いません。

  • 'dtw' — 時間軸の引き伸ばしやデータまたは信号のサンプルの繰り返しによって距離の短縮を試みます。詳細については、dtw を参照してください。

  • 'edr' — 編集の回数を最小化して、残りのデータ セグメントの各サンプルと対応する信号との距離が指定した許容誤差内にあるようにします。編集は、データ、信号、または両方からのサンプルの削除で構成されます。'EDRTolerance' 引数を使用して許容誤差を指定します。このオプションは、いずれかの入力配列に外れ値がある場合に使用します。詳細については、edr を参照してください。

編集距離の許容誤差。'EDRTolerance' および実数スカラーとで構成されるコンマ区切りペアとして指定します。この引数は、'TimeAlignment' 名前と値のペアの引数が 'edr' に設定されている場合に、信号を検出するために使用します。

データ型: single | double

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

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

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

  • '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)

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

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

プロットのスタイル。'Annotate' と次の値のいずれかで構成されるコンマ区切りペアとして指定します。

  • 'data' は、データをプロットし、信号に最もよく一致する領域を強調表示します。

  • 'signal' は、信号を別のサブプロットでプロットします。

  • 'all' は、信号、データ、正規化された信号、および正規化されたデータを別のサブプロットにプロットします。

この引数は出力引数で findsignal を呼び出している場合に無視されます。

出力引数

すべて折りたたむ

セグメントの開始および終了のインデックス。整数スカラーまたは整数ベクトルで返されます。

データと信号との間の最小距離。スカラーまたはベクトルとして返されます。

拡張機能

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

バージョン履歴

R2016b で導入