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.

243 lines
7.2KB

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