I have been spending quite a bit of time studying FFT's. I am in particular interesting in using KISSFFT because it is a very portable C implementation.
I am still very unclear how to turn i[x] and r[x] into a frequency bin's amplitude. So created a signed int 16 version of sin. I have 512 samples of my sin wave. I expected to see one Bin with data and the rest at zero. Not so...
Here is my code...
- (IBAction)testFFT:(id)sender{
NSLog(@"testFFT");
static double xAxis = 0;
static int sampleCount = 0;
static double pieSteps;
static double fullSinWave = 3.14159265*2;
static double sampleRate = 44100;
static double wantedHz = 0;
int octiveOffset;
char * globalString = stringToSend;
SInt16 dataStream[512];
// Notes: ioData contains buffers (may be more than one!)
// Fill them up as much as you can. Remember to set the size value in each buffer to match how
// much data is in the buffer.
for (int j = 0; j < 512; j++) {
wantedHz = 1000;
pieSteps = fullSinWave/(sampleRate/wantedHz);
xAxis += pieSteps;
dataStream[j] = (SInt16)(sin(xAxis) * 32768.0);
NSLog(@"%d) %d", j, dataStream[j]);
}
kiss_fft_cfg mycfg = kiss_fft_alloc(512,0,NULL,NULL);
kiss_fft_cpx* in_buf = malloc(sizeof(kiss_fft_cpx)*512);
kiss_fft_cpx* out_buf = malloc(sizeof(kiss_fft_cpx)*512);
for (int i = 0;i < 512;i++){
in_buf[i].r = dataStream[i];
in_buf[i].i = dataStream[i];
}
kiss_fft(mycfg,in_buf, out_buf);
for (int i = 0;i < 256;i++){
ix = out_buf[i].i;
rx = out_buf[i].r;
printfbar(sqrt(ix*ix+rx*rx)););
}
}
I am getting results that look like this....
***** ********************* **************************** ********************* ************************ ********************* **************************** ********************* ***** ********************* **************************** ********************* ***************** ********************* **************************** ********************* ***** ********************* **************************** ********************* ************************ ********************* **************************** *********************