PowerSpectrogram

class ketos.audio.spectrogram.PowerSpectrogram(data, time_res, freq_min, freq_res, window_func=None, filename=None, offset=0, label=None, annot=None, transforms=None, transform_log=None, waveform_transform_log=None, **kwargs)[source]

Power Spectrogram.

Args:
data: 2d or 3d numpy array

Spectrogram pixel values.

time_res: float

Time resolution in seconds (corresponds to the bin size used on the time axis)

freq_min: float

Lower value of the frequency axis in Hz

freq_res: float

Frequency resolution in Hz (corresponds to the bin size used on the frequency axis)

window_func: str

Window function used for computing the spectrogram

filename: str or list(str)

Name of the source audio file, if available.

offset: float or array-like

Position in seconds of the left edge of the spectrogram within the source audio file, if available.

label: int

Spectrogram label. Optional

annot: AnnotationHandler

AnnotationHandler object. Optional

transforms: list(dict)

List of dictionaries, where each dictionary specifies the name of a transformation to be applied to the spectrogram. For example, {“name”:”normalize”, “mean”:0.5, “std”:1.0}

transform_log: list(dict)

List of transforms that have been applied to this spectrogram

waveform_transform_log: list(dict)

List of transforms that have been applied to the waveform before generating this spectrogram

Attrs:
window_func: str

Window function.

Methods

empty()

Creates an empty PowerSpectrogram object

freq_res()

Get frequency resolution in Hz.

from_wav(path, window, step[, channel, ...])

Create power spectrogram directly from wav file.

from_waveform(audio[, window, step, ...])

Create a Power Spectrogram from an audio_signal.Waveform by computing the Short Time Fourier Transform (STFT).

get_kwargs()

Get keyword arguments required to create a copy of this instance.

get_repres_attrs()

Get audio representation attributes

classmethod empty()[source]

Creates an empty PowerSpectrogram object

freq_res()[source]

Get frequency resolution in Hz.

Returns:
: float

Frequency resolution in Hz

classmethod from_wav(path, window, step, channel=0, rate=None, window_func='hamming', offset=0, duration=None, resample_method='scipy', freq_min=None, freq_max=None, id=None, normalize_wav=False, transforms=None, waveform_transforms=None, decibel=True, smooth=0.01, **kwargs)[source]

Create power spectrogram directly from wav file.

The arguments offset and duration can be used to select a portion of the wav file.

Note that values specified for the arguments window, step, offset, and duration may all be subject to slight adjustments to ensure that the selected portion corresponds to an integer number of window frames, and that the window and step sizes correspond to an integer number of samples.

Args:
path: str

Path to wav file

window: float

Window size in seconds

step: float

Step size in seconds

channel: int

Channel to read from. Only relevant for stereo recordings

rate: float

Desired sampling rate in Hz. If None, the original sampling rate will be used

window_func: str
Window function (optional). Select between
  • bartlett

  • blackman

  • hamming (default)

  • hanning

offset: float

Start time of spectrogram in seconds, relative the start of the wav file.

duration: float

Length of spectrogrma in seconds.

resample_method: str
Resampling method. Only relevant if rate is specified. Options are
  • kaiser_best

  • kaiser_fast

  • scipy (default)

  • polyphase

See https://librosa.github.io/librosa/generated/librosa.core.resample.html for details on the individual methods.

freq_min: float

Lower frequency in Hz.

freq_max: str or float

Upper frequency in Hz.

id: str

Unique identifier (optional). If None, the filename will be used.

normalize_wav: bool

Normalize the waveform to have a mean of zero (mean=0) and a standard deviation of unity (std=1) before computing the spectrogram. Default is False.

transforms: list(dict)

List of dictionaries, where each dictionary specifies the name of a transformation to be applied to the spectrogram. For example, {“name”:”normalize”, “mean”:0.5, “std”:1.0}

waveform_transforms: list(dict)

List of dictionaries, where each dictionary specifies the name of a transformation to be applied to the waveform before generating the spectrogram. For example, {“name”:”add_gaussian_noise”, “sigma”:0.5}

decibel: bool

Convert to dB scale

smooth: float

Width in seconds of the smoothing region used for stitching together audio files.

Returns:
spec: MagSpectrogram

Magnitude spectrogram

Example:
>>> # load spectrogram from wav file
>>> from ketos.audio.spectrogram import MagSpectrogram
>>> spec = MagSpectrogram.from_wav('ketos/tests/assets/grunt1.wav', window=0.2, step=0.01)
>>> # crop frequency
>>> spec = spec.crop(freq_min=50, freq_max=800)
>>> # show
>>> fig = spec.plot()
>>> fig.savefig("ketos/tests/assets/tmp/spec_grunt1.png")
>>> plt.close(fig)
../_images/spec_grunt1.png
classmethod from_waveform(audio, window=None, step=None, seg_args=None, window_func='hamming', freq_min=None, freq_max=None, transforms=None, decibel=True, **kwargs)[source]

Create a Power Spectrogram from an audio_signal.Waveform by computing the Short Time Fourier Transform (STFT).

Args:
audio: Waveform

Audio signal

window: float

Window length in seconds

step: float

Step size in seconds

seg_args: dict

Input arguments used for evaluating audio.audio.segment_args(). Optional. If specified, the arguments window and step are ignored.

window_func: str
Window function (optional). Select between
  • bartlett

  • blackman

  • hamming (default)

  • hanning

freq_min: float

Lower frequency in Hz.

freq_max: str or float

Upper frequency in Hz.

transforms: list(dict)

List of dictionaries, where each dictionary specifies the name of a transformation to be applied to the spectrogram. For example, {“name”:”normalize”, “mean”:0.5, “std”:1.0}

decibel: bool

Convert to dB scale

Returns:
: MagSpectrogram

Magnitude spectrogram

get_kwargs()[source]

Get keyword arguments required to create a copy of this instance.

Does not include the data array and annotation handler.

get_repres_attrs()[source]

Get audio representation attributes