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

Python 试图将PCM转换为频率图,但结果在0附近看起来非常奇怪,python,signal-processing,fft,wav,Python,Signal Processing,Fft,Wav,我尝试将PCM数据从wav文件和FFT转换为频率图。 这是我的图表。 0.00s 512样本计数 3.15s 512样本计数 声音文件几乎是安静的,在3s开始时有一些敲击声 我注意到接近0的值非常高。但这是怎么回事! 另一个奇怪的点是“当频率大于16000左右时,该值为0” 这是我的密码: import soundfile as sf import numpy as np import math import matplotlib.pyplot as plt _audio_path = 's

我尝试将PCM数据从wav文件和FFT转换为频率图。 这是我的图表。 0.00s 512样本计数 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()
将声音文件导入为sf
将numpy作为np导入
输入数学
将matplotlib.pyplot作为plt导入
_音频路径='source\u normal.wav'
def绘图数据(pcm数据、采样器、当前时间):
x_轴=np.arange(0,len(pcm_数据)-1)/len(pcm_数据)*采样器
复数_数据=[x+0j表示pcm_数据中的x]
结果=np.fft.fft(复数数据)
长度=len(pcm_数据)//2
振幅=[结果中x的数学sqrt(x.imag*x.imag+x.real*x.real)[长度]]
plt.绘图(x_轴[:长度],振幅)
plt.title(“{}的样本计数:{}.”格式(当前时间,len(pcm_数据)))
plt.xlabel(“{}Hz.”格式(samplerate))
plt.show()
def baz():
数据,samplerate=sf.read(_audio_path,dtype='int16')
窗口=512
数据总数=len(数据)
当前指数=0#144000
当当前指数<数据总数时:
d=数据[当前索引:当前索引+窗口]
当前时间=当前指数/采样器
打印('当前时间:{}'。格式(当前索引/采样器))
绘图数据(d、采样器、当前时间)
当前索引+=窗口
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
baz()
我不熟悉DSP,以前从未尝试过。所以我认为我的代码有一些错误,请帮助,谢谢


这是我的声音文件

您在第一个图上看到的这个高值是由窗口中的常量分量引起的。尝试正常化:按窗口的平均值移动所有窗口的值

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