# How do generate spectrogram

How do I use gwpy to plot a spectrogram similar to the gravitational wave events on the GWOSC website, and what parameters should be set.
Such code is here

center = int(t0)

strain = strain.crop(center-2, center+1)

dt = 0.3 #-- Set width of q-transform plot, in seconds

hq = strain.q_transform(outseg=(t0 - dt, t0 + dt))

fig4 = hq.plot()

ax = fig4.gca()

ax.grid(False)

ax.set_yscale(‘log’)

try: fig4.colorbar(vmin = 0, vmax = 25)

except: pass

plt.show()
thank you for your solution,i wish you have a nice day!

Hi!

We show an example of this in the “Quickview Notebook”

Good luck!

Thank you,i have seen this example ,but in other GW events ,i want to know how to plot its spectogram as the same as the picture in GWOSC.Thank you for your reply!

Hi Faker

It looks like you’re using Q-Transform instead of spectrogram?
If you’re using gwpy spectrogram, there’re some parameters to adjust, for example fft length, overlap between windows,etc. Reminder: gwpy’s spectrogram function will normalize the spectorgram by default (median/mean). Normalizations here are done in each frequency bin, by using mean/median value of this bin instead of using fourier transform’s absolute coefficients. Normaliation help us to avoid spectral lines in spectrograms without line removal.

If you want to make plot yourself instead of gwpy plot function, you can first use spectrogram function to generate spectrogram class variable, and then extract the 2-d spectrogram numpy matrix with ‘variable_name.value’ then do whatever you want.
You can try to adjust those parameter in order to obtain the plots meet your requirement. BTW, I believe most of GWOSC plots are Q-Transform based time-frequency plots, you might need to take a look at Q-Transform(gwpy function)/Constant Q transform(librosa is recommended) to have a better understanding of parameters and then adjust them to obtain plots you want.

In gwpy q-transform there’s normalization as well. I suggest you to understand Constant Q-Transform first. Crucial parameters of librosa cqt: Q value (controls the time-frequency resolution), bins per octave (controls frequency bin sampling, in log scale ofc), number of bins and minimum frequency(controls frequency range). Q-transform in gwpy basically sampled multiple Q in Q range, and choose one of them, with some stratagies (for example, the one contains the brightest pixel) to perform CQT.

For data, in order to obtain better image, one should first select a long stationary noise around the target data for high resolution estimated PSD. And use the PSD to whiten the data, gwpy q transform function also contains whitening, but I believe that function uses target data for PSD estimation, which might not always be a good idea.

Best, happy xmas holidays

Thank you for your reply,i’ve already generate all gw event images that are list on the gwosc website,Unfortunately,the quantity of these images are too few.in my deep learning mission, i need one thousand images of gw images at least ,especially the images of the real gravitional wave just like the images in gw150914.If there are other ways to obtain GW images, I hope you can let me know.Thank you,best wishes.

One can use simulated data for training and test the method on real data, such as GW events released by GWOSC.

PyCBC is a standard package that has a lot of waveform integrated, which helps you generate simulated data easily, a suggestion is looking into the IMRPhenomD waveform.

Waveforms — PyCBC 2.4.dev1 documentation. Here’s a link of PyCBC document, you can take advantage from that.