From 12abfa14f5a3b328ddb78620486b40d46fe213ec Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Thu, 26 Aug 2021 08:09:19 +0100 Subject: Reaarange source --- NaiveFFT/fft.c | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ NaiveFFT/fft.h | 20 ++++ NaiveFFT/main.c | 276 +------------------------------------------------------- 3 files changed, 294 insertions(+), 273 deletions(-) create mode 100644 NaiveFFT/fft.c create mode 100644 NaiveFFT/fft.h diff --git a/NaiveFFT/fft.c b/NaiveFFT/fft.c new file mode 100644 index 0000000..cfcf3fa --- /dev/null +++ b/NaiveFFT/fft.c @@ -0,0 +1,271 @@ +// +// fft.c +// NaiveFFT +// +// Created by Jacky Jack on 26/08/2021. +// + +#include "fft.h" + + + + +//calc the fft +void fft_if(double *x_i, double *x_q, int n, int inv) { + + int i=0,j=0,k=0,m=0, irem=0, sign; + double tq,ti; + + k=n/2; + for (j=1,i=0;i1) { + irem = irem/2; + m = m+1; + } + + //FFT or IFFT + if (inv==1) { + sign = 1; + } else { + sign = -1; + } + + // transform + for (i=1; i>1; + int Nm1 = n-1; + int i,j; + + + for (i = 0, j = 0; i < n; i++) { + if (j > i) { + double tmp_r = x_i[i]; + double tmp_i = x_q[i]; + //data[i] = data[j]; + x_i[i] = x_q[j]; + x_q[i] = x_q[j]; + //data[j] = tmp; + x_i[j] = tmp_r; + x_q[j] = tmp_i; + } + + /* + * Find least significant zero bit + */ + + unsigned lszb = ~i & (i + 1); + + /* + * Use division to bit-reverse the single bit so that we now have + * the most significant zero bit + * + * N = 2^r = 2^(m+1) + * Nd2 = N/2 = 2^m + * if lszb = 2^k, where k is within the range of 0...m, then + * mszb = Nd2 / lszb + * = 2^m / 2^k + * = 2^(m-k) + * = bit-reversed value of lszb + */ + + unsigned mszb = Nd2 / lszb; + + /* + * Toggle bits with bit-reverse mask + */ + + unsigned bits = Nm1 & ~(mszb - 1); + j ^= bits; + } +} +void fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv) { + uint64_t n_log2; + uint64_t r; + uint64_t m, md2; + uint64_t i,j,k; + uint64_t i_e, i_o; + double theta_2pi; + double theta; + + double Wm_r, Wm_i, Wmk_r, Wmk_i; + double u_r, u_i, t_r, t_i; + + //find log of n + i=n; + n_log2 = 0; + while (i>1) { + i=i/2; + n_log2+=1; + } + + if (inv==1) { + theta_2pi = -2*M_PI; + } else { + theta_2pi = 2*M_PI; + } + + + for (i=1; i<= n_log2; i++) { + m = 1 << i; + md2 = m >> 1; + theta = theta_2pi / m; + Wm_r = cos(theta); + Wm_i = sin(theta); + + + for (j=0; j +#include +#include + +void fft_if(double *x_i, double *x_q, int n, int inv); +void ffti_shuffle_1(double *x_i, double *x_q, uint64_t n); +void fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv); +void dft(double *x_i, double *x_q, int n, int inv); + +#endif /* fft_h */ diff --git a/NaiveFFT/main.c b/NaiveFFT/main.c index 1d71ac5..718e5d0 100644 --- a/NaiveFFT/main.c +++ b/NaiveFFT/main.c @@ -14,286 +14,16 @@ #include #include -//data array +#include "fft.h" + +//test data array #define DATA_SIZE (8) //double data_i[DATA_SIZE] = {0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f}; //double data_q[DATA_SIZE] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f}; double data_i[DATA_SIZE] = {1.0f,1.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f}; double data_q[DATA_SIZE] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f}; -//calc the fft -void fft_if(double *x_i, double *x_q, int n, int inv) { - - int i=0,j=0,k=0,m=0, irem=0, sign; - double tq,ti; - - k=n/2; - for (j=1,i=0;i1) { - irem = irem/2; - m = m+1; - } - - - //FFT or IFFT - if (inv==1) { - sign = 1; - } else { - sign = -1; - } - - // transform - for (i=1; i>1; - int Nm1 = n-1; - int i,j; - - - for (i = 0, j = 0; i < n; i++) { - if (j > i) { - double tmp_r = x_i[i]; - double tmp_i = x_q[i]; - //data[i] = data[j]; - x_i[i] = x_q[j]; - x_q[i] = x_q[j]; - //data[j] = tmp; - x_i[j] = tmp_r; - x_q[j] = tmp_i; - } - - /* - * Find least significant zero bit - */ - - unsigned lszb = ~i & (i + 1); - - /* - * Use division to bit-reverse the single bit so that we now have - * the most significant zero bit - * - * N = 2^r = 2^(m+1) - * Nd2 = N/2 = 2^m - * if lszb = 2^k, where k is within the range of 0...m, then - * mszb = Nd2 / lszb - * = 2^m / 2^k - * = 2^(m-k) - * = bit-reversed value of lszb - */ - unsigned mszb = Nd2 / lszb; - - /* - * Toggle bits with bit-reverse mask - */ - - unsigned bits = Nm1 & ~(mszb - 1); - j ^= bits; - } -} -void fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv) { - uint64_t n_log2; - uint64_t r; - uint64_t m, md2; - uint64_t i,j,k; - uint64_t i_e, i_o; - double theta_2pi; - double theta; - - double Wm_r, Wm_i, Wmk_r, Wmk_i; - double u_r, u_i, t_r, t_i; - - //find log of n - i=n; - n_log2 = 0; - while (i>1) { - i=i/2; - n_log2+=1; - } - - if (inv==1) { - theta_2pi = -2*M_PI; - } else { - theta_2pi = 2*M_PI; - } - - - for (i=1; i<= n_log2; i++) { - m = 1 << i; - md2 = m >> 1; - theta = theta_2pi / m; - Wm_r = cos(theta); - Wm_i = sin(theta); - - - for (j=0; j