vocalpy.Spectrogram#
- class vocalpy.Spectrogram(data: ndarray[tuple[Any, ...], dtype[_ScalarT]], frequencies: ndarray[tuple[Any, ...], dtype[_ScalarT]], times: ndarray[tuple[Any, ...], dtype[_ScalarT]])[source]#
Bases:
objectClass that represents a spectrogram.
- Attributes:
- datanumpy.ndarray
The spectrogram itself, typically a matrix \(f \times t\) where there are \(f\) frequency bins and \(t\) time bins. Must have either 2 dimensions (frequencies, times) or 3 dimensions (channels, frequencies, times).
- frequenciesnumpy.ndarray
A vector of size \(f\) where values are frequencies at center of frequency bins in spectrogram.
- timesnumpy.ndarray
Vector of size \(t\) where values are times at center of time bins in spectrogram.
Methods
asdict()Convert this
vocalpy.Spectrogramto adict.read(path)Read spectrogram and associated arrays from a Numpy npz file at the given
path.write(path)Write this
vocalpy.Spectrogramto a Numpy npz file at the givenpath.validate_data
Examples
An example of creating a spectrogram with toy data, to demonstrate the expected dimensions of the arrays.
>>> import numpy as np >>> import vocalpy as voc >>> times = np.arange(data.shape[1]) / 32000 # 32000 is the sampling rate, to convert to seconds >>> frequencies = np.linspace(0, 10000, data.shape[0]) >>> spect = voc.Spectrogram(data, frequencies, times) >>> print(spect) Spectrogram(data=array([[0.354... 0.81988536]]), frequencies=array([ 0....000. ]), times=array([0.0000...3.121875e-02])) # noqa: E501
An example of reading a spectrogram from an npz file.
>>> spect = voc.Spectrogram.read("llb3_0066_2018_04_23_17_31_55.wav.npz") >>> spect Spectrogram(data=array([[0.561... 0. ]]), fequencies=array([[ 0...50. ]]), times=array([[0.000...6053968e+01]]))
- __init__(data: ndarray[tuple[Any, ...], dtype[_ScalarT]], frequencies: ndarray[tuple[Any, ...], dtype[_ScalarT]], times: ndarray[tuple[Any, ...], dtype[_ScalarT]]) None#
Method generated by attrs for class Spectrogram.
Methods
__init__(data, frequencies, times)Method generated by attrs for class Spectrogram.
asdict()Convert this
vocalpy.Spectrogramto adict.read(path)Read spectrogram and associated arrays from a Numpy npz file at the given
path.validate_data(attribute, value)write(path)Write this
vocalpy.Spectrogramto a Numpy npz file at the givenpath.Attributes
datafrequenciestimes- asdict()[source]#
Convert this
vocalpy.Spectrogramto adict.- Returns:
- spect_dictdict
A
dictwith keys {‘data’, ‘frequencies’, ‘times’} that map to the corresponding attributes of thisvocalpy.Spectrogram.
- classmethod read(path: str | Path)[source]#
Read spectrogram and associated arrays from a Numpy npz file at the given
path.- Parameters:
- pathstr, pathlib.Path
The path to the file containing the spectrogram
sand associated arraysfandt.
- Returns:
- spectvocalpy.Spectrogram
An instance of
vocalpy.Spectrogramcontaining the arrays loaded frompath.
- write(path: [<class 'str'>, <class 'pathlib._local.Path'>]) SpectrogramFile[source]#
Write this
vocalpy.Spectrogramto a Numpy npz file at the givenpath.- Parameters:
- pathstr, pathlib.Path
The path to where the path should be saved containing the spectrogram
dataand associated arraysfrequenciesandtimes. If this path does not already end with the extension “.npz”, that extension will be added (bynumpy.savez()).
- Returns:
- spectrogram_fileSpectrogramFile
An instance of
SpectrogramFilerepresenting the saved spectrogram.