vocalpy.Spectrogram#

class vocalpy.Spectrogram(data: ndarray[Any, dtype[_ScalarType_co]], frequencies: ndarray[Any, dtype[_ScalarType_co]], times: ndarray[Any, dtype[_ScalarType_co]])[source]#

Bases: object

Class 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.Spectrogram to a dict.

read(path)

Read spectrogram and associated arrays from a Numpy npz file at the given path.

write(path)

Write this vocalpy.Spectrogram to a Numpy npz file at the given path.

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[Any, dtype[_ScalarType_co]], frequencies: ndarray[Any, dtype[_ScalarType_co]], times: ndarray[Any, dtype[_ScalarType_co]]) None#

Method generated by attrs for class Spectrogram.

Methods

__init__(data, frequencies, times)

Method generated by attrs for class Spectrogram.

asdict()

Convert this vocalpy.Spectrogram to a dict.

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.Spectrogram to a Numpy npz file at the given path.

Attributes

data

frequencies

times

asdict()[source]#

Convert this vocalpy.Spectrogram to a dict.

Returns:
spect_dictdict

A dict with keys {‘data’, ‘frequencies’, ‘times’} that map to the corresponding attributes of this vocalpy.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 s and associated arrays f and t.

Returns:
spectvocalpy.Spectrogram

An instance of vocalpy.Spectrogram containing the arrays loaded from path.

write(path: [str, pathlib.Path]) SpectrogramFile[source]#

Write this vocalpy.Spectrogram to a Numpy npz file at the given path.

Parameters:
pathstr, pathlib.Path

The path to where the path should be saved containing the spectrogram data and associated arrays frequencies and times. If this path does not already end with the extension “.npz”, that extension will be added (by numpy.savez()).

Returns:
spectrogram_fileSpectrogramFile

An instance of SpectrogramFile representing the saved spectrogram.