Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

96 wiersze
2.2KB

  1. package dsp
  2. // StatefulDecimatingFIRComplex combines FIR filtering and decimation into a
  3. // single stateful stage. This avoids exposing FIR settling/transient output as
  4. // ordinary block-leading samples before decimation.
  5. type StatefulDecimatingFIRComplex struct {
  6. taps []float64
  7. delayR []float64
  8. delayI []float64
  9. factor int
  10. phase int // number of input samples until next output sample (0 => emit now)
  11. }
  12. func (f *StatefulDecimatingFIRComplex) Phase() int {
  13. if f == nil {
  14. return 0
  15. }
  16. return f.phase
  17. }
  18. func (f *StatefulDecimatingFIRComplex) TapsLen() int {
  19. if f == nil {
  20. return 0
  21. }
  22. return len(f.taps)
  23. }
  24. func NewStatefulDecimatingFIRComplex(taps []float64, factor int) *StatefulDecimatingFIRComplex {
  25. if factor < 1 {
  26. factor = 1
  27. }
  28. t := make([]float64, len(taps))
  29. copy(t, taps)
  30. return &StatefulDecimatingFIRComplex{
  31. taps: t,
  32. delayR: make([]float64, len(taps)),
  33. delayI: make([]float64, len(taps)),
  34. factor: factor,
  35. phase: 0,
  36. }
  37. }
  38. func (f *StatefulDecimatingFIRComplex) Reset() {
  39. for i := range f.delayR {
  40. f.delayR[i] = 0
  41. f.delayI[i] = 0
  42. }
  43. f.phase = 0
  44. }
  45. func (f *StatefulDecimatingFIRComplex) Process(iq []complex64) []complex64 {
  46. if len(iq) == 0 || len(f.taps) == 0 {
  47. return nil
  48. }
  49. if f.factor <= 1 {
  50. out := make([]complex64, len(iq))
  51. for i := 0; i < len(iq); i++ {
  52. copy(f.delayR[1:], f.delayR[:len(f.taps)-1])
  53. copy(f.delayI[1:], f.delayI[:len(f.taps)-1])
  54. f.delayR[0] = float64(real(iq[i]))
  55. f.delayI[0] = float64(imag(iq[i]))
  56. var accR, accI float64
  57. for k := 0; k < len(f.taps); k++ {
  58. w := f.taps[k]
  59. accR += f.delayR[k] * w
  60. accI += f.delayI[k] * w
  61. }
  62. out[i] = complex(float32(accR), float32(accI))
  63. }
  64. return out
  65. }
  66. out := make([]complex64, 0, len(iq)/f.factor+1)
  67. n := len(f.taps)
  68. for i := 0; i < len(iq); i++ {
  69. copy(f.delayR[1:], f.delayR[:n-1])
  70. copy(f.delayI[1:], f.delayI[:n-1])
  71. f.delayR[0] = float64(real(iq[i]))
  72. f.delayI[0] = float64(imag(iq[i]))
  73. if f.phase == 0 {
  74. var accR, accI float64
  75. for k := 0; k < n; k++ {
  76. w := f.taps[k]
  77. accR += f.delayR[k] * w
  78. accI += f.delayI[k] * w
  79. }
  80. out = append(out, complex(float32(accR), float32(accI)))
  81. f.phase = f.factor - 1
  82. } else {
  83. f.phase--
  84. }
  85. }
  86. return out
  87. }