-1

I generated this spectrogram (hann window, logaritmic scale) with Audacity. I need to generate similar data with Python basing on .wav file. Does anyone know, which libraries/functions should I use?

Frequency analysis from Audacity

pablo99
  • 21
  • 1
  • 5
  • Recommendations for software librarie etc are off-topic on SO. But implementing the code/functionality is not. So you may want to rephrase your question a little bit. – Jon Nordby Oct 14 '22 at 16:42

1 Answers1

0

Here are two methods for computing the frequency spectrum of an audio signal.


import librosa # for loading example audio
from matplotlib import pyplot as plt
import scipy.signal
import pandas
import numpy

def spectrum_stft(audio, sr, n_fft, window):
    """Method 1: Compute magnitude spectrogram, average over time"""
    S = librosa.stft(audio, n_fft=n_fft, window=window)
    S_db = librosa.amplitude_to_db(numpy.abs(S*S), ref=0.0, top_db=120)
    freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
    spectrum = numpy.mean(S_db, axis=1)
    return pandas.Series(spectrum, index=freqs)

def spectrum_welch(audio, sr, n_fft, window):
    """Method 2: Use Welch method. Uses overlapped complex spectrum"""
    freqs, power = scipy.signal.welch(audio, fs=sr, nfft=n_fft, window=window,
        scaling="spectrum", average='median')
    db = librosa.power_to_db(power, ref=0.0, top_db=120)
    return pandas.Series(db, index=freqs)


fft_length = 512*16
window = "hann"


# load some short example audio
path = librosa.example("trumpet")
audio, sr = librosa.load(path)
section = audio[int(1.0*sr):int(2.0*sr)]

# compute the spectrums
w = spectrum_welch(section, sr=sr, n_fft=fft_length, window=window)
s = spectrum_stft(section, sr=sr, n_fft=fft_length, window=window) - 65.0

# plot it
fig, ax = plt.subplots(1)
w.plot(ax=ax, label="welch")
s.plot(ax=ax, label="STFT")
ax.legend()
fig.savefig("spectrum.png")

Running it should produce something like this:

enter image description here

Jon Nordby
  • 5,494
  • 1
  • 21
  • 50