readme.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ==============================================================================
  2. FFTReal
  3. Version 2.11
  4. Fourier transformation (FFT, IFFT) library specialised for real data
  5. Portable ISO C++
  6. Copyright (c) 1999-2010 Laurent de Soras
  7. Object Pascal port (c) Frederic Vanmol
  8. ==============================================================================
  9. Contents:
  10. 1. Legal
  11. 2. Content
  12. 3. Using FFTReal
  13. 3.1 FFTReal - Length fixed at run-time
  14. 3.2 FFTRealFixLen - Length fixed at compile-time
  15. 3.3 Data organisation
  16. 4. Compilation and testing
  17. 5. History
  18. 6. Contact
  19. 1. Legal
  20. --------
  21. FFTReal is distributed under the terms of the Do What The Fuck You Want To
  22. Public License.
  23. Check the file license.txt to get full information about the license.
  24. 2. Content
  25. ----------
  26. FFTReal is a library to compute Discrete Fourier Transforms (DFT) with the
  27. FFT algorithm (Fast Fourier Transform) on arrays of real numbers. It can
  28. also compute the inverse transform.
  29. You should find in this package a lot of files ; some of them are of
  30. particular interest:
  31. - readme.txt : you are reading it
  32. - ffft/FFTReal.h : FFT, length fixed at run-time
  33. - ffft/FFTRealFixLen.h: FFT, length fixed at compile-time
  34. - delphi/FFTReal.pas : Pascal implementation (working but not up-to-date)
  35. 3. Using FFTReal
  36. ----------------
  37. Important - if you were using older versions of FFTReal (up to 1.03), some
  38. things have changed. FFTReal is now a template. Therefore use FFTReal<float>
  39. or FFTReal<double> in your code depending on the application datatype. The
  40. flt_t typedef has been removed. And if you were previously using FFTReal 2.0,
  41. note that all the classes have moved to the ffft namespace.
  42. You have two ways to use FFTReal. In the first way, the FFT has its length
  43. fixed at run-time, when the object is instanciated. It means that you have
  44. not to know the length when you write the code. This is the usual way of
  45. proceeding.
  46. 3.1 FFTReal - Length fixed at run-time
  47. --------------------------------------
  48. Just instanciate one time a FFTReal object. Specify the data type you want
  49. as template parameter (only floating point: float, double, long double or
  50. custom type). The constructor precompute a lot of things, so it may be a bit
  51. long. The parameter is the number of points used for the next FFTs. It must
  52. be a power of 2:
  53. #include "ffft/FFTReal.h"
  54. ...
  55. long len = 1024;
  56. ...
  57. // 1024-point FFT object constructed.
  58. ffft::FFTReal <float> fft_object (len);
  59. Then you can use this object to compute as many FFTs and IFFTs as you want.
  60. They will be computed very quickly because a lot of work has been done in the
  61. object construction.
  62. float x [1024];
  63. float f [1024];
  64. ...
  65. fft_object.do_fft (f, x); // x (real) --FFT---> f (complex)
  66. ...
  67. fft_object.do_ifft (f, x); // f (complex) --IFFT--> x (real)
  68. fft_object.rescale (x); // Post-scaling should be done after FFT+IFFT
  69. ...
  70. x [] and f [] are floating point number arrays. x [] is the real number
  71. sequence which we want to compute the FFT. f [] is the result, in the
  72. "frequency" domain. f has the same number of elements as x [], but f []
  73. elements are complex numbers. The routine uses some FFT properties to
  74. optimize memory and to reduce calculations: the transformaton of a real
  75. number sequence is a conjugate complex number sequence: F [k] = F [-k]*.
  76. 3.2 FFTRealFixLen - Length fixed at compile-time
  77. ------------------------------------------------
  78. This class is significantly faster than the previous one, giving a speed
  79. gain between 50 and 100 %. The template parameter is the base-2 logarithm of
  80. the FFT length. The datatype is float; it can be changed by modifying the
  81. DataType typedef in FFTRealFixLenParam.h. As FFTReal class, it supports
  82. only floating-point types or equivalent.
  83. Use is similar as the one of FFTReal. To instanciate the object, just proceed
  84. as indicated below:
  85. #include "ffft/FFTRealFixLen.h"
  86. ...
  87. // 1024-point (2^10) FFT object constructed.
  88. ffft::FFTRealFixLen <10> fft_object;
  89. Warning: long FFT objects may take a very long time to compile, depending on
  90. the compiler and its optimisation options. If compilation time is too high,
  91. encapsulate the FFT object in a seprate class whose header doesn't need
  92. to include FFTRealFixLen.h, so you just have to compile the wrapper once
  93. and only link it the other times. For example (quick, dirty and incomplete):
  94. ffft/FFTWrapper.h: | ffft/FFTWrapper.cpp:
  95. |
  96. class FFTWrapper | #include "ffft/FFTRealFixLen.h"
  97. { | #include "ffft/FFTWrapper.h"
  98. public: |
  99. FFTWrapper (); | FFTWrapper::FFTWrapper ()
  100. ~FFTWrapper (); | : _impl_ptr ((void*) new FTRealFixLen <10>)
  101. void do_fft (...); | {
  102. void do_ifft (...); | }
  103. private: |
  104. void *_impl_ptr; | ...
  105. } |
  106. 3.3 Data organisation
  107. ---------------------
  108. Mathematically speaking, DFT formulas below show what does FFTReal:
  109. do_fft() : f(k) = sum (p = 0, N-1, x(p) * exp (+j*2*pi*k*p/N))
  110. do_ifft(): x(k) = sum (p = 0, N-1, f(p) * exp (-j*2*pi*k*p/N))
  111. Where j is the square root of -1. The formulas differ only by the sign of
  112. the exponential. When the sign is positive, the transform is called positive.
  113. Common formulas for Fourier transform are negative for the direct tranform and
  114. positive for the inverse one.
  115. However in these formulas, f is an array of complex numbers and doesn't
  116. correspound exactly to the f[] array taken as function parameter. The
  117. following table shows how the f[] sequence is mapped onto the usable FFT
  118. coefficients (called bins):
  119. FFTReal output | Positive FFT equiv. | Negative FFT equiv.
  120. ---------------+-----------------------+-----------------------
  121. f [0] | Real (bin 0) | Real (bin 0)
  122. f [...] | Real (bin ...) | Real (bin ...)
  123. f [length/2] | Real (bin length/2) | Real (bin length/2)
  124. f [length/2+1] | Imag (bin 1) | -Imag (bin 1)
  125. f [...] | Imag (bin ...) | -Imag (bin ...)
  126. f [length-1] | Imag (bin length/2-1) | -Imag (bin length/2-1)
  127. And FFT bins are distributed in f [] as above:
  128. | | Positive FFT | Negative FFT
  129. Bin | Real part | imaginary part | imaginary part
  130. ------------+----------------+-----------------+---------------
  131. 0 | f [0] | 0 | 0
  132. 1 | f [1] | f [length/2+1] | -f [length/2+1]
  133. ... | f [...], | f [...] | -f [...]
  134. length/2-1 | f [length/2-1] | f [length-1] | -f [length-1]
  135. length/2 | f [length/2] | 0 | 0
  136. length/2+1 | f [length/2-1] | -f [length-1] | f [length-1]
  137. ... | f [...] | -f [...] | f [...]
  138. length-1 | f [1] | -f [length/2+1] | f [length/2+1]
  139. f [] coefficients have the same layout for FFT and IFFT functions. You may
  140. notice that scaling must be done if you want to retrieve x after FFT and IFFT.
  141. Actually, IFFT (FFT (x)) = x * length(x). This is a not a problem because
  142. most of the applications don't care about absolute values. Thus, the operation
  143. requires less calculation. If you want to use the FFT and IFFT to transform a
  144. signal, you have to apply post- (or pre-) processing yourself. Multiplying
  145. or dividing floating point numbers by a power of 2 doesn't generate extra
  146. computation noise.
  147. 4. Compilation and testing
  148. --------------------------
  149. Drop the following files into your project or makefile:
  150. ffft/Array.*
  151. ffft/def.h
  152. ffft/DynArray.*
  153. ffft/FFTReal*.h*
  154. ffft/OscSinCos.*
  155. Other files are for testing purpose only, do not include them if you just need
  156. to use the library; they are not needed to use FFTReal in your own programs.
  157. FFTReal may be compiled in two versions: release and debug. Debug version
  158. has checks that could slow down the code. Define NDEBUG to set the Release
  159. mode. For example, the command line to compile the test bench on GCC would
  160. look like:
  161. Debug mode:
  162. g++ -Wall -I. -o ./fftreal_debug.exe ffft/test/*.cpp ffft/test/stopwatch/*.cpp
  163. Release mode:
  164. g++ -Wall -I. -o ./fftreal_release.exe -DNDEBUG -O3 ffft/test/*.cpp ffft/test/stopwatch/*.cpp
  165. It may be tricky to compile the test bench because the speed tests use the
  166. stopwatch sub-library, which is not that cross-platform. If you encounter
  167. any problem that you cannot easily fix while compiling it, edit the file
  168. ffft/test/conf.h and un-define the speed test macro. Remove the stopwatch
  169. directory from your source file list, too.
  170. If it's not done by default, you should activate the exception handling
  171. of your compiler to get the class memory-leak-safe. Thus, when a memory
  172. allocation fails (in the constructor), an exception is thrown and the entire
  173. object is safely destructed. It reduces the permanent error checking overhead
  174. in the client code. Also, the test bench requires Run-Time Type Information
  175. (RTTI) to be enabled in order to display the names of the tested classes -
  176. sometimes mangled, depending on the compiler.
  177. Please note: the test bench may take an insane time to compile, especially in
  178. Release mode, because a lot of recursive templates are instanciated.
  179. 5. History
  180. ----------
  181. v2.11 (2010.09.12)
  182. - The LGPL was not well suited to 100% template code, therefore I changed
  183. the license again. Everything is released under the WTFPL.
  184. - Removed warnings in the testcode on MSVC++ 8.0
  185. - Fixed the multiple definition linking error with template specialisations
  186. on GCC 4.
  187. v2.10 (2008.05.28)
  188. - Classes are now in the ffft namespace
  189. - Changed directory structure
  190. - Fixed compilation information in the documentation
  191. v2.00 (2005.10.18)
  192. - Turned FFTReal class into template (data type as parameter)
  193. - Added FFTRealFixLen
  194. - Trigonometric tables are size-limited in order to preserve cache memory;
  195. over a given size, sin/cos functions are computed on the fly.
  196. - Better test bench for accuracy and speed
  197. - Changed license to LGPL
  198. v1.03 (2001.06.15)
  199. - Thanks to Frederic Vanmol for the Pascal port (works with Delphi).
  200. - Documentation improvement
  201. v1.02 (2001.03.25)
  202. - sqrt() is now precomputed when the object FFTReal is constructed, resulting
  203. in speed impovement for small size FFT.
  204. v1.01 (2000)
  205. - Small modifications, I don't remember what.
  206. v1.00 (1999.08.14)
  207. - First version released
  208. 6. Contact
  209. ----------
  210. Please address any comment, bug report or flame to:
  211. Laurent de Soras
  212. [email protected]
  213. http://ldesoras.free.fr
  214. For the Pascal port:
  215. Frederic Vanmol
  216. [email protected]