hx3d  1
2D/3D Simple Game Framework
fft.cpp
1 /*
2  FFT calculation.
3  Source: http://rosettacode.org/wiki/Fast_Fourier_transform#C.2B.2B
4 
5  Copyright (C) 2015 Denis BOURGE
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  USA
21 */
22 
23 #include "hx3d/audio/fft.hpp"
24 
25 namespace hx3d {
26 namespace audio {
27 
28 void FFT::fft(std::valarray<Complex>& vector)
29 {
30  const size_t N = vector.size();
31  if (N <= 1) return;
32 
33  // divide
34  std::valarray<Complex> even = vector[std::slice(0, N/2, 2)];
35  std::valarray<Complex> odd = vector[std::slice(1, N/2, 2)];
36 
37  // conquer
38  fft(even);
39  fft(odd);
40 
41  // combine
42  for (size_t k = 0; k < N/2; ++k)
43  {
44  Complex t = std::polar(1.0, -2 * math::PI * k / N) * odd[k];
45  vector[k ] = even[k] + t;
46  vector[k+N/2] = even[k] - t;
47  }
48 }
49 
50 void FFT::bfft(std::valarray<Complex>& vector)
51 {
52  // DFT
53  unsigned int N = vector.size(), k = N, n;
54  double thetaT = 3.14159265358979323846264338328L / N;
55  Complex phiT = Complex(cos(thetaT), sin(thetaT)), T;
56  while (k > 1)
57  {
58  n = k;
59  k >>= 1;
60  phiT = phiT * phiT;
61  T = 1.0L;
62  for (unsigned int l = 0; l < k; l++)
63  {
64  for (unsigned int a = l; a < N; a += n)
65  {
66  unsigned int b = a + k;
67  Complex t = vector[a] - vector[b];
68  vector[a] += vector[b];
69  vector[b] = t * T;
70  }
71  T *= phiT;
72  }
73  }
74 
75  // Decimate
76  unsigned int m = (unsigned int)(log(N) / log(2));
77  for (unsigned int a = 0; a < N; a++)
78  {
79  unsigned int b = a;
80  // Reverse bits
81  b = (((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1));
82  b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2));
83  b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4));
84  b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8));
85  b = ((b >> 16) | (b << 16)) >> (32 - m);
86  if (b > a)
87  {
88  Complex t = vector[a];
89  vector[a] = vector[b];
90  vector[b] = t;
91  }
92  }
93 
95  //Complex f = 1.0 / sqrt(N);
96  //for (unsigned int i = 0; i < N; i++)
97  // vector[i] *= f;
98 }
99 
100 } /* audio */
101 } /* hx3d */
static void fft(std::valarray< Complex > &vector)
Cooley–Tukey FFT (in-place, divide-and-conquer)
Definition: fft.cpp:28
hx3d framework namespace
Definition: audio.hpp:26
static void bfft(std::valarray< Complex > &vector)
Cooley-Tukey FFT (in-place, breadth-first, decimation-in-frequency)
Definition: fft.cpp:50
const double PI
PI.
Definition: constants.hpp:38