Aligning two noisy signals
33 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Star Strider
on 18 Aug 2022
Edited: Star Strider
on 18 Aug 2022
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)
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
More Answers (2)
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:
0 Comments
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)
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)
1 Comment
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!