Search code examples
pythonsignal-processingfftwav

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


I tried convert PCM data from wav file and FFT to frequency chart. Here is my chart. start from 0.00s 0.00s 512 sample count start from 3.15s 3.15s 512 sample count

The sound file almost quietly and have some knock sound start at 3s.

I noticed near 0 the value very high. But how it can be! Another strange point is "the value is 0 when frequency greater than about 16000".

Here is my code:

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()

I not familiar with DSP and never tried before. So I think my code have some mistake, please help, thank you.

here is my sound file sound file


Solution

  • This high value you see on the first plot is caused by the constant component in the window. Try normalization: shift all window's values by its average.

    Tail zeros are just amplitudes small enough to look like zeros. Check out their values to ensure ;)