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

233 řádky
6.9KB

  1. package config
  2. import (
  3. "math"
  4. "os"
  5. "time"
  6. "gopkg.in/yaml.v3"
  7. )
  8. type Band struct {
  9. Name string `yaml:"name" json:"name"`
  10. StartHz float64 `yaml:"start_hz" json:"start_hz"`
  11. EndHz float64 `yaml:"end_hz" json:"end_hz"`
  12. }
  13. type DetectorConfig struct {
  14. ThresholdDb float64 `yaml:"threshold_db" json:"threshold_db"`
  15. MinDurationMs int `yaml:"min_duration_ms" json:"min_duration_ms"`
  16. HoldMs int `yaml:"hold_ms" json:"hold_ms"`
  17. EmaAlpha float64 `yaml:"ema_alpha" json:"ema_alpha"`
  18. HysteresisDb float64 `yaml:"hysteresis_db" json:"hysteresis_db"`
  19. MinStableFrames int `yaml:"min_stable_frames" json:"min_stable_frames"`
  20. GapToleranceMs int `yaml:"gap_tolerance_ms" json:"gap_tolerance_ms"`
  21. CFARMode string `yaml:"cfar_mode" json:"cfar_mode"`
  22. CFARGuardCells int `yaml:"cfar_guard_cells" json:"cfar_guard_cells"`
  23. CFARTrainCells int `yaml:"cfar_train_cells" json:"cfar_train_cells"`
  24. CFARRank int `yaml:"cfar_rank" json:"cfar_rank"`
  25. CFARScaleDb float64 `yaml:"cfar_scale_db" json:"cfar_scale_db"`
  26. CFARWrapAround bool `yaml:"cfar_wrap_around" json:"cfar_wrap_around"`
  27. // Deprecated (backward compatibility)
  28. CFAREnabled *bool `yaml:"cfar_enabled,omitempty" json:"cfar_enabled,omitempty"`
  29. }
  30. type RecorderConfig struct {
  31. Enabled bool `yaml:"enabled" json:"enabled"`
  32. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  33. MinDuration string `yaml:"min_duration" json:"min_duration"`
  34. MaxDuration string `yaml:"max_duration" json:"max_duration"`
  35. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  36. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  37. RecordAudio bool `yaml:"record_audio" json:"record_audio"`
  38. AutoDemod bool `yaml:"auto_demod" json:"auto_demod"`
  39. AutoDecode bool `yaml:"auto_decode" json:"auto_decode"`
  40. MaxDiskMB int `yaml:"max_disk_mb" json:"max_disk_mb"`
  41. OutputDir string `yaml:"output_dir" json:"output_dir"`
  42. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  43. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  44. }
  45. type DecoderConfig struct {
  46. FT8Cmd string `yaml:"ft8_cmd" json:"ft8_cmd"`
  47. WSPRCmd string `yaml:"wspr_cmd" json:"wspr_cmd"`
  48. DMRCmd string `yaml:"dmr_cmd" json:"dmr_cmd"`
  49. DStarCmd string `yaml:"dstar_cmd" json:"dstar_cmd"`
  50. FSKCmd string `yaml:"fsk_cmd" json:"fsk_cmd"`
  51. PSKCmd string `yaml:"psk_cmd" json:"psk_cmd"`
  52. }
  53. type Config struct {
  54. Bands []Band `yaml:"bands" json:"bands"`
  55. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  56. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  57. FFTSize int `yaml:"fft_size" json:"fft_size"`
  58. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  59. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  60. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  61. AGC bool `yaml:"agc" json:"agc"`
  62. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  63. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  64. Detector DetectorConfig `yaml:"detector" json:"detector"`
  65. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  66. Decoder DecoderConfig `yaml:"decoder" json:"decoder"`
  67. WebAddr string `yaml:"web_addr" json:"web_addr"`
  68. EventPath string `yaml:"event_path" json:"event_path"`
  69. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  70. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  71. WebRoot string `yaml:"web_root" json:"web_root"`
  72. }
  73. func Default() Config {
  74. return Config{
  75. Bands: []Band{
  76. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  77. },
  78. CenterHz: 100.0e6,
  79. SampleRate: 2_048_000,
  80. FFTSize: 2048,
  81. GainDb: 30,
  82. TunerBwKHz: 1536,
  83. UseGPUFFT: false,
  84. AGC: false,
  85. DCBlock: false,
  86. IQBalance: false,
  87. Detector: DetectorConfig{
  88. ThresholdDb: -20,
  89. MinDurationMs: 250,
  90. HoldMs: 500,
  91. EmaAlpha: 0.2,
  92. HysteresisDb: 3,
  93. MinStableFrames: 3,
  94. GapToleranceMs: 500,
  95. CFARMode: "GOSCA",
  96. CFARGuardCells: 3,
  97. CFARTrainCells: 24,
  98. CFARRank: 36,
  99. CFARScaleDb: 6,
  100. CFARWrapAround: true,
  101. },
  102. Recorder: RecorderConfig{
  103. Enabled: false,
  104. MinSNRDb: 10,
  105. MinDuration: "1s",
  106. MaxDuration: "300s",
  107. PrerollMs: 500,
  108. RecordIQ: true,
  109. RecordAudio: false,
  110. AutoDemod: true,
  111. AutoDecode: false,
  112. MaxDiskMB: 0,
  113. OutputDir: "data/recordings",
  114. RingSeconds: 8,
  115. },
  116. Decoder: DecoderConfig{},
  117. WebAddr: ":8080",
  118. EventPath: "data/events.jsonl",
  119. FrameRate: 15,
  120. WaterfallLines: 200,
  121. WebRoot: "web",
  122. }
  123. }
  124. func Load(path string) (Config, error) {
  125. cfg := Default()
  126. if b, err := os.ReadFile(autosavePath(path)); err == nil {
  127. if err := yaml.Unmarshal(b, &cfg); err == nil {
  128. return applyDefaults(cfg), nil
  129. }
  130. }
  131. b, err := os.ReadFile(path)
  132. if err != nil {
  133. return cfg, err
  134. }
  135. if err := yaml.Unmarshal(b, &cfg); err != nil {
  136. return cfg, err
  137. }
  138. return applyDefaults(cfg), nil
  139. }
  140. func applyDefaults(cfg Config) Config {
  141. if cfg.Detector.MinDurationMs <= 0 {
  142. cfg.Detector.MinDurationMs = 250
  143. }
  144. if cfg.Detector.HoldMs <= 0 {
  145. cfg.Detector.HoldMs = 500
  146. }
  147. if cfg.Detector.MinStableFrames <= 0 {
  148. cfg.Detector.MinStableFrames = 3
  149. }
  150. if cfg.Detector.GapToleranceMs <= 0 {
  151. cfg.Detector.GapToleranceMs = cfg.Detector.HoldMs
  152. }
  153. if cfg.Detector.CFARMode == "" {
  154. if cfg.Detector.CFAREnabled != nil {
  155. if *cfg.Detector.CFAREnabled {
  156. cfg.Detector.CFARMode = "OS"
  157. } else {
  158. cfg.Detector.CFARMode = "OFF"
  159. }
  160. } else {
  161. cfg.Detector.CFARMode = "GOSCA"
  162. }
  163. }
  164. if cfg.Detector.CFARGuardCells <= 0 {
  165. cfg.Detector.CFARGuardCells = 3
  166. }
  167. if cfg.Detector.CFARTrainCells <= 0 {
  168. cfg.Detector.CFARTrainCells = 24
  169. }
  170. if cfg.Detector.CFARRank <= 0 || cfg.Detector.CFARRank > 2*cfg.Detector.CFARTrainCells {
  171. cfg.Detector.CFARRank = int(math.Round(0.75 * float64(2*cfg.Detector.CFARTrainCells)))
  172. if cfg.Detector.CFARRank <= 0 {
  173. cfg.Detector.CFARRank = 1
  174. }
  175. }
  176. if cfg.Detector.CFARScaleDb <= 0 {
  177. cfg.Detector.CFARScaleDb = 6
  178. }
  179. if cfg.FrameRate <= 0 {
  180. cfg.FrameRate = 15
  181. }
  182. if cfg.WaterfallLines <= 0 {
  183. cfg.WaterfallLines = 200
  184. }
  185. if cfg.WebRoot == "" {
  186. cfg.WebRoot = "web"
  187. }
  188. if cfg.WebAddr == "" {
  189. cfg.WebAddr = ":8080"
  190. }
  191. if cfg.EventPath == "" {
  192. cfg.EventPath = "data/events.jsonl"
  193. }
  194. if cfg.SampleRate <= 0 {
  195. cfg.SampleRate = 2_048_000
  196. }
  197. if cfg.FFTSize <= 0 {
  198. cfg.FFTSize = 2048
  199. }
  200. if cfg.TunerBwKHz <= 0 {
  201. cfg.TunerBwKHz = 1536
  202. }
  203. if cfg.CenterHz == 0 {
  204. cfg.CenterHz = 100.0e6
  205. }
  206. if cfg.Recorder.OutputDir == "" {
  207. cfg.Recorder.OutputDir = "data/recordings"
  208. }
  209. if cfg.Recorder.RingSeconds <= 0 {
  210. cfg.Recorder.RingSeconds = 8
  211. }
  212. return cfg
  213. }
  214. func (c Config) FrameInterval() time.Duration {
  215. fps := c.FrameRate
  216. if fps <= 0 {
  217. fps = 15
  218. }
  219. return time.Second / time.Duration(fps)
  220. }