maad.sound.spectrum

maad.sound.spectrum(s, fs, nperseg=256, noverlap=None, nfft=None, window='hann', method='welch', tlims=None, flims=None, scaling='spectrum', as_pandas_series=False, display=False)[source]

Estimate the power spectral density or power spectrum of 1D signal.

The estimates can be computed using two methods: Welch or periodogram. Welch’s method divides the signal into segments, computes the power spectral density for each segment and then takes the average between segments. The periodogram method computes the power spectral density of the input signal using a defined window (Hanning window by default).

Parameters:
s: 1D array

Input signal to process

fs: float, optional

Sampling frequency of audio signal

nperseg: int, optional

Length of segment for ‘welch’ method, default is 256

noverlap: int, optional

Overlap between segments for Welch’s method. If None, noverlap = nperseg/2.

windowstring, default is ‘hann’

Name of the window used for the short fourier transform, ‘hanning’ window is now deprecated.

nfft: int, optional

Length of FFT for periodogram method. If None, length of signal will be used. Length of FFT for welch method if zero padding is desired. If None, length of nperseg will be used.

method: {‘welch’, ‘periodogram’}

Method used to estimate the power spectral density of the signal

tlims: tuple of ints or floats

Temporal limits to compute the power spectral density in seconds (s) If None, estimates for the complete signal will be computed. Default is ‘None’

flims: tuple of ints or floats

Spectral limits to compute the power spectral density in Hertz (Hz) If None, estimates from 0 to fs/2 will be computed. Default is ‘None’

scaling: {‘spectrum’, ‘density’}, optional

Choose between power spectrum (units V**2) or power spectral density (units V**2/Hz) scaling. Defaults to ‘spectrum’.

as_pandas_series: bool

Return data as a pandas.Series. This is usefull when computing multiple features over a signal. Default is False.

Returns:
pxx: pandas Series

Power spectral density estimate.

f_idx: pandas Series

Index of sample frequencies.

Notes

This is a wrapper that uses functions from Scipy (scipy.org), in particular from the scipy.signal module.

Examples

>>> from maad import sound
>>> s, fs = sound.load('../data/spinetail.wav')
>>> spec, f_idx = sound.spectrum(s, fs, nperseg=512, display=True)

Specify temporal and spectral limits to get spectral characteristics of the spinetail’s song.

>>> spec, f_idx = sound.spectrum(s, fs, nperseg=2056, tlims=(5.3, 7.9), flims=(2000, 12000), display=True)