PandA-2024.02
fft_float.c
Go to the documentation of this file.
1 #include <math.h>
2 /*
3  This computes an in-place complex-to-complex FFT
4  x and y are the real and imaginary arrays of 2^m points.
5  dir = 1 gives forward transform
6  dir = -1 gives reverse transform
7  Code from http://paulbourke.net/miscellaneous/dft/
8  by Paul Bourke June 1993
9 */
10 short FFT(short int dir, long m, float *x, float *y) {
11  long n, i, i1, j, k, i2, l, l1, l2;
12  float c1, c2, tx, ty, t1, t2, u1, u2, z;
13 
14  /* Calculate the number of points */
15  n = 1;
16  for (i = 0; i < m; i++) n *= 2;
17 
18  /* Do the bit reversal */
19  i2 = n >> 1;
20  j = 0;
21  for (i = 0; i < n - 1; i++) {
22  if (i < j) {
23  tx = x[i];
24  ty = y[i];
25  x[i] = x[j];
26  y[i] = y[j];
27  x[j] = tx;
28  y[j] = ty;
29  }
30  k = i2;
31  while (k <= j) {
32  j -= k;
33  k >>= 1;
34  }
35  j += k;
36  }
37 
38  /* Compute the FFT */
39  c1 = -1.0;
40  c2 = 0.0;
41  l2 = 1;
42  for (l = 0; l < m; l++) {
43  l1 = l2;
44  l2 <<= 1;
45  u1 = 1.0;
46  u2 = 0.0;
47  for (j = 0; j < l1; j++) {
48  for (i = j; i < n; i += l2) {
49  i1 = i + l1;
50  t1 = u1 * x[i1] - u2 * y[i1];
51  t2 = u1 * y[i1] + u2 * x[i1];
52  x[i1] = x[i] - t1;
53  y[i1] = y[i] - t2;
54  x[i] += t1;
55  y[i] += t2;
56  }
57  z = u1 * c1 - u2 * c2;
58  u2 = u1 * c2 + u2 * c1;
59  u1 = z;
60  }
61  c2 = sqrtf((1.0 - c1) / 2.0);
62  if (dir == 1) c2 = -c2;
63  c1 = sqrtf((1.0 + c1) / 2.0);
64  }
65 
66  /* Scaling for forward transform */
67  if (dir == 1) {
68  for (i = 0; i < n; i++) {
69  x[i] /= n;
70  y[i] /= n;
71  }
72  }
73 
74  return 0;
75 }
76 
77 
78 /*
79  * Here is an example program which computes the FFT of a short pulse in a sample of length 128.
80  * To make the resulting fourier transform real the pulse is defined for equal positive and
81  * negative times (-10 ... 10), where the negative times wrap around the end of the array.
82  * The transformed data is rescaled by 1/\sqrt N so that it fits on the same plot as the input.
83  * Only the real part is shown, by the choice of the input data the imaginary part is zero.
84  * Allowing for the wrap-around of negative times at t=128, and working in units of k/N,
85  * the DFT approximates the continuum fourier transform, giving a modulated \sin function.
86 */
87 
88 int
89 main (void)
90 {
91  int i;
92  float x[128];
93  float y[128];
94 
95  for (i = 0; i < 128; i++)
96  {
97  x[i] = 0.0;
98  y[i] = 0.0;
99  }
100 
101  x[0] = 1.0;
102 
103  for (i = 1; i <= 10; i++)
104  {
105  x[i] = x[128-i] = 1.0;
106  }
107 
108  for (i = 0; i < 128; i++)
109  {
110  printf ("%d %e %e\n", i,
111  x[i], y[i]);
112  }
113  printf ("\n");
114 
115  i = FFT(1, 7, x, y);
116  for (i = 0; i < 128; i++)
117  {
118  printf ("%d %e %e\n", i,
119  x[i]/sqrtf(128),
120  y[i]/sqrtf(128));
121  }
122 
123  return 0;
124 }
125 
int main(void)
Definition: fft_float.c:89
TVMArray c2[1]
TVMArray c1[1]
static const uint32_t k[]
Definition: sha-256.c:22
x
Return the smallest n such that 2^n >= _x.
short FFT(short int dir, long m, float *x, float *y)
Definition: fft_float.c:10

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13