PICCANTE  0.4
The hottest HDR imaging library!
fft.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_FFT_HPP
19 #define PIC_UTIL_FFT_HPP
20 
21 #include <string.h>
22 #include <complex>
23 
24 #include "../base.hpp"
25 #include "../util/math.hpp"
26 
27 namespace pic {
28 
34 PIC_INLINE unsigned int RE(unsigned int x) {
35  return x << 1;
36 }
37 
43 PIC_INLINE unsigned int IM(unsigned int x) {
44  return (x << 1) + 1;
45 }
46 
50 typedef std::complex<float> complexf;
51 
55 typedef std::complex<double> complexd;
56 
64 PIC_INLINE float *DFT1D(float *in, unsigned int n, float *out = NULL)
65 {
66  if(out == NULL) {
67  out = new float[n * 2];
68  }
69 
70  float n_f = float(n);
71 
72  for(unsigned int i = 0; i < n ; i++) {
73  unsigned int re = RE(i);
74  unsigned int im = IM(i);
75 
76  out[re] = 0.0f;
77  out[im] = 0.0f;
78 
79  float i_f = float(i);
80  for(unsigned int j = 0; j < n ; j++) {
81 
82  float angle = -C_PI_2 * i_f * float(j) / n_f;
83  out[re] += in[j] * cosf(angle);
84  out[im] += in[j] * sinf(angle);
85  }
86  }
87 
88  return out;
89 }
90 
97 PIC_INLINE unsigned int bitReversal(unsigned int n, unsigned int nbit)
98 {
99  unsigned int out = 0;
100  for(int i = nbit; i>0; i--) {
101  unsigned int bit = (n >> (i - 1)) & 0x00000001;
102  out += bit << (nbit - i);
103  }
104 
105  return out;
106 }
107 
115 PIC_INLINE float *FFTIterative1D(float *in, unsigned int n, float *out = NULL)
116 {
117  if(out == NULL) {
118  out = new float[n * 2];
119  }
120 
121  unsigned int logn = std::log2(n);
122 
123  for(unsigned int i = 0; i< n; i++) {
124  //bit reversal
125  unsigned int i_rev = bitReversal(i, logn);
126  out[RE(i_rev)] = in[i];
127  out[IM(i_rev)] = 0.0f;
128  }
129 
130  for(unsigned int s = 1; s <= logn; s++) {
131  int m = 1 << s;
132  float angle = -C_PI_2 / float(m);
133 
134  complexf omega_m = complexf(cosf(angle), sinf(angle));
135  complexf omega = complexf(1.0f, 0.0f);
136 
137  for(unsigned int j = 0; j < (m / 2); j++) {
138  for(unsigned int k = j; k < n; k += m) {
139  unsigned int ind = k + m / 2;
140 
141  complexf t = omega * complexf(out[RE(ind)], out[IM(ind)]);
142  complexf u = complexf(out[RE(k)], out[IM(k)]);
143 
144  complexf tmp0 = u + t;
145 
146  out[RE(k)] = tmp0.real();
147  out[IM(k)] = tmp0.imag();
148 
149  complexf tmp1 = u - t;
150 
151  out[RE(ind)] = tmp1.real();
152  out[IM(ind)] = tmp1.imag();
153  }
154 
155  omega *= omega_m;
156  }
157  }
158 
159  return out;
160 }
161 
166 {
167  int n = 16;
168  float *values = new float[n];
169  float *values_fft = new float[n * 2];
170  float *values_dft = new float[n * 2];
171 
172  for(int i=0;i<n;i++) {
173  values[i] = 0.0f;
174  }
175 
176  values[1] = 1.0f;
177  values[2] = 1.0f;
178 
179  FFTIterative1D(values, n, values_fft);
180 
181  printf("FFT\n");
182  for(int i=0;i<n;i++) {
183  printf("%3.3f %3.3f\n", values_fft[RE(i)], values_fft[IM(i)]);
184  }
185 
186  DFT1D(values, n, values_dft);
187  printf("DFT\n");
188  for(int i=0;i<n;i++) {
189  printf("%3.3f %3.3f\n", values_dft[RE(i)], values_dft[IM(i)]);
190  }
191 }
192 
193 } // end namespace pic
194 
195 #endif /* PIC_UTIL_FFT_HPP */
196 
PIC_INLINE unsigned int RE(unsigned int x)
RE.
Definition: fft.hpp:34
PIC_INLINE float * FFTIterative1D(float *in, unsigned int n, float *out=NULL)
FFTIterative1D.
Definition: fft.hpp:115
const float C_PI_2
Definition: math.hpp:52
PIC_INLINE float * DFT1D(float *in, unsigned int n, float *out=NULL)
DFT1D.
Definition: fft.hpp:64
PIC_INLINE unsigned int bitReversal(unsigned int n, unsigned int nbit)
bitReversal
Definition: fft.hpp:97
std::complex< double > complexd
complexd
Definition: fft.hpp:55
#define PIC_INLINE
Definition: base.hpp:33
std::complex< float > complexf
complexf
Definition: fft.hpp:50
PIC_INLINE int log2(int n)
log2 computes logarithm in base 2 for integers.
Definition: saturation.hpp:302
PIC_INLINE void fftTest()
fftTest
Definition: fft.hpp:165
Definition: bilateral_separation.hpp:25
PIC_INLINE unsigned int IM(unsigned int x)
IM.
Definition: fft.hpp:43