25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

74 satır
1.3KB

  1. package fftutil
  2. import (
  3. "math"
  4. "gonum.org/v1/gonum/dsp/fourier"
  5. )
  6. func Hann(n int) []float64 {
  7. w := make([]float64, n)
  8. if n <= 1 {
  9. if n == 1 {
  10. w[0] = 1
  11. }
  12. return w
  13. }
  14. for i := 0; i < n; i++ {
  15. w[i] = 0.5 * (1 - math.Cos(2*math.Pi*float64(i)/float64(n-1)))
  16. }
  17. return w
  18. }
  19. func Spectrum(iq []complex64, window []float64) []float64 {
  20. n := len(iq)
  21. if n == 0 {
  22. return nil
  23. }
  24. in := make([]complex128, n)
  25. for i := 0; i < n; i++ {
  26. v := iq[i]
  27. w := 1.0
  28. if len(window) == n {
  29. w = window[i]
  30. }
  31. in[i] = complex(float64(real(v))*w, float64(imag(v))*w)
  32. }
  33. fft := fourier.NewCmplxFFT(n)
  34. out := make([]complex128, n)
  35. fft.Coefficients(out, in)
  36. power := make([]float64, n)
  37. eps := 1e-12
  38. invN := 1.0 / float64(n)
  39. for i := 0; i < n; i++ {
  40. idx := (i + n/2) % n
  41. mag := cmplxAbs(out[idx]) * invN
  42. p := 20 * math.Log10(mag+eps)
  43. power[i] = p
  44. }
  45. return power
  46. }
  47. func SpectrumFromFFT(out []complex64) []float64 {
  48. n := len(out)
  49. if n == 0 {
  50. return nil
  51. }
  52. power := make([]float64, n)
  53. eps := 1e-12
  54. invN := 1.0 / float64(n)
  55. for i := 0; i < n; i++ {
  56. idx := (i + n/2) % n
  57. v := out[idx]
  58. mag := math.Hypot(float64(real(v)), float64(imag(v))) * invN
  59. p := 20 * math.Log10(mag+eps)
  60. power[i] = p
  61. }
  62. return power
  63. }
  64. func cmplxAbs(v complex128) float64 {
  65. return math.Hypot(real(v), imag(v))
  66. }