繁体   English   中英

实验数据向频域的傅里叶变换

[英]Fourier transformation of experimental data into frequency domain

我对python完全陌生。 我已经进行了波浪数据测试实验。 我有可用的时间序列数据。 如何在频域中证明这一点? 有什么我可以参考的例子吗? 我想出了一个如下所示的程序,但是它似乎没有用。 请帮忙。

#Program for Fourier Transformation
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt

def readdat( filename ):
    """
        Reads sectional area curve data from file filename
    """

    # read all lines of input files
    fp = open( filename, 'r')
    lines = fp.readlines() # to read the tabulated data
    fp.close()

    # interpret data
    time = []
    ampl = []
    for line in lines:
        if line[0:1] == '#':
            continue # ignore comments in the file
        try:
            time.append(float(line.split()[0])) #first column is time
            ampl.append(float(line.split()[1])) # second column is corresponding amplitude
        except:
            # if the data interpretation fails..
            continue
    return np.asarray(time), np.asarray(ampl)

if __name__ == '__main__':

    time, ampl = readdat( 'data.dat')
    print time
    print ampl

spectrum = fft.fft(ampl)
freq = fft.fftfreq(len(spectrum))
print freq

对程序进行最小限度的校正以得出一些结果是这样的

#Program for Fourier Transformation
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt

def readdat( filename ):
    """
        Reads sectional area curve data from file filename
    """

    # read all lines of input files
    fp = open( filename, 'r')
    lines = fp.readlines() # to read the tabulated data
    fp.close()

    # interpret data
    time = []
    ampl = []
    for line in lines:
        if line[0:1] == '#':
            continue # ignore comments in the file
        try:
            time.append(float(line.split()[0])) #first column is time
            ampl.append(float(line.split()[1])) # second column is corresponding amplitude
        except:
            # if the data interpretation fails..
            continue
    return np.asarray(time), np.asarray(ampl)

if __name__ == '__main__':

    time, ampl = readdat( 'data.dat')
    print time
    print ampl

spectrum = fft.fft(ampl)
timestep = time[1]-time[0] # assume samples at regular intervals
freq = fft.fftfreq(len(spectrum),d=timestep)
freq=fft.fftshift(freq)
spectrum = fft.fftshift(spectrum)
plt.figure(0,figsize=(5.0*1.21,5.0))
plt.plot(freq,spectrum)
print freq
plt.xlabel("frequencies")
plt.ylabel("spectrum")
plt.savefig("/tmp/figure.png")

暂无
暂无

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

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