Nenhuma descrição

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. "math/rand"
  6. "github.com/naleek/gortty"
  7. )
  8. // 96000 = 14
  9. // 48000 = 7
  10. // 44100 = 7
  11. // 22050 = 3
  12. // 11025 = 1
  13. // 8000 = 1
  14. func main() {
  15. // lpf := 100
  16. // l := newLpf(lpf)
  17. // for i := 5000; i <= 44100; i += 1000 {
  18. // l.update(dostuff(11025))
  19. // dostuff(11025)
  20. dostuff(8000)
  21. // dostuff(11025)
  22. // dostuff(22050)
  23. // dostuff(44100)
  24. // dostuff(48000)
  25. // dostuff(96000)
  26. // fmt.Printf("%d\t%f\n", i-lpf/2, v)
  27. //}
  28. }
  29. func dostuff(freq int) float64 {
  30. done := make(chan bool)
  31. s := &gortty.ModemSettings{
  32. Baud: 45,
  33. StopBits: 2,
  34. DataBits: 5,
  35. SampleRate: freq,
  36. OneFreq: 2125,
  37. ZeroFreq: 2295,
  38. }
  39. demodulatorIn := make(chan int16)
  40. demodulatorOut := make(chan [3]float64)
  41. go func() {
  42. // pulse
  43. // for i := 0; i <= 200; i++ {
  44. // if i == 50 {
  45. // demodulatorIn <- int16(16384)
  46. // } else {
  47. // demodulatorIn <- int16(0)
  48. // }
  49. // }
  50. //
  51. // for samp := 0; samp <= s.SampleRate/45; samp++ {
  52. // phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * 2125 * 2
  53. // demodulatorIn <- int16(math.Sin(phase) * 16384)
  54. // }
  55. // // noise
  56. for i := 0; i <= s.SampleRate/4; i++ {
  57. n := rand.Float64()
  58. demodulatorIn <- int16(n * 16384)
  59. }
  60. for x := 0; x <= 5; x++ {
  61. for samp := 0; samp <= s.SampleRate/45; samp++ {
  62. phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * float64(s.OneFreq)
  63. n := rand.Float64()
  64. demodulatorIn <- int16((math.Sin(phase) + n) * 16384)
  65. }
  66. for samp := 0; samp <= s.SampleRate/45; samp++ {
  67. phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * float64(s.ZeroFreq)
  68. n := rand.Float64()
  69. demodulatorIn <- int16((math.Sin(phase) + n) * 16384)
  70. }
  71. }
  72. //
  73. // for samp := 0; samp <= s.SampleRate/1; samp++ {
  74. // phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * float64(s.ZeroFreq)
  75. // demodulatorIn <- int16(math.Sin(phase) * 16384)
  76. // }
  77. // for samp := 0; samp <= s.SampleRate/40; samp++ {
  78. // phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * float64(s.OneFreq)
  79. // demodulatorIn <- int16(math.Sin(phase) * 16384)
  80. // }
  81. //
  82. // for samp := 0; samp <= s.SampleRate/40; samp++ {
  83. // phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * float64(s.ZeroFreq)
  84. // demodulatorIn <- int16(math.Sin(phase) * 16384)
  85. // }
  86. // for samp := 0; samp <= s.SampleRate/10; samp++ {
  87. // phase := 2 * math.Pi * float64(samp) / float64(s.SampleRate) * float64(s.ZeroFreq)
  88. // demodulatorIn <- int16(math.Sin(phase) * 16384)
  89. // }
  90. done <- true
  91. }()
  92. gortty.NewDumbDemodulator(s, demodulatorIn, demodulatorOut)
  93. c := 0
  94. running := true
  95. var r float64
  96. for running {
  97. select {
  98. case v := <-demodulatorOut:
  99. fmt.Printf("%f\t%f\t%f\n", v[0], v[1], v[2])
  100. r = v[0]
  101. c++
  102. case <-done:
  103. running = false
  104. }
  105. }
  106. return r
  107. }
  108. type lpf struct {
  109. x []float64
  110. t float64
  111. }
  112. func newLpf(size int) *lpf {
  113. l := new(lpf)
  114. l.x = make([]float64, size)
  115. return l
  116. }
  117. func (l *lpf) update(v float64) float64 {
  118. o := l.x[0]
  119. l.x = append(l.x, v)
  120. l.x = l.x[1:]
  121. l.t -= o
  122. l.t += v
  123. return l.t
  124. }