# Android: fundamental frequency

I want to find the fundamental frequency for human voice in an Android Application. I want to use autocorrelation method. My code is this:

public double calculateFFT(double[] signal)

{
final int mNumberOfFFTPoints =1024;

double[] magnitude = new double[mNumberOfFFTPoints/2];
DoubleFFT_1D fft = new DoubleFFT_1D(mNumberOfFFTPoints);
double[] fftData = new double[mNumberOfFFTPoints*2];
double max_index=-1;
double max_magnitude=-1;

final float sampleRate=44100;
double frequency;

for (int i=0;i<mNumberOfFFTPoints;i++){

//fftData[2 * i] = buffer[i+firstSample];
fftData[2 * i] = signal[i];  //da controllare
fftData[2 * i + 1] = 0;

fft.complexForward(fftData);
}

for(int i = 0; i < mNumberOfFFTPoints/2; i++){

magnitude[i]=Math.sqrt((fftData[2*i] * fftData[2*i]) + (fftData[2*i + 1] * fftData[2*i + 1]));

if (max_magnitude<magnitude[i]){
max_magnitude=magnitude[i];
max_index=i;
}
}
return frequency=sampleRate*(double)max_index/(double)mNumberOfFFTPoints;

}


I used "edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;" Is my "return" the value of fundamental frequency or not?

thanks

I changed my code in this mode:

    public double calculateFFT(double[] signal)

{

final int mNumberOfFFTPoints =1024;
double[] magnitude = new double[mNumberOfFFTPoints/2];
DoubleFFT_1D fft = new DoubleFFT_1D(mNumberOfFFTPoints);
double[] fftData = new double[mNumberOfFFTPoints*2];

double max_index=-1;
double max_magnitude=-1;

final float sampleRate=44100;
double frequency;

for (int i=0;i<mNumberOfFFTPoints;i++){

fftData[2 * i] = signal[i];
fftData[2 * i + 1] = 0;

}

fft.complexForward(fftData);

for(int i = 0,j=0; i < mNumberOfFFTPoints/2; i += 2,j++){

magnitude[j]=Math.sqrt((fftData[2*i] * fftData[2*i]) + (fftData[2*i + 1] * fftData[2*i + 1]));

}
fft.complexInverse(fftData, false);

for(int i=0;i<magnitude.length;i++)

if (max_magnitude<magnitude[i]){
max_magnitude=magnitude[i];
max_index=i;
}

return frequency=sampleRate*(double)max_index/(double)mNumberOfFFTPoints;

}


in this mode I calculate autocorrelation thanks inverse transform "fft.complexInverse(fftData, false);"; isn't it? And also in this case isn't the "return" the fundamental frequency?

• This is a slightly harder problem than you may think. – Aaron May 26 '14 at 16:59
• Duplicate - cross-posted on StackOverflow: stackoverflow.com/questions/23867867/… - this question has been posted by the OP three times on StackOverflow and now once here, and OP does not seem to be taking any notice of the helpful answers and comments already posted in response to the previous iterations of his questions. – Paul R May 27 '14 at 9:19