0

Given a 40 Hz non-stationary signal, with a total of 4800 samples, I want to generate a spectrogram with appropriate parameters, and I was wondering how best to set the parameters in scipy.signal.spectrogram to use a window of 5 seconds and a 2.5 second overlap.

Would the following be correct?

f, t, Sxx = signal.spectrogram(trace.data, fs=40, nperseg=200, window=('hamming'), noverlap=100)

I get slightly confused if I use a different windowing technique, i.e. slepian, which requires a width. What is the difference between this width used with the slepian window, and the nperseg parameter? Also, what would be the benefit of applying zero-padding (nfft)?

Thanks.

geeb.24
  • 527
  • 2
  • 7
  • 25

1 Answers1

3

The parameterization seems correct, assuming that with a 40 Hz signal you mean a signal recorded at a rate of 40 samples per second.

The effect of zero padding with nfft is making the spectrum smoother and it can help improve computational performance by bringing the FFT length to a good value (multiples of small primes, like e.g. powers of 2 usually work well).

The difference between window width and nperseg is that the width determines the shape of the window and nperseg is how many samples the window contains. You could, for example, have a window with many samples but if most of them are close to zero the effective window length is shorter.

I guess this is best illustrated using an image:

enter image description here

With the same number of samples you can have a wider or narrower window, while the number of samples basically only detrmines how finely the window shape is resolved.

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import windows as wnd

i = 0
for n in [10, 20]:
    for width in [0.2, 0.9]:
        i += 1
        plt.subplot(2, 2, i)
        plt.stem(wnd.slepian(n, width))

        plt.title('n={}, width={}'.format(n, width))
plt.tight_layout()
plt.show()
MB-F
  • 22,770
  • 4
  • 61
  • 116
  • Thank you very much for your description and corresponding image. My understanding of both the window width and numbers per segment is significantly improved. Cheers. – geeb.24 Jan 04 '18 at 13:58