开发者

kiss FFT bin amplitude

开发者 https://www.devze.com 2023-03-09 06:42 出处:网络
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 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 开发者_C百科= 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....


*****
*********************
****************************
*********************
************************
*********************
****************************
*********************
*****
*********************
****************************
*********************
*****************
*********************
****************************
*********************
*****
*********************
****************************
*********************
************************
*********************
****************************
*********************


A couple of programming changes, first of all:

xAxis += pieSteps;
if (xAxis >= fullSinWave)
  xAxis -= fullSinWave; //wrap x back into 0-2pi period

will help reduce numeric error.

in_buf[i].r = dataStream[i];
in_buf[i].i = 0;

will set the input buffer to sin(x), previously you had it set to sin(x) + j*sin(x), where j = sqrt(-1).

Moving wantedHz = 1000; out of the loop looks better.

And a more fundamental issue: you set wantedHz = 1000. With a sample rate of 44.1 kHz this corresponds to 44100 points/sec * (1/1000) sec/cycle = 44.1 points/cycle. With a buffer of 512 points, you will get 11.6 cycles of the sine wave in the buffer. Non-integer cycles lead to leakage.

Before getting into this, though, try setting wantedHz = 12*44100.0/512 to give exactly 12 cycles in the buffer. You should see two spikes in the transform: one at index 12, and one at index 511-12.

The reason you'll see two spikes is that the transform of sin(w_0*x) is j*{-delta(w-w_0) - delta(w+w_0)}. That is, you get an impulse function at w_0 and -w_0 in the imaginary part of the transform. The reason they are at the places they are is that the transform goes from 0 to 2*pi.

After you do this, go back to wantedH = 1000, giving you a non-integer number of cycles in the buffer. You should see a wide tent-shaped result, centered around bins 11 and 511-11. You should multiply dataStream by a window function (Hann is good) to reduce the impact of this effect.


I have been fighting with this library too, and this code could help you to test. It´s a mixing of code that I read in Internet to see if It could be interesting to include in a project. Works Fine. It writes a file with the waveform and values of the original signal, FFT and the inverse FFT too just to test. It was compiled with 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;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消