[英]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.