Skip to content

Instantly share code, notes, and snippets.

View JanWilczek's full-sized avatar
💭
Teaching Digital Audio at TheWolfSound.com

Jan Wilczek JanWilczek

💭
Teaching Digital Audio at TheWolfSound.com
View GitHub Profile
@JanWilczek
JanWilczek / plot_audio_spectrogram.py
Created June 5, 2024 13:04
Easily plot the spectrogram of an audio file. plot_spectrogram_and_save() calculates the short-time Fourier transform (the STFT), then computes its magnitude (i.e., the spectrogram), then converts it to decibels full scale (normalized to the highest value), then plots the spectrogram with beautiful formatting, only to finally save it to a file. …
#!/usr/bin/env python3
# requirements:
# librosa==0.9.2
# numpy
# matplotlib
# pathlib
# soundfile
from pathlib import Path
import librosa
import librosa.display
@JanWilczek
JanWilczek / plot_magnitude_spectrum.py
Created March 29, 2024 18:53
Easily plot the magnitude spectrum of any audio signal and save it as a png file using Python and Matplotlib.Pyplot library. Includes styling the figure so that it looks reasonably well. You can use decibels full scale (dBFS) for the magnitude axis and a logarithmic frequency axis with ISO-standardized frequencies. Feel free to copy, paste & mod…
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
import soundfile as sf
import librosa
COLOR = "#ef7600"
IMG_OUTPUT_PATH = Path("img")
SAVE_PARAMS = {"dpi": 300, "bbox_inches": "tight", "transparent": True}
@JanWilczek
JanWilczek / stem_signal.py
Created February 24, 2024 16:57
Easily display audio samples with a stem plot and (optionally) save to a file. Uses matplotlib for plotting and saving the figure, pathlib for path handling, and numpy for sine generation. I use this snippet all the time. Feel free to treat it as a template: copy, paste & tweak as you need!
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
IMG_OUTPUT_PATH = Path('img')
def save(output_path: Path):
output_path.parent.mkdir(parents=True, exist_ok=True)
@JanWilczek
JanWilczek / plot_signal.py
Created February 1, 2024 16:45
Easily plot an audio signal as a continuous waveform and (optionally) save to a file. Uses matplotlib for plotting and saving the figure, pathlib for path handling, and numpy for sine generation. I use this snippet all the time to plot signals and inspect outputs of an audio system. Feel free to treat it as a template: copy, paste & tweak accord…
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
IMG_OUTPUT_PATH = Path('img')
SAVE_PARAMS = {'dpi': 300, 'bbox_inches': 'tight', 'transparent': True}
def plot_signal(signal, time=None):