我花了很多时间研究 FFT。我对使用 KISSFFT 特别感兴趣,因为它是一个非常便携的 C 实现。
我仍然不清楚如何将 i[x] 和 r[x] 转换为频率仓的幅度。所以创建了一个有符号的 int 16 版本的 sin。我有 512 个正弦波样本。我希望看到一个带有数据的 Bin,而其余的则为零。不是这样...
这是我的代码...
- (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)););
}
}
我得到的结果看起来像这样......
***** ************************ ****************************** ************************ ************************ ************************ ****************************** ************************ ***** ************************ ****************************** ************************ ***************** ************************ ****************************** ************************ ***** ************************ ****************************** ************************ ************************ ************************ ****************************** ************************