I am following the tutorial example at scipy.signal.spectrogram.
Is it possible to limit the frequencies we want to calculate the spectogram for, let's say in the range 1000-2000Hz. As per FFT, we will get it for half of the sampling frequencies. If not then can I slice the outputs to get a 3D matrix of t,f,Sxx
where I limit the frequencies to say 1000-2000 Hz? Usage in my case is that my signal contains only certain range of frequencies and keeping all will be costly for my analysis.
Low-pass and high-pass filters can be selected simply by customizing the third argument passed into the filter. The second argument indicates frequency (as fraction of Nyquist frequency, half the sample rate). Passing a list of two values in for the second argument allows for band-pass filtering of a signal.
npersegint, optional. Length of each segment. Defaults to None, but if window is str or tuple, is set to 256, and if window is array_like, is set to the length of the window. noverlapint, optional. Number of points to overlap between segments.
Unless you were to analyse a very small percentage of the frequency spectrum, computing the entire spectrum with the FFT (and throwing away the parts you don't want) is usually still a fairly efficient way to obtain a partial spectrum.
You can then find the frequencies of interest in f
using numpy.where
(following this answer by askewchan). The next step would then be to simply slice the output, extracting the desired frequency values in the 1D-array f
and associated power spectrum values in the 2D-array Sxx
. This can be done as follows:
...
f, t, Sxx = signal.spectrogram(x, fs)
fmin = 1000 # Hz
fmax = 2000 # Hz
freq_slice = np.where((f >= fmin) & (f <= fmax))
# keep only frequencies of interest
f = f[freq_slice]
Sxx = Sxx[freq_slice,:][0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With