Question

I'll try to be more specific: I have several time histories of a signal which have pretty much all the same behaviour (sine waves) but all start at a different point in time. How do I automatically detect the initial time lag and delete it such that all sine waves start at the same instant in time?

Was it helpful?

Solution 2

The following works pretty well under low noise conditions and fast sampling and may do depending on your requirements for accuracy. It uses a simple threshold and thus is subject to inaccuracy when things get noisy. Adjust thresh to a low value above the noise.

Nwav = 3;
Np = 100;
tmax = 50;
A = 1000;
Nz = Np/5;

%%%%%%%%%%%%%%
thresh = A/50;
%%%%%%%%%%%%%%



% generate some waveforms
t = [0:tmax/(Np-1):tmax].';
w = rand(1,Nwav);
offs = round(rand(1,Nwav)*100);
sig = [A*sin(t(1:end-Nz)*w) ; zeros(Nz,Nwav)] + randn(Np,Nwav);
for ii=1:Nwav
    sig(:,ii) = circshift(sig(:,ii),round(rand()*Nz));
end
figure, plot(t,sig)
hold on, plot(t,repmat(thresh,length(t),1),'k--')


% use the threshold and align the waveforms
for ii=1:Nwav
    [ir ic] = find(sig(:,ii)>thresh,1)
    sig(:,ii) = circshift(sig(:,ii),-ir+1);
end
figure, plot(t,sig)
hold on, plot(t,repmat(thresh,length(t),1),'k--')

There is room for improvement (noise filtering, slope detection) but this should get you started.

I also recommend you look into waveform processing toolboxes, in matlab central for instance.

OTHER TIPS

If you have two signals, x and y, each being a n x 1 matrix where y is a shifted version of x:

[c,lags] = xcorr(x,y); % c is the correlation, should have a clear peak
s = lags(c==max(c)); % s is the shift you need
y2 = circshift(y,s); % y2 should now overlap x

(Demo purposes only - I don't suggest you circshift your actual data). The shift you are looking for in this case should ideally be relatively small compared to the length of x and y. A lot depends on the noise level and the nature of the offset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top