Note
Go to the end to download the full example code.
Calculating the time-dependent coherence between two channels#
The standard coherence calculation outputs a frequency series
(FrequencySeries) giving a time-averaged measure
of coherence. See Calculate the coherence between two channels for an
example.
The TimeSeries method coherence_spectrogram() performs the
same coherence calculation every stride, giving a time-varying coherence
measure.
These data are available as part of the O3 Auxiliary Channel Data Release.
First, we import the TimeSeries
from gwpy.timeseries import TimeSeries
and then get() the LIGO-Hanford strain data,
and the mains power monitor data, for a 20 minute (1200 second
window near the end of the O3 Auxiliary Channel Data Release:
error authenticating against nds.ligo.caltech.edu, attempting Kerberos kinit()
error authenticating against nds.ligo-wa.caltech.edu, attempting Kerberos kinit()
Data are accessed separately
We could have used TimeSeriesDict.get to access both channels in a
single call - that method would first try to find a single dataset that
contain both of them, then automatically fall back to separate calls if
that fails.
But, since we know that these channels are in different datsets, we access them separately here for clarity and speed.
Calculating coherence#
We can then calculate the coherence() of one
TimeSeries with respect to the other, using an 2-second Fourier
transform length, with a 1-second (50%) overlap, and a stride of 10:
coh = strain.coherence_spectrogram(mains, 20, fftlength=2, overlap=1)
Finally, we can plot() the
resulting data
plot = coh.plot()
ax = plot.gca()
ax.set_ylabel("Frequency [Hz]")
ax.set_yscale("log")
ax.set_ylim(10, 2000)
ax.set_title("Coherence between Power mains and LIGO-Hanford strain data")
ax.grid(visible=True, which="both", axis="both")
ax.colorbar(label="Coherence", clim=[0, 1], cmap="plasma")
plot.show()

This shows the time-dependent coherence between the two channels. The 60 Hz power mains frequency and its harmonics are clearly visible, but the coherence is variable over time, demonstrating the varying fidelity of the incoming mains signal.
Total running time of the script: (0 minutes 6.373 seconds)