0

I am currently working on a project for my 2nd year. I am supposed to code in java a tuner. I have chosen to do a guitar tuner.

After looking around on the internet, I found a java code to do a FFT. I changed it a bit, understood it and have tested it. I know it works fine (i made a graph of it and looked at the different peaks using simple sines functions).

I am now trying to find the fundamental frequency. From what I understand, this frequency is given by the first peak.

I would thus like to create a method that finds for instance the first 5 peaks of my FFT and gives them to me with their indexes.

I first did a simple method where I compared two by two each point of my spectrogram and when the sign changed that's where I knew there was a peak. This method works great with ideal signals (without any noise). However it becomes completely useless if I add noise.

I am really bad in java (I actually started with this project and basically the simple function I described above is my master piece.... just so you get an idea of my level).

Can anyone help me? I would really appreciate it! :) Thanks in advance!

Have a great day!

fireangel

fireangel3000
  • 11
  • 2
  • 5
  • Pitch is not FFT peak frequency. The Pitch frequency might not be the first peak or any peak. Especially for the sounds recorded from the low strings of a guitar. – hotpaw2 May 05 '12 at 02:04
  • Possible dup of: http://stackoverflow.com/questions/8699360/audio-analysis-frequency-vs-pitch – hotpaw2 May 05 '12 at 02:10
  • Thank you hotpaw2 for you answer. I have come up with the notion of pitch also. From what I understood, pitch is linked to the frequency so I thought that finding the frequency could get me the pitch. – fireangel3000 May 05 '12 at 10:36

1 Answers1

0

I'd say your best bet is going to be to read in all the values as an array, then run over them and 'smooth' them using a rolling average of some kind.

Afterwards, you'll have a much smoother curve. Find your peaks using this curve, then go back to your original data and use the peak indexes to find the actual peak there.

pseudocode:

// Your raw data
int[] data = getData();

// This is an array to hold your 'smoothed' data
int[] newData = new int[data.length]; 

// Iterate over your data, smooth it, and read it into your smoothed array
for (i <  data.length) {
    newData[i] = (data[i-2] + data[i-1] + data[i] + data[i+1] + data[i+2]) / 5;
}

// Use your existing peak finding function on your smoothed data, and get 
// another array of the indexes your peaks occur.
int[] peakIndexes = yourPeakFindingFunction(newData);

// Create an array to hold your final values.
int[] peakValues = new int[peakIndexes.length];

// Iterate over your peak indexes and get the original data's value at that location.
for(i < peakIndexes.length) {
    peadValues[i] = data[peakIndexes[i]];
}

Very basic and very brute-force, but it should get you on the right track for an assignment.

You'll need to play with the algorithms for smoothing the data so it's representative and for finding the actual peak at the location indicated by the smoothed data (as it won't be exact).

Charles
  • 245
  • 2
  • 11
  • Hello Charles.Thanks a lot for your answer. If I understand correctly, the smoothing you do is by taking the average value of 5 values of my raw data. Like this all the little peaks coming from noise will diminish because of one real peak. Is that correct? However, what I don't understand, is that after if I look for the first peak, I will get the index of a peak that does not correspond to the index of my real peak. And I dont see how to get back the real one.... – fireangel3000 May 05 '12 at 10:40
  • Basically, what you're doing is just averaging out the little spikes, meaning that the smoothed line will represent a general trend: flat, upward, downward, peak, etc. The issue could be that if there's a severe downward or upward spike on either side of your 'real' peak in your original data, that could skew the average one way or the other, artificially moving the peak a bit in either direction. You'll have to play with the algorithm to reduce the effects of those noise peaks. – Charles May 07 '12 at 16:48
  • Thanks for the answer Charles. I actually took a complete new approach by using Harmonic Product Spectrum and now it works fine. THanks a lot for your help! – fireangel3000 May 18 '12 at 17:29