尝试将 PCM 转换为频率图,但结果在 0 附近看起来很奇怪

Trying to convert PCM to frequency chart but result looks very strange near 0

我尝试将 PCM 数据从 wav 文件和 FFT 转换为频率图。 这是我的图表。 0.00s 512 样本计数 3.15s 512 样本计数

声音文件几乎是安静的,3 秒开始有一些敲击声。

我注意到接近 0 的值非常高。可是怎么可能! 还有一个奇怪的点是"the value is 0 when frequency greater than about 16000".

这是我的代码:

import soundfile as sf
import numpy as np
import math
import matplotlib.pyplot as plt


_audio_path = 'source_normal.wav'


def plot_data(pcm_data, samplerate, current_time):
    x_axis = np.arange(0, len(pcm_data) - 1) / len(pcm_data) * samplerate
    complex_data = [x+0j for x in pcm_data]
    result = np.fft.fft(complex_data)
    length = len(pcm_data) // 2
    amplitudes = [math.sqrt(x.imag * x.imag + x.real * x.real) for x in result[:length]]
    plt.plot(x_axis[:length], amplitudes)
    plt.title('{}s sample count: {}'.format(current_time, len(pcm_data)))
    plt.xlabel('{}Hz'.format(samplerate))
    plt.show()


def baz():
    data, samplerate = sf.read(_audio_path, dtype='int16')
    window = 512
    total_number_of_data = len(data)
    current_index = 0 # 144000
    while current_index < total_number_of_data:
        d = data[current_index:current_index+window]
        current_time = current_index / samplerate
        print('current time: {}'.format(current_index / samplerate))
        plot_data(d, samplerate, current_time)
        current_index += window


if __name__ == '__main__':
    baz()

我对DSP不熟悉,也没有尝试过。所以我认为我的代码有一些错误,请帮助,谢谢。

这是我的声音文件sound file

您在第一个图中看到的这个高值是由 window 中的常量分量引起的。尝试归一化:将所有 window 的值平移其平均值。

尾部零点只是振幅小到看起来像零点。检查它们的值以确保 ;)