API Reference
multitaper_spectrogram(data, fs, time_step, window_length, NW=4.0, n_tapers=None, freq_range=None, weight_type='unity', detrend='constant', nfft=None, db_scale=True, p_ref=2e-05)
Compute the multitaper PSD of the input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
NDArray
|
(n_samples,) Input data |
required |
fs |
int
|
Sampling frequency |
required |
time_step |
float
|
Time step between frames in seconds |
required |
window_length |
float
|
Window length in seconds |
required |
NW |
float
|
NW value, see notes for details. Defaults to 4.0. |
4.0
|
n_tapers |
Optional[int]
|
The max number of tapers, if |
None
|
freq_range |
Optional[list]
|
The desired frequency range. If |
None
|
weight_type |
Literal['unity', 'eig']
|
The type of weights among tapers. Defaults to "unity". |
'unity'
|
detrend |
Literal['constant', 'linear', 'off']
|
Whether and how to detrend the signal. Defaults to "constant". |
'constant'
|
nfft |
Optional[int]
|
The number of FFT points. If |
None
|
db_scale |
bool
|
Whether convert the result to db scale, i.e. 10log10(psd/p_ref**2). Defaults to True. |
True
|
p_ref |
float
|
If |
2e-05
|
Notes
The value of 2W is the regularization bandwidth. Typically, we choose W to be a small multiple of the fundamental frequency 1/(Ndt) (where N is the number of samples in the data), i.e. W=i/(Ndt). The value of the parameter NW
here is in fact the value of i (when dt is seen as 1). There's a trade-off between frequency resolution and variance reduction: A larger NW
will reduce the variance of the PSD estimate, but also reduce the frequency resolution.
Returns:
Name | Type | Description |
---|---|---|
times |
NDArray
|
(n_frames,) Time points of each frame |
freqs |
NDArray
|
(n_freqs,) Frequency points of the spectrogram |
psd |
NDArray
|
(n_freqs,n_frames) PSD spectrogram |
Examples:
Source code in src\pymultitaper\spectral.py
plot_spectrogram(times, freqs, psd, ax=None, **kwargs)
Plot the spectrogram.
Note: Convert the spectrogram to dB scale (set db_scale
to True
in the spectrogram functions, or convert it manually) before plotting, otherwise the plot may not be very informative.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
times |
(n_frames)
|
Time points of each frame |
required |
freqs |
(n_freqs)
|
Frequency points of the spectrogram |
required |
psd |
(n_freqs, n_frames)
|
PSD spectrogram |
required |
ax |
Optional[Axes]
|
The Axes object to plot the spectrogram. If |
None
|
**kwargs |
Additional arguments to |
{}
|
Returns:
Name | Type | Description |
---|---|---|
fig |
Figure
|
The figure object |
ax |
Axes
|
The Axes object |
Examples:
Source code in src\pymultitaper\spectral.py
plot_spectrum(times, freqs, psd, time, ax=None, **kwargs)
Plot the spectrum at a specific time point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
times |
(n_frames)
|
Time points of each frame |
required |
freqs |
(n_freqs)
|
Frequency points of the spectrogram |
required |
psd |
(n_freqs, n_frames)
|
PSD spectrogram |
required |
time |
float
|
The time point to plot the spectrum |
required |
ax |
Optional[Axes]
|
The Axes object to plot the spectrum. If |
None
|
**kwargs |
Additional arguments to |
{}
|
Returns:
Name | Type | Description |
---|---|---|
fig |
Figure
|
The figure object |
ax |
Axes
|
The Axes object |
Examples:
Source code in src\pymultitaper\spectral.py
spectrogram(data, fs, time_step, window_length, window_shape='hamming', freq_range=None, detrend='constant', nfft=None, db_scale=True, p_ref=2e-05)
Compute the ordinary (single-taper) PSD of the input data. This is similar to scipy.signal.spectrogram
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
NDArray
|
(n_samples,) Input data |
required |
fs |
int
|
Sampling frequency |
required |
time_step |
float
|
Time step between frames in seconds |
required |
window_length |
float
|
Window length in seconds |
required |
window_shape |
Union[str, tuple]
|
The shape of the window function. Defaults to "hamming". |
'hamming'
|
freq_range |
Optional[list]
|
The desired frequency range. If |
None
|
detrend |
Literal['constant', 'linear', 'off']
|
Whether and how to detrend the signal. Defaults to "constant". |
'constant'
|
nfft |
Optional[int]
|
The number of FFT points. If |
None
|
db_scale |
bool
|
Whether convert the result to db scale, i.e. 10log10(psd/p_ref**2). Defaults to True. |
True
|
p_ref |
float
|
If |
2e-05
|
Returns:
Name | Type | Description |
---|---|---|
times |
NDArray
|
(n_frames,) Time points of each frame |
freqs |
NDArray
|
(n_freqs,) Frequency points of the spectrogram |
psd |
NDArray
|
(n_freqs,n_frames) PSD spectrogram |
Examples: