You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.5 KiB

  1. //! [Algorithm][1] to compute the [discrete Fourier transform][2] and its
  2. //! inverse.
  3. //!
  4. //! [1]: https://en.wikipedia.org/wiki/Fast_Fourier_transform
  5. //! [2]: https://en.wikipedia.org/wiki/Discrete_Fourier_transform
  6. // The implementation is based on:
  7. // http://www.librow.com/articles/article-10
  8. extern crate complex;
  9. use complex::c64;
  10. use std::slice;
  11. /// A means of obtaining a slice of mutable complex numbers.
  12. pub trait AsMutComplex<'l> {
  13. fn as_mut_complex(self) -> &'l mut [c64];
  14. }
  15. impl<'l> AsMutComplex<'l> for &'l mut [c64] {
  16. #[inline(always)]
  17. fn as_mut_complex(self) -> &'l mut [c64] {
  18. self
  19. }
  20. }
  21. impl<'l> AsMutComplex<'l> for &'l mut [f64] {
  22. /// Treat the slice as a collection of pairs of real and imaginary parts and
  23. /// reinterpret it as a slice of complex numbers.
  24. ///
  25. /// ## Panics
  26. ///
  27. /// The function panics if the number of elements is not even.
  28. #[inline]
  29. fn as_mut_complex(self) -> &'l mut [c64] {
  30. unsafe {
  31. let length = self.len();
  32. assert!(length % 2 == 0, "the number of elements should be even");
  33. slice::from_raw_parts_mut(self.as_mut_ptr() as *mut _, length / 2)
  34. }
  35. }
  36. }
  37. impl<'l> AsMutComplex<'l> for &'l mut Vec<f64> {
  38. #[inline]
  39. fn as_mut_complex(self) -> &'l mut [c64] {
  40. <&mut [f64]>::as_mut_complex(&mut *self)
  41. }
  42. }
  43. /// Perform the Fourier transform.
  44. ///
  45. /// The number of points should be a power of two.
  46. pub fn forward<'l, T: AsMutComplex<'l>>(data: T) {
  47. let data = data.as_mut_complex();
  48. let n = data.len();
  49. if n < 1 || n & (n - 1) != 0 {
  50. panic!("expected the number of points to be a power of two");
  51. }
  52. rearrange(data, n);
  53. perform(data, n, false);
  54. }
  55. /// Perform the inverse Fourier transform.
  56. ///
  57. /// The number of points should be a power of two.
  58. pub fn inverse<'l, T: AsMutComplex<'l>>(data: T, scaling: bool) {
  59. let data = data.as_mut_complex();
  60. let n = data.len();
  61. if n < 1 || n & (n - 1) != 0 {
  62. panic!("expected the number of points to be a power of two");
  63. }
  64. rearrange(data, n);
  65. perform(data, n, true);
  66. if scaling {
  67. scale(data, n);
  68. }
  69. }
  70. #[inline(always)]
  71. fn rearrange(data: &mut [c64], n: usize) {
  72. let mut target = 0;
  73. for position in 0..n {
  74. if target > position {
  75. data.swap(position, target);
  76. }
  77. let mut mask = n >> 1;
  78. while target & mask != 0 {
  79. target &= !mask;
  80. mask >>= 1;
  81. }
  82. target |= mask;
  83. }
  84. }
  85. #[inline(always)]
  86. fn perform(data: &mut [c64], n: usize, inverse: bool) {
  87. use std::f64::consts::PI;
  88. let pi = if inverse { PI } else { -PI };
  89. let mut step = 1;
  90. while step < n {
  91. let jump = step << 1;
  92. let delta = pi / step as f64;
  93. let sine = (0.5 * delta).sin();
  94. let multiplier = c64(-2.0 * sine * sine, delta.sin());
  95. let mut factor = c64(1.0, 0.0);
  96. for group in 0..step {
  97. let mut pair = group;
  98. while pair < n {
  99. let match_pair = pair + step;
  100. let product = factor * data[match_pair];
  101. data[match_pair] = data[pair] - product;
  102. data[pair] = data[pair] + product;
  103. pair += jump;
  104. }
  105. factor = multiplier * factor + factor;
  106. }
  107. step <<= 1;
  108. }
  109. }
  110. #[inline(always)]
  111. fn scale(data: &mut [c64], n: usize) {
  112. let factor = 1.0 / n as f64;
  113. for position in 0..n {
  114. data[position] = data[position] * factor;
  115. }
  116. }