Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13981

Python fast Fourier transform for very noisy data

$
0
0

I have a file with velocity magnitude data and vorticity magnitude data from a fluid simulation.

I want to find out what is the frequency for these two data sets.

my code:

# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import reimport mathimport matplotlib.pyplot as pltimport numpy as npprobeU1 = []probeV1 = []# this creates an array containing all the timesteps, cutting of the first 180, because the system has to stabilize.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 dimensionality 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, which all have the data from timestep 180-301    for l in probeU1:      b.append(l[i])      #the frequency was not oscillating around 0, so moved it there by subtracting 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('velocity frequency')plt.show()# just duplicate to the one above itans1=[]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()

My data contains 117 probes each having their own 121 point of velocity magnitude data.

My aim is to find the dominate frequency for each probe and then collect all those and plot them in a histogram.

My question is about the part where it says this is where the magic happens. I believe the fft is already working correctly

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

And if I'm not mistaken the freq1 list should contain all the frequencies for a given probe. I've checked this list visually and the amount of different frequencies is very high(20+) so the signal is probably very noisy.

# If I'm 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]))    

That this part should in theory take the biggest signal from one probe and than put in the "ans" list. But I'm a bit confused as to how I can no correctly identify the right frequency. As there should in theory be one main frequency. How can I correctly estimate the "main" frequency from all this data from all the noise

For reference I'm modeling an Von Karmann vortex street and I'm looking for the frequency of vortex shedding. https://en.wikipedia.org/wiki/K%C3%A1rm%C3%A1n_vortex_street

Can anyone help me on how to solve this?


Viewing all articles
Browse latest Browse all 13981

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>