Hi. I’m trying to make sure that I know where certain audio frequencies are located when I load an audio clip.
The musical note A4 is 440Hz. If the frequencies of a clip with a sample rate of 44100Hz are split into 1024 bands, the 10th band should be closest to 440Hz (according to calculations given here). 10 * 44100/1024 = 430Hz
So I set up a typical frequency display, feeding in a 440Hz sine wave, but when I mark out the 10th band with a green line, I see that it’s not at the 440Hz frequency (loudest frequency). If I mark out the 20th band with a red line, I see that this is at the 440Hz line instead. Again, if I do the calculations for band to frequency, the 20th band is closest to 880Hz, which is A5, one octave up. So why am I getting this wrong?
I’ll show my code below:
import processing.sound.*;
SoundFile file;
FFT fft;
int bands = 1024;
float[] spectrum = new float[bands];
void setup() {
size(1024, 512);
file = new SoundFile(this, "sin440.wav");
file.play();
fft = new FFT(this, bands);
fft.input(file);
}
void draw() {
fft.analyze(spectrum);
for (int i = 0; i < bands; i++) {
stroke(0, 0, 0);
if (i == 2) {
stroke(0, 0, 255);
}
if (i == 5) {
stroke(0, 255, 255);
}
if (i == 10) {
stroke(0, 255, 0);
}
if (i == 20) {
stroke(255, 0, 0);
}
line( i, height, i, height - spectrum[i]*height*5 );
}
}
The 440Hz sine wave can be generated and downloaded here.
My guess is I’m making a very stupid mistake, but I’ve been trying to figure this out for an embarrassing amount of time, so I hand it over to you now.