def calculateFFT(self, duration, framerate, sample):
        """
            Calculates FFT for a given sound wave. 
            Considers only frequencies with the magnitudes higher than
            a given threshold.
        """

        fft_length = int(duration * framerate) # 216090
        #fft_length = int(2**17)
        
        '''
        # numpy.fft is very slow if fft length is a prime number.
        # TODO: Fix this problem.
        while is_Prime(fft_length):
            print 'PRIME!!!'
            fft_length -= 1
        FFT = numpy.fft.fft(sample, n=fft_length)
        '''
        fft_length = get_next_power_2(fft_length)
        FFT = numpy.fft.fft(sample, n=fft_length)

        ''' ADJUSTING THRESHOLD '''
        
        threshold = 0
        power_spectra = []
        frequency_bin_with_max_spectrum = 0
        for i in range(len(FFT) / 2):
            power_spectrum = scipy.absolute(FFT[i]) * scipy.absolute(FFT[i])
            if power_spectrum > threshold:
                threshold = power_spectrum
                frequency_bin_with_max_spectrum = i
            power_spectra.append(power_spectrum)
        max_power_spectrum = threshold
        threshold = float(self.avg_noise_power) - 3 * float(self.std_noise_power)

        binFrequencies = []
        magnitudes = []
        binResolution = float(framerate) / float(fft_length)
        sum_of_significant_spectra = 0
        # For each bin calculate the corresponding frequency.
        for k in range(len(FFT)):
            binFreq = k * binResolution
            
            # Truncating the FFT so we consider only hearable frequencies.
            if binFreq > self.maxFreqConsidered:
                FFT = FFT[:k]
                break
            elif binFreq > self.minFreqConsidered:
                # Consider only the frequencies with magnitudes higher than the threshold.
                power_spectrum = power_spectra[k]
                if power_spectrum > threshold:
                    magnitudes.append(power_spectrum)
                    binFrequencies.append(binFreq)
                    
                    # Sum all significant power spectra except the max power spectrum.
                    if power_spectrum != max_power_spectrum:
                        sum_of_significant_spectra += power_spectrum
            
            #binFrequencies.append(binFreq)
        significant_freq = 0.0
        
        if max_power_spectrum > sum_of_significant_spectra:
            significant_freq = frequency_bin_with_max_spectrum * binResolution
        
        maxFreq = len(FFT) / duration  # Max. frequency considered after truncating.
                                       # maxFreq = rate without truncating.
        return (FFT, binFrequencies, maxFreq, magnitudes, significant_freq)

    # Code for STFT taken from:
    # http://stackoverflow.com/questions/2459295/stft-and-istft-in-python
dataset/ETHPy150Open Agerrr/Automated_Music_Transcription/least_squares_first_peaks_2.py/MIDI_Detector_Least_Squares_2.calculateFFT/original