1

The code below does an inverse 2D FFT of a 4x4 array of frequencies (set to harmonic numbers), followed by a forward FFT of the result. Scaling down the result, it should equal the original frequencies. Some of them do match, but some of them are clearly different. What am I doing wrong?

#include <stdio.h>
#include "kiss_fftndr.h"

// function to print FFT output
void print_freq(kiss_fft_cpx* in, int n0, int n1){

    printf("\n");
    for(int y = 0; y < n0; y++){
        for(int x = 0; x < n1; x++){
            printf(" (%f, %f) ", in[y*n1+x].r, in[y*n1+x].i);
        }
        printf("\n");
    }
}

int main(){

    int dims[2] = {4,4};
    kiss_fftndr_cfg f_cfg = kiss_fftndr_alloc(dims, 2, 0, NULL, NULL);
    kiss_fftndr_cfg i_cfg = kiss_fftndr_alloc(dims, 2, 1, NULL, NULL);

    float signal[16];
    kiss_fft_cpx freq[12];

    for(int i = 0; i < 12; i++){
        freq[i].r = 1.0/(i+1);
        freq[i].i = 0.0;
    }

    printf("Before:\n");
    print_freq(freq, 4, 3);

    // inverse transform followed by transform
    kiss_fftndri(i_cfg, freq, signal);
    kiss_fftndr(f_cfg, signal, freq);

    // renormalize
    for(int i = 0; i < 12; i++){
        freq[i].r /= 16.0;
        freq[i].i /= 16.0;
    }

    printf("\nAfter:\n");
    print_freq(freq, 4, 3);
}

The output is

Before:

 (1.000000, 0.000000)  (0.500000, 0.000000)  (0.333333, 0.000000)
 (0.250000, 0.000000)  (0.200000, 0.000000)  (0.166667, 0.000000)
 (0.142857, 0.000000)  (0.125000, 0.000000)  (0.111111, 0.000000)
 (0.100000, 0.000000)  (0.090909, 0.000000)  (0.083333, 0.000000)

After:

 (1.000000, 0.000000)  (0.500000, 0.000000)  (0.333333, 0.000000)
 (0.175000, 0.000000)  (0.200000, 0.000000)  (0.125000, 0.000000)
 (0.142857, 0.000000)  (0.125000, -0.000000)  (0.111111, 0.000000)
 (0.175000, 0.000000)  (0.090909, 0.000000)  (0.125000, 0.000000)
p2r3i5m7e
  • 11
  • 1
  • You probably need a complex-valued signal. Why are you assuming that frequency spectrum will lead to a real-valued signal? – Cris Luengo Jun 10 '19 at 20:28
  • The way I understand it, kiss_fftndri interprets the input as half of the full spectrum, with the other half being redundant for real-valued signals (i.e. the other half is given as the conjugate of the given half). That's why freq has only 12 elements and not 16. – p2r3i5m7e Jun 10 '19 at 21:31

0 Answers0