pyquist.plot#
Matplotlib helpers for visualizing Audio — waveform,
magnitude spectrum, and spectrogram.
Plot helpers for Audio (waveform, magnitude spectrum, spectrogram).
These work the same in a notebook and a regular script. Pass
output_file=<path> to save the figure to disk in addition to (or instead
of) displaying it interactively.
- pyquist.plot.plot(audio, *, offset=None, duration=None, figsize=(10, 3), ax=None, output_file=None)[source]#
Plots the waveform of an Audio.
Channels are overlaid on a single axis. The x-axis is time in seconds if
audio.sample_rateis set, otherwise sample index. The y-axis is symmetric about zero.- Parameters:
audio (Audio) – The audio to plot.
offset (float | None) – Start time in seconds. Defaults to
0.0(beginning of audio). Requiresaudio.sample_rateto be set.duration (float | None) – Length to plot in seconds. Defaults to the rest of the audio. Requires
audio.sample_rateto be set.figsize (Tuple[float, float]) – Figure size passed to
plt.subplots(ignored ifaxis given).ax (Axes | None) – An existing axis to draw into. If
None, a new figure is created.output_file (str | None) – If given, the figure is saved to this path via
Figure.savefig. Extension determines format (.png,.pdf,.svg, …).
- Returns:
The matplotlib
Axesthe waveform was drawn on.- Return type:
Axes
- pyquist.plot.plot_freq(audio, *, offset=None, duration=None, n_fft=None, log_frequency=True, log_amplitude=True, dynamic_range_db=80.0, figsize=(10, 3), ax=None, output_file=None)[source]#
Plots the magnitude spectrum of an Audio via a single FFT.
Multi-channel audio is first mixed to mono. By default
n_fftis the smallest power of two>= num_samples(zero-padding shorter signals), capped atNFFT_MAX. When the cap kicks in, a warning is issued and the FFT is taken over only the firstNFFT_MAXsamples.- Parameters:
audio (Audio) – The audio to analyze. Must have a
sample_rate.offset (float | None) – Start time in seconds. Defaults to
0.0(beginning of audio).duration (float | None) – Length to analyze in seconds. Defaults to the rest of the audio.
n_fft (int | None) – FFT size.
None(default) picks the smallest power of two>= num_samples(capped atNFFT_MAX). When set explicitly, shorter signals are zero-padded and longer signals are truncated to the firstn_fftsamples (no warning).log_frequency (bool) – If True (default), the x-axis uses a log scale.
log_amplitude (bool) – If True (default), magnitudes are converted to dB.
dynamic_range_db (float) – Magnitudes below
-dynamic_range_dbdB are floored for numerical stability. Only used whenlog_amplitude=True.figsize (Tuple[float, float]) – Figure size (ignored if
axis given).ax (Axes | None) – An existing axis to draw into. If
None, a new figure is created.output_file (str | None) – If given, the figure is saved to this path.
- Returns:
The matplotlib
Axesthe spectrum was drawn on.- Return type:
Axes
- pyquist.plot.plot_spec(audio, *, offset=None, duration=None, n_fft=2048, hop_length=512, log_frequency=True, log_amplitude=True, dynamic_range_db=80.0, figsize=(10, 4), ax=None, output_file=None)[source]#
Plots a magnitude spectrogram of an Audio.
Multi-channel audio is first mixed to mono. A Hann-windowed STFT with
n_fftwindow size andhop_lengthframe advance is computed; magnitudes are optionally converted to dB and plotted on a log-frequency axis (both defaults).- Parameters:
audio (Audio) – The audio to analyze. Must have a
sample_rate.offset (float | None) – Start time in seconds. Defaults to
0.0(beginning of audio).duration (float | None) – Length to analyze in seconds. Defaults to the rest of the audio.
n_fft (int) – STFT window size in samples. Defaults to 2048 (~46 ms at 44.1 kHz).
hop_length (int) – Frame advance in samples. Defaults to 512 (75% overlap).
log_frequency (bool) – If True (default), the y-axis uses a log scale.
log_amplitude (bool) – If True (default), magnitudes are converted to dB (via
pyquist.helper.amplitude_to_db()).dynamic_range_db (float) – Magnitudes below
-dynamic_range_dbdB are floored for numerical stability. Only used whenlog_amplitude=True.figsize (Tuple[float, float]) – Figure size (ignored if
axis given).ax (Axes | None) – An existing axis to draw into. If
None, a new figure is created.output_file (str | None) – If given, the figure is saved to this path.
- Returns:
The matplotlib
Axesthe spectrogram was drawn on.- Return type:
Axes