0

The peak suggestion, in the comments, does not find the peak that occurs most often. I need to find the frequency that occurs most often.

I need to find the dominant frequency in my Coefficient of Lift data. The frequency I am getting with the following code is quite large and not the dominant frequency. I know because the 2-D analysis is easy to analyze with a graph. It is sinusoidal. By dominant frequency, I mean the frequency of the signal with the most repeats.

#1/usr/bin/env python

import sys
import numpy
from numpy import sin
from math import pi

print("Hello World!")

data = numpy.loadtxt('CoefficientLiftData.dat', usecols= (0,3)) 
n = data.size
timestep = 0.000005
The peak data suggestion, in the comments, does not provide a count method to find how often a frequency occurs. 

print(data)
fourier = numpy.fft.fft(data)
frequencies = numpy.fft.fftfreq(n, d=timestep) 
positive_frequencies = frequencies[numpy.where(frequencies >= 0)] 
magnitudes = abs(fourier[numpy.where(frequencies >= 0)])

peak_frequency = numpy.argmax(magnitudes)
print(peak_frequency)
Chris Harding
  • 27
  • 1
  • 8
  • 1
    Does this answer your question? [Peak-finding algorithm for Python/SciPy](https://stackoverflow.com/questions/1713335/peak-finding-algorithm-for-python-scipy) – Basj Dec 04 '20 at 20:06
  • I still can't figure it out with your info. Sorry. – Chris Harding Dec 04 '20 at 20:21
  • 1
    `np.argmax` will tell you the _index_ of the maximum value in the `magnitudes` array. You need to use that index on the `positive_frequencies` array to get the frequency at which the magnitude is max. – Pranav Hosangadi Dec 04 '20 at 20:26
  • @Pranav: I'm sorry, I don't follow what you are saying. – Chris Harding Dec 04 '20 at 20:30
  • You mean the main frequency which has harmonics? – eventHandler Dec 04 '20 at 20:32
  • [Here's a tutorial on lists in python](https://www.tutorialspoint.com/python/python_lists.htm). It should give you an idea of what "index" means in the context of a list/array. A numpy array is essentially a list that can contain only one data type, but the concept of index applies here too. Take a look at the [documentation for `np.argmax()`](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html): it returns the index where the given array has a maximum value. – Pranav Hosangadi Dec 04 '20 at 20:35
  • If you write the values in `positive_frequencies` and `magnitudes` next to each other, you get a table of frequency and magnitude of the signal at that frequency. The dominant frequency is the frequency with the largest magnitude. So you need to find which _index_ of `magnitudes` contains the maximum value of `magnitudes`. Then you need to look up that same index in `positive_frequencies` to get the frequency corresponding to that (maximum) magnitude. This is the answer you want. Hope this is clear. If you don't follow, can you tell me _what specifically_ you didn't follow? @ChrisHarding – Pranav Hosangadi Dec 04 '20 at 20:37
  • _" I need to find the peak that occurs most often"_ You'll hardly ever have the exact same magnitude for multiple frequencies, so I'm not sure what you expect to get with this. It is definitely not the dominant frequency (which will be the frequency of vortex shedding or whatever physical phenomenon is causing the oscillations of C_L). Maybe make a histogram and take the one with the maximum count? – Pranav Hosangadi Dec 04 '20 at 21:13
  • In 2-D, the Coefficient of Lift data looks sinusoidal. It has minimum and maximum peeks during vortex shedding. The distance, in seconds, from one maximum peak to the neighbor is the period. frequency = 1/T. So, I need the period (T) that happens the most often, which would be the dominating period, and dominating frequency. – Chris Harding Dec 04 '20 at 21:27
  • I have asked the person who helped me at cfd-online to explain what he meant by dominating frequency. – Chris Harding Dec 04 '20 at 22:31
  • 1
    @ChrisHarding, You should read about Fourier transforms: they transform a signal from the time domain into the frequency domain, so from a C_L vs time plot, you get a magnitude vs. frequency plot. Considering the C_L vs. time plot is the addition of a number of sine waves `A0 * sin(w0 * t) + A1 * sin(w1 * t) + ...` and so on, so the FFT plots `w0, w1, ...` on the X axis and `A0, A1, ...` on the Y axis. – Pranav Hosangadi Dec 05 '20 at 00:32
  • 1
    Now, the sine wave with the largest amplitude is the one that dominates the resultant waveform, and the frequency of this wave is called the dominant frequency. When you look at the FFT result (or the `A*` vs `w*` plot), the index of the wave with max amplitude is `i = numpy.argmax(magnitudes)`, and the frequency of this sine wave is `positive_frequencies[i]`. – Pranav Hosangadi Dec 05 '20 at 00:36

1 Answers1

0

Here is how to extract the dominate frequency from Coefficient of Lift data from, as an example, OpenFOAM.

#1/usr/bin/env python

import sys
import numpy as np
import scipy.fftpack as fftpack
from numpy import sin
from math import pi
import matplotlib.pyplot as plt
print("Hello World!")

N = 2500
Nev = 1000

data = np.loadtxt('CoefficientLiftData.dat', usecols= (0,3)) 

times = data[:,0]
length = int(len(times)/2)

forcez= data[:,1]
t = np.linspace(times[length], times[-1], 2500) 
forcezint = np.interp(t, times, forcez) 

fourier = fftpack.fft(forcezint[Nev-1:N-1])
frequencies = fftpack.fftfreq(forcezint[Nev-1:N-1].size, d=t[1]-t[0]) 
#print(frequencies)
freq = frequencies[np.argmax(np.abs(fourier))]


print(freq)
Chris Harding
  • 27
  • 1
  • 8