Suppose I have two time series and they are correlated, x(t) and y(t). Both of them have many peaks, and I want to know the time lag between the occurrence time of their peak. Is this possible in Matlab or R?
Spectral analysis can help to detect the period. It is better than a temporal analysis (due to the definition of a peak which can be arbitrary). Matlab Signal Processing Toolbox contains several methods. The periodogram is fine for periodic signals. Otherwise, consider power spectral density estimation. It is also possible to handle your two series using cospectral density estimation. I did that all a long time ago but not using Matlab so I cannot give you more practical details.
I think it is a good idea to measure them directly, but since my variables are correlated, rainfall and runoff data, it is a little difficult to distinguish which rainfall gives a specific discharge peak.