Wideband autonomous SDR analysis engine forked from sdr-visual-suite
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.

80 lines
1.6KB

  1. package mock
  2. import (
  3. "math"
  4. "math/rand"
  5. "sync"
  6. "time"
  7. "sdr-wideband-suite/internal/sdr"
  8. )
  9. type Source struct {
  10. mu sync.Mutex
  11. phase float64
  12. phase2 float64
  13. phase3 float64
  14. sampleRate float64
  15. noise float64
  16. rng *rand.Rand
  17. }
  18. func New(sampleRate int) *Source {
  19. return &Source{
  20. sampleRate: float64(sampleRate),
  21. noise: 0.02,
  22. rng: rand.New(rand.NewSource(time.Now().UnixNano())),
  23. }
  24. }
  25. func (s *Source) Start() error { return nil }
  26. func (s *Source) Stop() error { return nil }
  27. func (s *Source) UpdateConfig(sampleRate int, centerHz float64, gainDb float64, agc bool, bwKHz int) error {
  28. s.mu.Lock()
  29. defer s.mu.Unlock()
  30. if sampleRate > 0 {
  31. s.sampleRate = float64(sampleRate)
  32. }
  33. return nil
  34. }
  35. func (s *Source) ReadIQ(n int) ([]complex64, error) {
  36. s.mu.Lock()
  37. defer s.mu.Unlock()
  38. out := make([]complex64, n)
  39. f1 := 50e3
  40. f2 := -120e3
  41. f3 := 300e3
  42. const twoPi = 2 * math.Pi
  43. for i := 0; i < n; i++ {
  44. s.phase += twoPi * f1 / s.sampleRate
  45. s.phase2 += twoPi * f2 / s.sampleRate
  46. s.phase3 += twoPi * f3 / s.sampleRate
  47. if s.phase > twoPi {
  48. s.phase -= twoPi
  49. }
  50. if s.phase2 > twoPi {
  51. s.phase2 -= twoPi
  52. }
  53. if s.phase2 < 0 {
  54. s.phase2 += twoPi
  55. }
  56. if s.phase3 > twoPi {
  57. s.phase3 -= twoPi
  58. }
  59. re := math.Cos(s.phase) + 0.7*math.Cos(s.phase2) + 0.4*math.Cos(s.phase3)
  60. im := math.Sin(s.phase) + 0.7*math.Sin(s.phase2) + 0.4*math.Sin(s.phase3)
  61. re += s.noise * s.rng.NormFloat64()
  62. im += s.noise * s.rng.NormFloat64()
  63. out[i] = complex(float32(re), float32(im))
  64. }
  65. return out, nil
  66. }
  67. func (s *Source) Stats() sdr.SourceStats {
  68. return sdr.SourceStats{}
  69. }
  70. func (s *Source) Flush() {}