Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting scipy.signal.spectrogram to calculate only specific frequencies

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.

like image 798
gmatharu Avatar asked Jan 04 '18 14:01

gmatharu


People also ask

How do you filter a signal in Python?

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.

What is Nperseg?

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.


1 Answers

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]
like image 144
SleuthEye Avatar answered Oct 04 '22 21:10

SleuthEye