CSL  6.0
fft_N.c
Go to the documentation of this file.
1 /*
2  * Free FFT and convolution (C)
3  * Copyright (c) 2017 Project Nayuki. (MIT License)
4  * https://www.nayuki.io/page/free-small-fft-in-multiple-languages
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10  * the Software, and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  * - The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  * - The Software is provided "as is", without warranty of any kind, express or
15  * implied, including but not limited to the warranties of merchantability,
16  * fitness for a particular purpose and noninfringement. In no event shall the
17  * authors or copyright holders be liable for any claim, damages or other
18  * liability, whether in an action of contract, tort or otherwise, arising from,
19  * out of or in connection with the Software or the use or other dealings in the
20  * Software.
21  */
22 
23 #include <math.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "fft_N.h"
29 
30 // Private function prototypes
31 
32 static size_t reverse_bits(size_t x, int n);
33 static void * memdup(const void *src, size_t n);
34 
35 // setup functions
36 
37 static CFTTYPE * cos_table = 0;
38 static CFTTYPE * sin_table = 0;
39 static unsigned len = 0;
40 static size_t size = 0;
41 
42 void Fft_setup(size_t n) {
43  if ((cos_table) && (sin_table) && (len == n))
44  return;
45  len = n;
46  size = (n / 2) * sizeof(CFTTYPE);
47  if (cos_table) free(cos_table);
48  cos_table = malloc(size);
49  if (sin_table) free(sin_table);
50  sin_table = malloc(size);
51  if (cos_table == NULL || sin_table == NULL)
52  goto oops;
53  float nF = (float) n;
54  float iF = 0.0f;
55  for (size_t i = 0; i < n / 2; i++) {
56  cos_table[i] = cosf(2.0f * M_PI * iF / nF);
57  sin_table[i] = sinf(2.0f * M_PI * iF / nF);
58  iF++;
59  }
60  return;
61 oops:
62  if (cos_table) {
63  free(cos_table);
64  cos_table = 0;
65  }
66  if (sin_table) {
67  free(sin_table);
68  sin_table = 0;
69  }
70 }
71 
72 bool Fft_transform(CFTTYPE real[], CFTTYPE imag[], size_t n) {
73  if (n == 0)
74  return true;
75  if (n != len) {
76  printf("Need to re-init FFT\n");
77  return false;
78  }
79  if ((n & (n - 1)) == 0) { // Is power of 2
80  return Fft_transformRadix2(real, imag, n);
81  } else { // otherwise complain
82  printf("Only power of 2 fft len supported\n");
83  return false;
84  }
85 }
86 
87 bool Fft_inverseTransform(CFTTYPE real[], CFTTYPE imag[], size_t n) {
88  return Fft_transform(imag, real, n);
89 }
90 
91 bool Fft_transformRadix2(CFTTYPE real[], CFTTYPE imag[], size_t n) {
92  // Length variables
93  bool status = false;
94  int levels = 0; // Compute levels = floor(log2(n))
95  for (size_t temp = n; temp > 1U; temp >>= 1)
96  levels++;
97  if ((size_t)1U << levels != n)
98  return false; // n is not a power of 2
99  // Trignometric tables
100  if (SIZE_MAX / sizeof(CFTTYPE) < n / 2)
101  return false;
102  // Bit-reversed addressing permutation
103  for (size_t i = 0; i < n; i++) {
104  size_t j = reverse_bits(i, levels);
105  if (j > i) {
106  CFTTYPE temp = real[i];
107  real[i] = real[j];
108  real[j] = temp;
109  temp = imag[i];
110  imag[i] = imag[j];
111  imag[j] = temp;
112  }
113  }
114  // Cooley-Tukey decimation-in-time radix-2 FFT
115  for (size_t size = 2; size <= n; size *= 2) {
116  size_t halfsize = size / 2;
117  size_t tablestep = n / size;
118  for (size_t i = 0; i < n; i += size) {
119  for (size_t j = i, k = 0; j < i + halfsize; j++, k += tablestep) {
120  size_t l = j + halfsize;
121  CFTTYPE tpre = real[l] * cos_table[k] + imag[l] * sin_table[k];
122  CFTTYPE tpim = -real[l] * sin_table[k] + imag[l] * cos_table[k];
123  real[l] = real[j] - tpre;
124  imag[l] = imag[j] - tpim;
125  real[j] += tpre;
126  imag[j] += tpim;
127  }
128  }
129  if (size == n) // Prevent overflow in 'size *= 2'
130  break;
131  }
132  status = true;
133 cleanup:
134  return status;
135 }
136 
137 static size_t reverse_bits(size_t x, int n) {
138  size_t result = 0;
139  for (int i = 0; i < n; i++, x >>= 1)
140  result = (result << 1) | (x & 1U);
141  return result;
142 }
143 
144 static void * memdup(const void *src, size_t n) {
145  void *dest = malloc(n);
146  if (n > 0 && dest != NULL)
147  memcpy(dest, src, n);
148  return dest;
149 }
bool Fft_inverseTransform(CFTTYPE real[], CFTTYPE imag[], size_t n)
Definition: fft_N.c:87
void Fft_setup(size_t n)
Definition: fft_N.c:42
static size_t reverse_bits(size_t x, int n)
Definition: fft_N.c:137
static size_t size
Definition: fft_N.c:40
bool Fft_transform(CFTTYPE real[], CFTTYPE imag[], size_t n)
Definition: fft_N.c:72
static CFTTYPE * sin_table
Definition: fft_N.c:38
static unsigned len
Definition: fft_N.c:39
static void * memdup(const void *src, size_t n)
Definition: fft_N.c:144
#define CFTTYPE
Definition: fft_N.h:34
bool Fft_transformRadix2(CFTTYPE real[], CFTTYPE imag[], size_t n)
Definition: fft_N.c:91
static CFTTYPE * cos_table
Definition: fft_N.c:37