Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
fft.c
Go to the documentation of this file.
1 /*
2  * fft.c
3  * Copyright 2011 John Lindgren
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <complex.h>
21 #include <math.h>
22 
23 #include "fft.h"
24 
25 #ifndef HAVE_CEXPF
26 /* e^(a+bi) = (e^a)(cos(b)+sin(b)i) */
27 #define cexpf(x) (expf(crealf(x))*(cosf(cimagf(x))+sinf(cimagf(x))*I))
28 #warning Your C library does not have cexpf(). Please update it.
29 #endif
30 
31 #define N 512 /* size of the DFT */
32 #define LOGN 9 /* log N (base 2) */
33 
34 static float hamming[N]; /* hamming window, scaled to sum to 1 */
35 static int reversed[N]; /* bit-reversal table */
36 static float complex roots[N / 2]; /* N-th roots of unity */
37 static char generated = 0; /* set if tables have been generated */
38 
39 /* Reverse the order of the lowest LOGN bits in an integer. */
40 
41 static int bit_reverse (int x)
42 {
43  int y = 0;
44 
45  for (int n = LOGN; n --; )
46  {
47  y = (y << 1) | (x & 1);
48  x >>= 1;
49  }
50 
51  return y;
52 }
53 
54 /* Generate lookup tables. */
55 
56 static void generate_tables (void)
57 {
58  if (generated)
59  return;
60 
61  for (int n = 0; n < N; n ++)
62  hamming[n] = 1 - 0.85 * cosf (2 * M_PI * n / N);
63  for (int n = 0; n < N; n ++)
64  reversed[n] = bit_reverse (n);
65  for (int n = 0; n < N / 2; n ++)
66  roots[n] = cexpf (2 * M_PI * I * n / N);
67 
68  generated = 1;
69 }
70 
71 /* Perform the DFT using the Cooley-Tukey algorithm. At each step s, where
72  * s=1..log N (base 2), there are N/(2^s) groups of intertwined butterfly
73  * operations. Each group contains (2^s)/2 butterflies, and each butterfly has
74  * a span of (2^s)/2. The twiddle factors are nth roots of unity where n = 2^s. */
75 
76 static void do_fft (float complex a[N])
77 {
78  int half = 1; /* (2^s)/2 */
79  int inv = N / 2; /* N/(2^s) */
80 
81  /* loop through steps */
82  while (inv)
83  {
84  /* loop through groups */
85  for (int g = 0; g < N; g += half << 1)
86  {
87  /* loop through butterflies */
88  for (int b = 0, r = 0; b < half; b ++, r += inv)
89  {
90  float complex even = a[g + b];
91  float complex odd = roots[r] * a[g + half + b];
92  a[g + b] = even + odd;
93  a[g + half + b] = even - odd;
94  }
95  }
96 
97  half <<= 1;
98  inv >>= 1;
99  }
100 }
101 
102 /* Input is N=512 PCM samples.
103  * Output is intensity of frequencies from 1 to N/2=256. */
104 
105 void calc_freq (const float data[N], float freq[N / 2])
106 {
107  generate_tables ();
108 
109  /* input is filtered by a Hamming window */
110  /* input values are in bit-reversed order */
111  float complex a[N];
112  for (int n = 0; n < N; n ++)
113  a[reversed[n]] = data[n] * hamming[n];
114 
115  do_fft (a);
116 
117  /* output values are divided by N */
118  /* frequencies from 1 to N/2-1 are doubled */
119  for (int n = 0; n < N / 2 - 1; n ++)
120  freq[n] = 2 * cabsf (a[1 + n]) / N;
121 
122  /* frequency N/2 is not doubled */
123  freq[N / 2 - 1] = cabsf (a[N / 2]) / N;
124 }