Aligning two noisy signals

33 views (last 30 days)
Aksu
Aksu on 18 Aug 2022
Commented: Star Strider on 19 Aug 2022
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.

Accepted Answer

Star Strider
Star Strider on 18 Aug 2022
Edited: Star Strider on 18 Aug 2022
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 Comments
Aksu
Aksu on 19 Aug 2022
Ok, I think I will experiment with findpeaks. Thanks for good tips!
Star Strider
Star Strider on 19 Aug 2022
As always, my pleasure!

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 18 Aug 2022
Moved: Image Analyst on 18 Aug 2022
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 on 18 Aug 2022
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 Comment
Image Analyst
Image Analyst on 18 Aug 2022
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!