2017-05-08 6 views
-1

私は流体シミュレーションから速度の大きさデータと渦度データを持つファイルを持っています。非常に騒々しいデータのためのPython高速フーリエ変換

私は、これらの2つのデータセットの頻度を調べたいと思います。

私のコード:

# -*- coding: utf-8 -*- 
""" 
Spyder Editor 

This is a temporary script file. 
""" 
import re 
import math 
import matplotlib.pyplot as plt 

import numpy as np 


probeU1 = [] 
probeV1 = [] 
# this creates an array containig all the timesteps, cutting of the first 180, because the system has to stabelize. 
number = [ round(x * 0.1, 1) for x in range(180, 301)] 

# this function loops over the different time directories, and reads the velocity file. 
for i in range(len(number)): 
    filenamepath = "/Refinement/Vorticity4/probes/" +str(number[i]) + "/U" 
    data= open(filenamepath,"r") 
    temparray = [] 
    #removes all the formatting around the data 
    for line in data: 
     if line.startswith('#'): 
      continue 
    else: 
     line = re.sub('[()]', "", line) 
     values = line.split() 
     #print values[1], values[2] 
     xco = values[1::3] 
     yco = values[2::3]   
     #here it extracts all the velocity data from all the different probes 
     for i in range(len(xco)): 

      floatx = float(xco[i]) 
      floaty = float(yco[i]) 
      temp1 = math.pow(floatx,2) 
      temp2 = math.pow(floaty,2) 
      #print temp2, temp1 
      temp3 = temp1+temp2 
      temp4 = math.sqrt(temp3) 
      #takes the magnitude of the velocity 
      #print temp4 
      temparray.append(temp4) 
     probeU1.append(temparray) 

#   
#print probeU1[0] 
#print len(probeU1[0])  
#   

# this function loops over the different time directories, and reads the vorticity file. 
for i in range(len(number)): 
    filenamepath = "/Refinement/Vorticity4/probes/" +str(number[i]) + "/vorticity" 
    data= open(filenamepath,"r") 
# print data.read() 
    temparray1 = [] 

    for line in data: 
     if line.startswith('#'): 
      continue 
    else: 
     line = re.sub('[()]', "", line) 
     values = line.split() 
     zco = values[3::3] 
     #because the 2 dimensionallity the z-component of the vorticity is already the magnitude 
     for i in range(len(zco)): 
      abso = float(zco[i]) 
      add = np.abs(abso) 

      temparray1.append(add) 

    probeV1.append(temparray1) 

#Old code block to display the data and check that it made a wave pattern(which it did) 
##Printing all probe data from 180-300 in one graph(unintelligible) 
#for i in range(len(probeU1[1])): 
# B=[] 
# for l in probeU1: 
#  B.append(l[i]) 
## print 'B=', B 
## print i 
# plt.plot(number,B) 
# 
# 
#plt.ylabel('magnitude of velocity') 
#plt.show() 
# 
##Printing all probe data from 180-300 in one graph(unintelligible) 
#for i in range(len(probeV1[1])): 
# R=[] 
# for l in probeV1: 
#  R.append(l[i]) 
## print 'R=', R 
## print i 
# plt.plot(number,R) 
# 
# 
#plt.ylabel('magnitude of vorticity') 
#plt.show() 

#Here is where the magic happens, (i hope) 
ans=[] 
for i in range(len(probeU1[1])): 
    b=[] 
    #probeU1 is a nested list, because there are 117 different probes, wich all have the data from timestep 180-301 
    for l in probeU1: 
     b.append(l[i]) 
     #the freqeuncy was not oscillating around 0, so moved it there by substracting the mean 
     B=b-np.mean(b) 
     #here the fft happens 
     u = np.fft.fft(B) 
     #This should calculate the frequencies? 
     freq = np.fft.fftfreq(len(B), d= (number[1] - number[0])) 
     # If im not mistakes this finds the peak frequency for 1 probe and passes it another list 
     val = np.argmax(np.abs(u)) 
     ans.append(np.abs(freq[val]))  

     plt.plot(freq, np.abs(u)) 

#print np.mean(ans) 
plt.xlabel('frequency?') 
plt.savefig('velocitiy frequency') 
plt.show() 

# just duplicate to the one above it 
ans1=[] 
for i in range(len(probeV1[1])): 
    c=[] 

    for l in probeU1: 
     c.append(l[i]) 
     C=c-np.mean(c) 
     y = np.fft.fft(C) 
     freq1 = np.fft.fftfreq(len(C), d= (number[1] - number[0])) 
     val = np.argmax(np.abs(y)) 
     ans1.append(np.abs(freq1[val]))  

     plt.plot(freq1, np.abs(y)) 

#print np.mean(ans1) 
plt.ylabel('frequency?') 
plt.savefig('vorticity frequency') 
plt.show()  



data.close() 

私のデータは、速度の大きさデータの独自の121点を有する117個のプローブそれぞれが含まれています。

私の目的は、各プローブの優位な周波数を見つけて、それらのすべてを収集してヒストグラムにプロットすることです。

私の質問は、これがマジックが起こる場所であると言う部分についてです。私はfftが既に正しく動作していると信じています

y = np.fft.fft(C) 
      freq1 = np.fft.fftfreq(len(C), d= (number[1] - number[0])) 

私が間違っていないのであれば、freq1リストには特定のプローブのすべての周波数が含まれているはずです。私はこのリストを視覚的にチェックしました。異なる周波数の量は非常に高く(20+)、信号はおそらく非常に騒々しいです。

# If im not mistakes this finds the peak frequency for 1 probe and passes it another list 
     val = np.argmax(np.abs(u)) 
     ans.append(np.abs(freq1[val]))  

この部分は、理論上、1つのプローブからの最大信号を「ans」リストに入れるべきであることを確認してください。しかし、私はちょうど正しい周波数を正確に特定できない方法についてちょっと混乱しています。理論は1つの主な周波数でなければならない。どのようにしてすべてのノイズからこのデータすべてから「主な」周波数を正しく推定できますか?

参考のため、私はVon Karmannボルテックスストリートをモデリングしています。 https://en.wikipedia.org/wiki/K%C3%A1rm%C3%A1n_vortex_street

誰でもこの問題の解決方法を教えてください。

答えて

0

ライン

freq1 = np.fft.fftfreq(len(C), d= (number[1] - number[0])) 

のみ

alphaのように計算された基本的な波数である
freq[i] = freq1[i]*alpha 

としてあなたの周波数配列を計算するのに有用である

freq1 = [0, 1, ..., len(C)/2-1,  -len(C)/2, ..., -1]/(d*len(C)) 

行くからインデックスを生成

alpha = 1/Ts 

あなたのサンプリング期間はTsです。私は、freq1はスケールされていないので、周波数の配列が非常に高いと思います。

異なるタイムステップを使用してデータをサンプリングする場合は、等間隔スペースドメインでnumpy.interp(たとえば)を使用してデータを補間する必要があります。

主な頻度を推定するには、fft変換された変数が高いインデックスを見つけ、そのインデックスをfreq[i]に関連付けるだけです。

関連する問題