简体   繁体   中英

How to plot discrete fourier graph of frequency spectrum

I am trying to plot the frequnecy spectrum of my wav file data. I get a list of complex numbers and by testing with smaller numbers, I know the values are correct. But, I don't know how to plot. I just know I am supposed to get something like small barcharts, that are supposed be symmetrical. But my graph it's not. Below is my code and current plot.

from scipy.io.wavfile import read
import numpy as np
import matplotlib.pyplot as plt
import math

df = read('matches-4.wav')
data = np.array(df[1], dtype=float)[:, 1][2000:2512]  # using right channel


def discrete_fourier_transformation():
    c = []
    for k in data:
        summation = 0
        for j in data:
            summation += j * math.e ** (-1j * (2 * math.pi / len(data)) * j * k)
        c.append(abs(1 / len(data) * summation))
    return c


values = discrete_fourier_transformation()
plt.plot(values)
plt.show()

在此处输入图片说明

You are not getting the expected symmetry out of your graph because of a problem in your implementation of the Discrete Fourier Transformation.

More specifically, in your Discrete Fourier Transformation implementation the complex exponential should include multiplication by integer factors, which I imagine is what you tried to do with j and k . However the way you configured your loops the j and k values are the actual data values. To fix this you should update your loops to go over the range(0,len(data) range with the following:

def discrete_fourier_transformation():
    c = []
    for k in range(0,len(data)):
        summation = 0
        for j in range(0,len(data)):
            summation += data[j] * math.e ** (-1j * (2 * math.pi / len(data)) * j * k)
        c.append(abs(1 / len(data) * summation))
    return c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM