简体   繁体   English

如何绘制频谱的离散傅立叶图

[英]How to plot discrete fourier graph of frequency spectrum

I am trying to plot the frequnecy spectrum of my wav file data. 我正在尝试绘制wav文件数据的频率频谱。 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 . 更具体地说,在您的离散傅立叶变换实现中,复指数应包括乘以整数因子,我想这就是您尝试对jk However the way you configured your loops the j and k values are the actual data values. 但是,您配置循环的方式jk值为实际数据值。 To fix this you should update your loops to go over the range(0,len(data) range with the following: 要解决此问题,您应该使用以下命令更新循环以使其超出range(0,len(data)范围:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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