I don't know exactly what you mean by time lag between peaks.
If you just want to know the time between two positive peaks within one pulse, it suffices to look at the frequency spectrum: the large peaks are from you dominant frequency which you can determine by an FFT taking into account your sampling frequency.
If you would like to know the time between the different replica of the pulse -- that is, the time between the peaks on the lowest figure -- first take the envelope using the Hilbert transform and than take the distance between the different maxima. If you know the shape of the pulse, you can increase the signal to noise ratio and the resolution by using a matched filter (aka auto-correlation).
Thank you for your respond. Actually, I want to use time gap between two successive predominant peaks to remove spurious peaks. My problem is finding the exact time gap between peaks because we have other spurious peak in part B of the figure.
Study a bit, mate. Xcorr will give you a function, a plot. The distance between the first peak of that and lag zero will give you the time you are looking for. To test and learn I suggest you correlate a function x1(nT) with a shifted version of itselk x2(nT)=x1(nT+T0). You will dee that this will have a peak on lag T0.
Crrecting spelling (wild fingers or keyboard) and giving some more detail:
To test and learn I suggest you correlate a function x1(nT) with a shifted version of itself x2(nT)=x1(nT+T0). Or, in the other direction of time, x2(nT)=x1(nT-T0). One is an advance, the other a delay. You will see that this will have a peak on lag T0.
I would also suggest cross-correlation, but between energy measures for example:
if 'x' is the cross correlation operator and Y1, Y2 your two time series, rather than:
Y1 x Y2, use g(Y1) x g(Y2)
where g(y) may be as simple as g(y):=y.^2,
or discontinuous g(), as:
g(y):=((detrend(y).^2) > A)
where the threshold A may be a constant, or defined as a function of y as well. The cross correlation of these indicator functions will give you a much sharper, well defined peak for a periodic waveform.
One may of course also substitute any spectral estimate for the cross correlation, the phase of the estimate should also give a good estimate of the lag, and again, indicator or energy functions may be employed to improve the measurements.
Thanks for your attention. I can not understand, I have just single signal and when I use [pk,loc]=Xcorr(((detrend(signal).^2) > mean(signal)) ; it gives me 1001 for loc. but the time lag is about 400 between 2 sinusoidal peaks. Could you please explain simpler ?
Look at the result of the individual operations, and remember that the purpose of the energy function is to provide a localized indicator of phase and to suppress impulse noise and baseline changes - a sinusiodal test signal is a different beast from that of your original question. Also, note Fernando Schlindwein's kind corrections. Try:
Not quite. I quite like the filters introduced by James Taylor but he made a minor mistake in identifying the delay (which is clearly 20 and not 19.5 as you can see in figure(1) of the script below (zoom and you will see). A slightly modified script that corrects that is as follows:
Apologies, correct - I didn't look too closely, as one might expect in a real signal to have much more than +-1 sample variation, both in the phase lag and in the precision of the energy function. The index correction, though, is good practice since it removes a systemic bias -