【问题标题】:Calculating spectrogram of .wav files in python在python中计算.wav文件的频谱图
【发布时间】:2019-02-27 10:57:33
【问题描述】:

我正在尝试使用 Python 从.wav 文件中计算频谱图。为了做到这一点,我遵循in here 中的说明。我首先使用 librosa 库阅读 .wav 文件。链接中的代码可以正常工作。该代码是:

sig, rate = librosa.load(file, sr = None)
sig = buf_to_int(sig, n_bytes=2)
spectrogram = sig2spec(rate, sig)

还有函数sig2spec:

def sig2spec(signal, sample_rate):

# Read the file.
# sample_rate, signal = scipy.io.wavfile.read(filename)
# signal = signal[0:int(1.5 * sample_rate)]  # Keep the first 3.5 seconds
# plt.plot(signal)
# plt.show()

# Pre-emphasis step: Amplification of the high frequencies (HF)
# (1) balance the frequency spectrum since HF usually have smaller magnitudes compared to LF
# (2) avoid numerical problems during the Fourier transform operation and
# (3) may also improve the Signal-to-Noise Ratio (SNR).
pre_emphasis = 0.97
emphasized_signal = numpy.append(signal[0], signal[1:] - pre_emphasis * signal[:-1])
# plt.plot(emphasized_signal)
# plt.show()
# Consequently, we split the signal into short time windows. We can safely make the assumption that
# an audio signal is stationary over a small short period of time. Those windows size are balanced from the
# parameter called frame_size, while the overlap between consecutive windows is controlled from the
# variable frame_stride.

frame_size = 0.025
frame_stride = 0.01
frame_length, frame_step = frame_size * sample_rate, frame_stride * sample_rate  # Convert from seconds to samples
signal_length = len(emphasized_signal)
frame_length = int(round(frame_length))
frame_step = int(round(frame_step))
num_frames = int(numpy.ceil(float(numpy.abs(signal_length - frame_length)) / frame_step))
# Make sure that we have at least 1 frame

pad_signal_length = num_frames * frame_step + frame_length
z = numpy.zeros((pad_signal_length - signal_length))
pad_signal = numpy.append(emphasized_signal, z)
# Pad Signal to make sure that all frames have equal
# number of samples without truncating any samples from the original signal

indices = numpy.tile(numpy.arange(0, frame_length), (num_frames, 1)) \
          + numpy.tile(numpy.arange(0, num_frames * frame_step, frame_step), (frame_length, 1)).T

frames = pad_signal[indices.astype(numpy.int32, copy=False)]

# Apply hamming windows. The rationale behind that is  the assumption made by the FFT that the data
# is infinite and to reduce spectral leakage.
frames *= numpy.hamming(frame_length)

# Fourier-Transform and Power Spectrum
nfft = 2048
mag_frames = numpy.absolute(numpy.fft.rfft(frames, nfft))  # Magnitude of the FFT
pow_frames = ((1.0 / nfft) * (mag_frames ** 2))  # Power Spectrum

# Transform the FFT to MEL scale
nfilt = 40
low_freq_mel = 0
high_freq_mel = (2595 * numpy.log10(1 + (sample_rate / 2) / 700))  # Convert Hz to Mel
mel_points = numpy.linspace(low_freq_mel, high_freq_mel, nfilt + 2)  # Equally spaced in Mel scale
hz_points = (700 * (10 ** (mel_points / 2595) - 1))  # Convert Mel to Hz
bin = numpy.floor((nfft + 1) * hz_points / sample_rate)

fbank = numpy.zeros((nfilt, int(numpy.floor(nfft / 2 + 1))))
for m in range(1, nfilt + 1):
    f_m_minus = int(bin[m - 1])  # left
    f_m = int(bin[m])  # center
    f_m_plus = int(bin[m + 1])  # right

    for k in range(f_m_minus, f_m):
        fbank[m - 1, k] = (k - bin[m - 1]) / (bin[m] - bin[m - 1])
    for k in range(f_m, f_m_plus):
        fbank[m - 1, k] = (bin[m + 1] - k) / (bin[m + 1] - bin[m])
filter_banks = numpy.dot(pow_frames, fbank.T)
filter_banks = numpy.where(filter_banks == 0, numpy.finfo(float).eps, filter_banks)  # Numerical Stability
filter_banks = 20 * numpy.log10(filter_banks)  # dB

return (filter_banks/ np.amax(filter_banks))*255

我可以生成如下所示的图像:

但是,在某些情况下,我的频谱图看起来像:

发生了一些非常奇怪的事情,因为在信号开始时,图像中有一些蓝色条纹,我不明白它们是否真的意味着什么,或者在计算频谱图时有错误。我猜这个问题与规范化有关,但我不确定到底是什么。

编辑:我尝试使用库中推荐的 librosa:

sig, rate = librosa.load("audio.wav", sr = None)
spectrogram = librosa.feature.melspectrogram(y=sig, sr=rate)
spec_shape = spectrogram.shape
fig = plt.figure(figsize=(spec_shape), dpi=5)
lidis.specshow(spectrogram.T, cmap=cm.jet)
plt.tight_layout()
plt.savefig("spec.jpg")

现在的规范几乎到处都是深蓝色:

【问题讨论】:

  • 除非你出于某种原因打算自己实现它,否则我建议使用scipy.signal.spectrogram
  • @user2699 OP 尝试计算 mel 缩放的频谱图,因此此函数不会给出预期的结果。这个librosa function 应该。你能看到运行 melspectrogram 并查看是否得到类似的结果吗?
  • 我对 Librosa 的问题是我无法重现相同的调色板。因此,我跳过了它。我可以试试,但最好我想使用发布的代码。
  • 知道我收到这些频谱图意味着什么吗?
  • 这里有一本关于 DSP with Python 的优秀免费书籍...greenteapress.com/wp/think-dsp

标签: python numpy audio spectrogram librosa


【解决方案1】:

这可能是因为您没有调整 librosa melspectrogram 方法的参数。

在您的原始实现中,您指定 nfft=2048。这可以传递给 melspectrogram,你会看到不同的结果。

本文介绍了“波形频率分辨率”和“fft 分辨率”,它们是进行 FT 时的重要参数。了解它们可能有助于重现您的原始频谱图。

http://www.bitweenie.com/listings/fft-zero-padding/

specshow 方法也有各种参数,这些参数会直接影响您制作的图。

此堆栈帖子列出了 MATLAB 中的各种频谱图参数,但您可以得出 MATLAB 版本和 librosa 版本之间的相似之处。

What is a spectrogram and how do I set its parameters?

【讨论】:

  • 我认为 librosa melspectrogram 中的默认值是相同的 2048。
猜你喜欢
  • 2022-01-10
  • 2016-07-30
  • 1970-01-01
  • 2013-06-17
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2018-07-08
相关资源
最近更新 更多