Sahan De Silva
Sahan De Silva

Reputation: 471

Best Buffer Size

What is the best value for buffer size when implementing a guitar tuner using FFT? Am getting an output, but it seems that the value displayed is not much accurate as I expected. I think it's an issue with the buffer size I allocated. I'm using 8000 as the buffer size. Are there any other suggestions to retrieve more efficient result?

Upvotes: 1

Views: 630

Answers (1)

Hounshell
Hounshell

Reputation: 5459

You can kinda wiggle the results around a bit. It's been a while since I've done FFT work, but if I recall, with a buffer of 8000, the Nth bucket would be (8000 / 2) / N Hz (is that right? It's been a long time). So the 79th through 81st buckets are 50.63, 50, and 49.38 Hz.

You can then do a FFT with a slightly different number of buckets. So if you dropped down to 6000 buckets, the 59th through 61st buckets would be 50.84, 50, and 49.18 Hz.

Now you've got an algorithm that you can use to home in on the specific frequency. I think it's O((log M) * (N log N)) where N is roughly the number of buckets you use each time, and M is the precision.

Update: Sample Stretching

public byte[] stretch(byte[] input, int newLength) {
  byte[] result = new byte[newLength];
  result[0] = input[0];

  for (int i = 1; i < newLength; i++) {
    float t = i * input.length / newLength;
    int j = (int) t;
    float d = t - j;

    result[i] = (byte) (input[j - 1] * d + input[j] * (1 - d))
  }

  return result;
}

You might have to fix some of the casting to make sure you get the right numbers, but that looks about right.

i = index in result[] j = index in input[] (rounded up) d = percentage of input[j - 1] to use 1 - d = percentage of input[j] to use

Upvotes: 2

Related Questions