indexing spectrogram object
Created by: shivarajkandhasamy
Below is a code showing issue with special case of index addressing of a spectrogram object. It seems the issue depends on how the spectrogram object is created.
import numpy as np
from gwpy import timeseries, spectrogram
from scipy import signal
sample_rate = 256
segment_duration = 10
no_of_segments = 4
N = segment_duration * sample_rate * no_of_segments
mu, sigma = 0, 1 # mean and standar deviation
data = np.random.default_rng().normal(mu, sigma, N)
data_start_time = 0
time_series_data = timeseries.TimeSeries(
data, t0=data_start_time, sample_rate=sample_rate
)
specgram_gwpy = time_series_data.spectrogram(segment_duration, fftlength=segment_duration, overlap=0)
print(specgram_gwpy)
Output looks
Spectrogram([[2.36786241e-04, 1.42787448e-03, 5.53594133e-03,
..., 1.78440578e-02, 3.41429717e-03,
1.59868986e-03],
[1.21973698e-02, 1.48942053e-02, 3.62360384e-03,
..., 3.30778092e-02, 1.94371388e-02,
2.54025866e-03],
[1.05670841e-03, 1.33569450e-02, 1.62157161e-03,
..., 2.51058318e-03, 8.15885644e-03,
1.04887447e-03],
[5.47444481e-07, 9.16911852e-04, 2.53900384e-03,
..., 3.27919955e-04, 5.59133760e-03,
5.44161576e-03]]
unit: 1 / Hz,
name: None,
epoch: 0.0,
channel: None,
x0: 0.0 s,
dx: 10.0 s,
xindex: [ 0. 10. 20. 30.] s,
y0: 0.0 Hz,
dy: 0.1 Hz,
yindex: [0.000e+00 1.000e-01 2.000e-01 ... 1.278e+02 1.279e+02 1.280e+02] Hz)
Creating spectrogram object in another way,
overlap_factor = 0
f, t, Sxx = signal.spectrogram(
time_series_data.data,
fs=sample_rate,
window="hann",
nperseg= segment_duration* sample_rate,
noverlap=overlap_factor * segment_duration * sample_rate,
nfft=segment_duration * sample_rate,
mode="complex",
detrend=False,
)
specgram_scipy = spectrogram.Spectrogram(
np.real(2*np.conj(Sxx.T)*Sxx.T),
times=t + time_series_data.t0.value - (segment_duration / 2),
frequencies=f, # - (fftlength / 2)
)
print(specgram_scipy)
The output looks
Spectrogram([[0.00120062, 0.0018412 , 0.00553594, ...,
0.01784406, 0.0034143 , 0.00319738],
[0.02590223, 0.01547779, 0.0036236 , ...,
0.03307781, 0.01943714, 0.00508052],
[0.00469127, 0.01464505, 0.00162157, ...,
0.00251058, 0.00815886, 0.00209775],
[0.0013641 , 0.00231494, 0.002539 , ...,
0.00032792, 0.00559134, 0.01088323]]
unit: dimensionless,
name: None,
epoch: 0.0,
channel: None,
x0: 0.0 s,
dx: 10.0 s,
xindex: [ 0. 10. 20. 30.] s,
y0: 0.0 Hz,
dy: 0.1 Hz,
yindex: [0.000e+00 1.000e-01 2.000e-01 ... 1.278e+02 1.279e+02 1.280e+02] Hz)
Now if I try to access a particular element using the below syntax I get error with specgram_scipy
(the specgram_gwpy
works fine).
ii=0
print(specgram_gwpy[ii:ii+1])
print(specgram_scipy[ii:ii+1])