我花了不少时间研究FFT。我特别感兴趣的是使用KISSFFT,因为它是一个非常便携的C实现。
我仍然不清楚如何将i [x]和r [x]转换为频率仓的幅度。因此创建了一个签名的int 16版本的sin。我有512个我的sin波样本。我希望看到一个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)););
}
}
我得到的结果看起来像这样......
***** ********************* **************************** ********************* ************************ ********************* **************************** ********************* ***** ********************* **************************** ********************* ***************** ********************* **************************** ********************* ***** ********************* **************************** ********************* ************************ ********************* **************************** *********************
答案 0 :(得分:2)
首先进行一些编程更改:
xAxis += pieSteps;
if (xAxis >= fullSinWave)
xAxis -= fullSinWave; //wrap x back into 0-2pi period
将有助于减少数字错误。
in_buf[i].r = dataStream[i];
in_buf[i].i = 0;
会将输入缓冲区设置为sin(x)
,之前您已将其设置为sin(x) + j*sin(x)
,其中j = sqrt(-1)
。
将wantedHz = 1000;
移出循环看起来更好。
还有一个更基本的问题:你设置wantedHz = 1000
。采样率为44.1 kHz时,这相当于44100 points/sec * (1/1000) sec/cycle = 44.1 points/cycle
。使用512点的缓冲区,您将在缓冲区中获得11.6个正弦波周期。非整数周期导致leakage。
在开始之前,尝试设置wantedHz = 12*44100.0/512
以在缓冲区中准确地给出12个周期。你应该在变换中看到两个尖峰:一个在索引12处,一个在索引511-12处。
您会看到两个峰值的原因是sin(w_0*x)
的转换为j*{-delta(w-w_0) - delta(w+w_0)}
。也就是说,在变换的虚部中,您在w_0和-w_0处获得脉冲函数。他们在这些地方的原因是变换从0变为2 * pi。
执行此操作后,返回wantedH = 1000
,在缓冲区中给出非整数个循环。您应该看到一个宽大的帐篷形状的结果,以11和511-11为中心。您应该将dataStream
乘以窗口函数(Hann是好的)以减少此效果的影响。
答案 1 :(得分:2)
我也一直在与这个库作斗争,这段代码可以帮助你测试。这是我在互联网上阅读的代码混合,看看在项目中包含它是否有趣。工作良好。它写入一个文件,其中包含原始信号的波形和值,FFT和反向FFT也只是为了测试。它是用VS2010编译的
#include "kiss_fft.h"
#include "tools\kiss_fftr.h"
#include <stdio.h>
#include <conio.h>
#define numberOfSamples 1024
int main(void)
{
struct KissFFT
{
kiss_fftr_cfg forwardConfig;
kiss_fftr_cfg inverseConfig;
kiss_fft_cpx* spectrum;
int numSamples;
int spectrumSize;
} fft;
static double dospi = 3.14159265*2;
static double sampleRate = 44100;
static double wantedHz = 0;
int j,i,k;
float dataStream[numberOfSamples];
float dataStream2[numberOfSamples];
float mags[numberOfSamples];
FILE * pFile;
//Frequency to achive
wantedHz = 9517;
fft.forwardConfig = kiss_fftr_alloc(numberOfSamples,0,NULL,NULL);
fft.inverseConfig = kiss_fftr_alloc(numberOfSamples,1,NULL,NULL);
fft.spectrum = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * numberOfSamples);
fft.numSamples = numberOfSamples;
fft.spectrumSize = numberOfSamples/2+1;
pFile = fopen ("c:\\testfft.txt","w");
//filling the buffer data with a senoidal wave of frequency -wantedHz- and printing to testing it
for (j = 0; j < numberOfSamples; j++) {
dataStream[j] = 32768*(sin(wantedHz*dospi*j/sampleRate));
//Draw the wave form
for (k=-64;k<(int)(dataStream[j]/512);k++) fprintf(pFile," ");
fprintf(pFile,"*\n");
}
//spectrum
kiss_fftr(fft.forwardConfig, dataStream, fft.spectrum);
//inverse just to testing
kiss_fftri( fft.inverseConfig, fft.spectrum, dataStream2 );
for(i=0;i<fft.spectrumSize;i++) {
mags[i] = hypotf(fft.spectrum[i].r,fft.spectrum[i].i);
fprintf(pFile,"[Sample %3d] ORIGINAL[%6.0f] -SPECTRUM[%5dHz][%11.0f]",i,dataStream[i],i*(int)sampleRate/numberOfSamples,mags[i]);
dataStream2[i] = dataStream2[i] / (float)fft.numSamples;
fprintf(pFile," -INVERSE[%6.0f]\n",dataStream2[i]);
}
//end
//free and close
fclose (pFile);
kiss_fft_cleanup();
free(fft.forwardConfig);
free(fft.inverseConfig);
free(fft.spectrum);
getch();
return 0;
}