필터 지우기
필터 지우기

Find a portion of a signal using dtw ?

조회 수: 27 (최근 30일)
David Santos
David Santos 2024년 7월 19일 16:43
편집: Walter Roberson 대략 3시간 전
I have the two attached signals and I want to know if "ref" is included somehow (time warping could happen) inside "det" signal.
Figure 1 show ref and det signals:
As you can see ref "can fit" inside det (ref is similar to det(1:100) aprox ) so I try to adjust them normalizing signals and using dtw:
refn=ref/max(ref);
detn=det/max(det);
[~, ix ,iy]= dtw(detn,refn);
figure;plot(refn(iy));hold on;plot(detn(ix));
max(normxcorr2(refn(iy),detn(ix)))
Obtaining:
with a normalized correlation of 0.86
I would love to be able to dtw adjust ref signal over det with the nedd of both signal be the same size so the adjust could be better. For example in this case I could have the signals between x=57:204 and get a better correlation (0.99) of both signals:
x=detn(ix);
y=refn(iy);
max(normxcorr2(x(57:204),y(57:204)))
But I'm missing the first part of ref signal (which could fit quita well) and also some samples at the end because dtw always thinks that both signals have to be the same size but I'm looking also for the possibility of being just a fragment of the other.
Any clue on how to get this adjustment of signals when one can be a portion of the other?
All the best
  댓글 수: 5
David Santos
David Santos 대략 5시간 전
any guidance or clues on how to implement that custom dtw? the internal matlab algorithm is hide on a mex function that I can't review
Umar
Umar 대략 3시간 전
편집: Walter Roberson 대략 3시간 전
Hi David,
Here's a simplified example to get you started. This custom algorithm should allow for the adjustment of warping path constraints to facilitate partial matches between signals, offering a more personalized solution to the specific problem being addressed.
function [distance, warpPath] = customDTW(signal1, signal2, constraint)
% Initialize variables
n = length(signal1);
m = length(signal2);
warpPath = zeros(n+m, 2);
% Fill the warpPath matrix with constraints
for i = 1:n+m
warpPath(i, 1) = max(1, i-constraint);
warpPath(i, 2) = min(n, i+constraint);
end
% Calculate the DTW distance
D = inf(n, m);
D(1, 1) = abs(signal1(1) - signal2(1));
for i = 1:n
for j = max(1, i-constraint):min(m, i+constraint)
cost = abs(signal1(i) - signal2(j));
D(i, j) = cost + min([D(i-1, j), D(i, j-1), D(i-1, j-1)]);
end
end
distance = D(n, m);
end
So, I define a function customDTW that takes two input signals signal1 and signal2, along with a constraint value. The function calculates the DTW distance between the two signals while considering the constraints for the warping path, it also initializes variables and creates a matrix warpPath to store the warping path constraints based on the input constraint value.
It then computes the DTW distance by iteratively calculating the cost of aligning each point in signal1 with each point in signal2 within the specified constraints.The final DTW distance is stored in the variable distance, which is returned along with the warpPath matrix. Please let me know if you have any further questions.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by