ctype-conversionfftkissfft

Complex frequencies to real signal transform with kiss_fft


I am trying to FFT my complex signal and the output should be real. So I make this code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lib\kiss_fftr.c"

int N = 16;

void TestFftReal(const char* title, const kiss_fft_cpx  in[N/2+1], kiss_fft_scalar out[N/2+1])
{
    kiss_fftr_cfg cfg = kiss_fftr_alloc(N, 1/*is_inverse_fft*/, NULL, NULL);

    printf("%s\n", title);

    if (cfg != NULL)
    {
        size_t i;

        kiss_fftr(cfg, in, out);
        free(cfg);

        for (i = 0; i < N; i++)
        {
            printf("out[%2zu] = %+f ", i, out[i]);
            if (i < N/2+1 )
            {
                printf(" in[%2zu] = %+f, %+f  ", i, in[i].r, in[i].i);
            }
            printf("\n");
        }
    }
}
int main(void)
{
    kiss_fft_cpx In[N/2+1];
    kiss_fft_scalar Out[N];
    size_t i;

    for (i = 0; i < N; i++)
    {
        In[i].r = In[i].i = 0;
    }
    TestFftReal("Zeroes (real)", In, Out);

    for (i = 0; i < N; i++)
    {
        In[i].r = 1, In[i].i = 0;
    }
    TestFftReal("Ones (real)", In, Out);

    for (i = 0; i < N; i++)
    {
        In[i].r = sin(45 * i * (M_PI / 180)), In[i].i =0;
    }
    TestFftReal("SineWave (real)", In, Out);

    return 0;
}

But now I have a problem with the function kiss_fftr(). Should I use kiss_fft() ? The error says that in or out isn't the correct type, but on Git-Hub it says it should go so. https://github.com/berndporr/kiss-fft


Solution

  • At least this problem:

    Code attempts to assign outside In[] range in 3 different loops

    kiss_fft_cpx In[N/2+1];
    ...
    for (i = 0; i < N; i++)
    {
        In[i].r = In[i].i = 0;
    }
    

    Recommended alternative:

    for (i = 0; i < (N/2+1); i++)
    

    If kiss_fftr(cfg, in, out); is

    void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
    

    ... then that functions is going from kiss_fft_scalar to kiss_fft_cpx. OP's code has it the other way around.

     TestFftReal( ... const kiss_fft_cpx  in[N/2+1], kiss_fft_scalar out[N/2+1]) {
        kiss_fftr(cfg, in, out);
    

    Maybe OP wants kiss_fftri() instead of kiss_fftr()?

    void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)