Hi,
I need to align two noisy signals (sig1 and sig2). I tried to use both alignsignals and xcorr functions, as they seem to be perfectly fit to this task- but I have no success, both of these methods give delay = 0.
My code:
Fs = 500;
N= length(sig1);
t = 0:1/Fs:(N-1)/Fs;
figure;plot(t,sig1,t,sig2);
[Xa,Ya,D] = alignsignals(sig1,sig2);
figure;plot(t,Xa);hold on;plot(t,Ya); title(num2str(D/Fs));
[C1,LAG] = xcorr(sig1,sig2);
figure;plot(LAG/Fs,C1)
I tried smoothing the signals, but it doesn't help.
Edit: correct signals attached.

 채택된 답변

Star Strider
Star Strider 2022년 8월 18일
편집: Star Strider 2022년 8월 18일

0 개 추천

See if the alignsignals function will do what you want.
EDIT — (18 Aug 2022 at 16:04)
With the signals now provided, I can get a reasonable result by thresholding the signals to eliminate the noise below about 82, however not with the original signals —
LD1 = load(websave('sig1','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1101410/sig1.mat'));
sig1 = LD1.sig1;
LD2 = load(websave('sig2','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1101415/sig2.mat'));
sig2 = LD2.sig2;
sig1e = sig1(sig1>=82);
sig2e = sig2(sig2>=82);
[S1,S2,D] = alignsignals(sig1e, sig2e)
S1 = 179597×1
0 0 0 0 0 0 0 0 0 0
S2 = 195403×1
82.0010 82.0023 82.0037 82.0050 82.0064 82.0077 82.0091 82.0105 82.0118 82.0132
D = 4826
Fs = 500;
N= length(sig1);
t = 0:1/Fs:(N-1)/Fs;
figure
plot(t,sig1, 'DisplayName','sig1')
hold on
plot(t,sig2, 'DisplayName','sig2')
hold off
xlabel('Time')
ylabel('Amplitude')
legend('Location','best')
figure
plot(S1, 'DisplayName','sig1')
hold on
plot(S2, 'DisplayName','sig2')
hold off
ylim([80 max(ylim)])
xlabel('Index')
ylabel('Amplitude')
legend('Location','best')
text(1E+4,111, sprintf('sig1 delay = %d samples',D))
This is the best I can do with these data.
.

댓글 수: 5

Aksu
Aksu 2022년 8월 18일
Hi,
sorry for such annoying mistake. Both signals are attached now.
A.
Aksu
Aksu 2022년 8월 19일
Hi,
thank you, @Star Strider! The alignment now is much closer to expected. I was just wondering, is there any reason this function doesn't work perfectly? The outline seems quite straightforward. Maybe it could somehow be approached as an image processing task?
Star Strider
Star Strider 2022년 8월 19일
My pleasure!
The problem apears to be noise at the beginning, and for some reason that is confusing it. DIt could be worthwhile to use the sgolayfilt function to elimiinate some of the noise first (the same parameters for both signals). I doubt that an image proceasing approach would produce the result you want, that being a delay in the two signals. One approach that might work slightly better is to use findpeaks and then adjust the signals depending on the ‘locs’ differences. It depends on the result you want.
Aksu
Aksu 2022년 8월 19일
Ok, I think I will experiment with findpeaks. Thanks for good tips!
Star Strider
Star Strider 2022년 8월 19일
As always, my pleasure!

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

추가 답변 (2개)

Image Analyst
Image Analyst 2022년 8월 18일
이동: Image Analyst 2022년 8월 18일

0 개 추천

I'd think that should work but you forgot to attach your data. Alternatively you could smooth the data with sgolayfilt and then find the one or two major peaks with findpeaks and find the shift based on that.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
John D'Errico
John D'Errico 2022년 8월 18일

0 개 추천

Smoothing is NOT necessary. Lacking your data to use as an example...
t = 0:500;
S1 = sin(t/10) + randn(size(t))/10;
S2 = cos(t/10) + randn(size(t))/10;
plot(t,[S1;S2])
Now, we know the two signals (sine and cosine) are apart by a phase shift of pi/2, So in terms of the x axis used, the shift should be 10*pi/2.
[C,lag] = xcorr(S1,S2)
C = 1×1001
-0.1283 -0.0626 0.2948 0.5059 0.7778 1.2985 1.7911 2.2664 2.9037 3.6600 4.1773 4.8377 5.4632 6.0606 6.5894 7.0769 7.4680 7.6193 7.5751 7.5787 7.4423 7.2509 6.7587 6.2833 5.5216 4.8947 3.7635 2.7649 1.6219 0.4139
lag = 1×1001
-500 -499 -498 -497 -496 -495 -494 -493 -492 -491 -490 -489 -488 -487 -486 -485 -484 -483 -482 -481 -480 -479 -478 -477 -476 -475 -474 -473 -472 -471
plot(lag,C)
grid on
hold on
plot(10*pi/2*[1 1],[-250,250],'r-')
We see the peak where we would expect a peak. So the translation wil be found as we expect.
[~,ind] = max(abs(C));
translation = lag(ind)
translation = 16

댓글 수: 1

Image Analyst
Image Analyst 2022년 8월 18일
I only suggested smoothing because he said alignsignals and xcorr weren't working and if you don't smooth it there would be far too many peaks to match them up one-by-one.
Anyway, he has now attached two .mat files with the signals so maybe one of us will try with them soon.

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

카테고리

질문:

2022년 8월 18일

댓글:

2022년 8월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by