Get frequency with highest amplitude from FFT

Solution 1:

I suggest you step away from your code and first master ability to perform a fft call and make sense of the result returned from that call ... either read in a sin curve of known freq or just write a function to populate an array with a floating point sin curve ( this is your time domain signal ) ... then feed that array into a fft call which will typically return back to you a new array of complex numbers ... each element of this new array which is now in the frequency domain represents one frequency value ... a frequency bin ... the magnitude of that frequency can be calculated using

nyquist_limit_index := int(number_of_samples / 2)

curr_freq := 0.0
incr_freq := flow_data_spec.sample_rate / number_of_samples

for index, curr_complex := range complex_fft { 

    if index <= nyquist_limit_index  {

        curr_real = real(curr_complex) // pluck out real portion of imaginary number
        curr_imag = imag(curr_complex) // ditto for im

        curr_mag = 2.0 * math.Sqrt(curr_real*curr_real+curr_imag*curr_imag) / number_of_samples

        curr_theta = math.Atan2(curr_imag, curr_real) // phase shift of this freq

        curr_dftt := discrete_fft { // populate a struct of current array element

            real:      2.0 * curr_real,
            imaginary: 2.0 * curr_imag,
            magnitude: curr_mag,
            theta:     curr_theta,
        }

        //  optionally stow curr_dftt for later
    }

    curr_freq += incr_freq
}

where number_of_samples is just the length of your time domain array you fed into the fft call

Above code shows you how to iterate across the frequency domain array of complex numbers returned back to you from an earlier fft call ... above is pseudo code not python but your process could will be very similar

To identify the frequency ( curr_freq ) with the largest amplitude just keep track of which curr_freq had maximum magnitude in above loop ... In our toy setup you may well know the frequency of your source input sin curve so that same frequency should pop out as the curr_freq with the largest magnitude in above ... after you get this working and its concepts sink in then apply what you have learned to your task at hand - good luck

Fourier Analysis and its various incantations are extremely powerful and can open many doors. Its a topic which demands thinking, yet if we allow ourselves to simply plug some api calls together to get something working we have missed something very magical indeed