Wideband autonomous SDR analysis engine forked from sdr-visual-suite
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

274 líneas
8.4KB

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