Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

66 řádky
1.2KB

  1. package dsp
  2. import (
  3. "math"
  4. "math/cmplx"
  5. )
  6. // FFT computes the discrete Fourier transform of x (in-place, radix-2).
  7. // len(x) must be a power of 2.
  8. func FFT(x []complex128) {
  9. n := len(x)
  10. if n <= 1 {
  11. return
  12. }
  13. // Bit-reversal permutation
  14. j := 0
  15. for i := 1; i < n; i++ {
  16. bit := n >> 1
  17. for j&bit != 0 {
  18. j ^= bit
  19. bit >>= 1
  20. }
  21. j ^= bit
  22. if i < j {
  23. x[i], x[j] = x[j], x[i]
  24. }
  25. }
  26. // Cooley-Tukey butterfly
  27. for size := 2; size <= n; size <<= 1 {
  28. half := size >> 1
  29. wn := cmplx.Exp(complex(0, -2*math.Pi/float64(size)))
  30. for start := 0; start < n; start += size {
  31. w := complex(1, 0)
  32. for k := 0; k < half; k++ {
  33. u := x[start+k]
  34. v := x[start+k+half] * w
  35. x[start+k] = u + v
  36. x[start+k+half] = u - v
  37. w *= wn
  38. }
  39. }
  40. }
  41. }
  42. // IFFT computes the inverse DFT of x (in-place).
  43. func IFFT(x []complex128) {
  44. n := len(x)
  45. // Conjugate, FFT, conjugate, scale
  46. for i := range x {
  47. x[i] = cmplx.Conj(x[i])
  48. }
  49. FFT(x)
  50. scale := 1.0 / float64(n)
  51. for i := range x {
  52. x[i] = cmplx.Conj(x[i]) * complex(scale, 0)
  53. }
  54. }
  55. // HannWindow fills w with a Hann window of length n.
  56. func HannWindow(w []float64) {
  57. n := len(w)
  58. for i := range w {
  59. w[i] = 0.5 * (1.0 - math.Cos(2*math.Pi*float64(i)/float64(n)))
  60. }
  61. }