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

时间:2018-11-26 08:52:37

标签: python signal-processing fft wav

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

声音文件几乎安静,并在3s时发出敲门声。

我注意到接近0的值非常高。但是怎么可能! 另一个奇怪的点是“当频率大于大约16000时,该值为0”。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

您在第一张图上看到的高值是由窗口中的常量分量引起的。尝试规范化:将所有窗口的值均移其平均值。

尾部零点的振幅足够小,看起来像零点。检查它们的值以确保;)