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.

439 lines
16KB

  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. // Audio quality settings (AQ-2, AQ-3, AQ-5)
  52. DeemphasisUs float64 `yaml:"deemphasis_us" json:"deemphasis_us"` // De-emphasis time constant in µs. 50=Europe, 75=US/Japan, 0=disabled. Default: 50
  53. ExtractionTaps int `yaml:"extraction_fir_taps" json:"extraction_fir_taps"` // FIR tap count for extraction filter. Default: 101, max 301
  54. ExtractionBwMult float64 `yaml:"extraction_bw_mult" json:"extraction_bw_mult"` // BW multiplier for extraction. Default: 1.2 (20% wider than detected)
  55. }
  56. type DecoderConfig struct {
  57. FT8Cmd string `yaml:"ft8_cmd" json:"ft8_cmd"`
  58. WSPRCmd string `yaml:"wspr_cmd" json:"wspr_cmd"`
  59. DMRCmd string `yaml:"dmr_cmd" json:"dmr_cmd"`
  60. DStarCmd string `yaml:"dstar_cmd" json:"dstar_cmd"`
  61. FSKCmd string `yaml:"fsk_cmd" json:"fsk_cmd"`
  62. PSKCmd string `yaml:"psk_cmd" json:"psk_cmd"`
  63. }
  64. type PipelineGoalConfig struct {
  65. Intent string `yaml:"intent" json:"intent"`
  66. MonitorStartHz float64 `yaml:"monitor_start_hz" json:"monitor_start_hz"`
  67. MonitorEndHz float64 `yaml:"monitor_end_hz" json:"monitor_end_hz"`
  68. MonitorSpanHz float64 `yaml:"monitor_span_hz" json:"monitor_span_hz"`
  69. SignalPriorities []string `yaml:"signal_priorities" json:"signal_priorities"`
  70. AutoRecordClasses []string `yaml:"auto_record_classes" json:"auto_record_classes"`
  71. AutoDecodeClasses []string `yaml:"auto_decode_classes" json:"auto_decode_classes"`
  72. }
  73. type PipelineConfig struct {
  74. Mode string `yaml:"mode" json:"mode"`
  75. Goals PipelineGoalConfig `yaml:"goals" json:"goals"`
  76. }
  77. type SurveillanceConfig struct {
  78. AnalysisFFTSize int `yaml:"analysis_fft_size" json:"analysis_fft_size"`
  79. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  80. Strategy string `yaml:"strategy" json:"strategy"`
  81. DisplayBins int `yaml:"display_bins" json:"display_bins"`
  82. DisplayFPS int `yaml:"display_fps" json:"display_fps"`
  83. }
  84. type RefinementConfig struct {
  85. Enabled bool `yaml:"enabled" json:"enabled"`
  86. MaxConcurrent int `yaml:"max_concurrent" json:"max_concurrent"`
  87. MinCandidateSNRDb float64 `yaml:"min_candidate_snr_db" json:"min_candidate_snr_db"`
  88. MinSpanHz float64 `yaml:"min_span_hz" json:"min_span_hz"`
  89. MaxSpanHz float64 `yaml:"max_span_hz" json:"max_span_hz"`
  90. }
  91. type ResourceConfig struct {
  92. PreferGPU bool `yaml:"prefer_gpu" json:"prefer_gpu"`
  93. MaxRefinementJobs int `yaml:"max_refinement_jobs" json:"max_refinement_jobs"`
  94. MaxRecordingStreams int `yaml:"max_recording_streams" json:"max_recording_streams"`
  95. }
  96. type ProfileConfig struct {
  97. Name string `yaml:"name" json:"name"`
  98. Description string `yaml:"description" json:"description"`
  99. Pipeline *PipelineConfig `yaml:"pipeline,omitempty" json:"pipeline,omitempty"`
  100. Surveillance *SurveillanceConfig `yaml:"surveillance,omitempty" json:"surveillance,omitempty"`
  101. Refinement *RefinementConfig `yaml:"refinement,omitempty" json:"refinement,omitempty"`
  102. Resources *ResourceConfig `yaml:"resources,omitempty" json:"resources,omitempty"`
  103. }
  104. type Config struct {
  105. Bands []Band `yaml:"bands" json:"bands"`
  106. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  107. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  108. FFTSize int `yaml:"fft_size" json:"fft_size"`
  109. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  110. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  111. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  112. ClassifierMode string `yaml:"classifier_mode" json:"classifier_mode"`
  113. AGC bool `yaml:"agc" json:"agc"`
  114. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  115. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  116. Pipeline PipelineConfig `yaml:"pipeline" json:"pipeline"`
  117. Surveillance SurveillanceConfig `yaml:"surveillance" json:"surveillance"`
  118. Refinement RefinementConfig `yaml:"refinement" json:"refinement"`
  119. Resources ResourceConfig `yaml:"resources" json:"resources"`
  120. Profiles []ProfileConfig `yaml:"profiles" json:"profiles"`
  121. Detector DetectorConfig `yaml:"detector" json:"detector"`
  122. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  123. Decoder DecoderConfig `yaml:"decoder" json:"decoder"`
  124. WebAddr string `yaml:"web_addr" json:"web_addr"`
  125. EventPath string `yaml:"event_path" json:"event_path"`
  126. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  127. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  128. WebRoot string `yaml:"web_root" json:"web_root"`
  129. }
  130. func Default() Config {
  131. return Config{
  132. Bands: []Band{
  133. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  134. },
  135. CenterHz: 100.0e6,
  136. SampleRate: 2_048_000,
  137. FFTSize: 2048,
  138. GainDb: 30,
  139. TunerBwKHz: 1536,
  140. UseGPUFFT: false,
  141. ClassifierMode: "combined",
  142. AGC: false,
  143. DCBlock: false,
  144. IQBalance: false,
  145. Pipeline: PipelineConfig{
  146. Mode: "legacy",
  147. Goals: PipelineGoalConfig{
  148. Intent: "general-monitoring",
  149. },
  150. },
  151. Surveillance: SurveillanceConfig{
  152. AnalysisFFTSize: 2048,
  153. FrameRate: 15,
  154. Strategy: "single-resolution",
  155. DisplayBins: 2048,
  156. DisplayFPS: 15,
  157. },
  158. Refinement: RefinementConfig{
  159. Enabled: true,
  160. MaxConcurrent: 8,
  161. MinCandidateSNRDb: 0,
  162. MinSpanHz: 0,
  163. MaxSpanHz: 0,
  164. },
  165. Resources: ResourceConfig{
  166. PreferGPU: true,
  167. MaxRefinementJobs: 8,
  168. MaxRecordingStreams: 16,
  169. },
  170. Profiles: []ProfileConfig{
  171. {Name: "legacy", Description: "Current single-band pipeline behavior", Pipeline: &PipelineConfig{Mode: "legacy", Goals: PipelineGoalConfig{Intent: "general-monitoring"}}},
  172. {Name: "wideband-balanced", Description: "Prepared baseline for scalable wideband surveillance", Pipeline: &PipelineConfig{Mode: "wideband-balanced", Goals: PipelineGoalConfig{Intent: "wideband-surveillance"}}},
  173. {Name: "wideband-aggressive", Description: "Higher surveillance/refinement budgets for future broad-span monitoring", Pipeline: &PipelineConfig{Mode: "wideband-aggressive", Goals: PipelineGoalConfig{Intent: "high-density-wideband-surveillance"}}},
  174. {Name: "archive", Description: "Record-first monitoring profile", Pipeline: &PipelineConfig{Mode: "archive", Goals: PipelineGoalConfig{Intent: "archive-and-triage"}}},
  175. },
  176. Detector: DetectorConfig{
  177. ThresholdDb: -20,
  178. MinDurationMs: 250,
  179. HoldMs: 500,
  180. EmaAlpha: 0.2,
  181. HysteresisDb: 3,
  182. MinStableFrames: 3,
  183. GapToleranceMs: 500,
  184. CFARMode: "GOSCA",
  185. CFARGuardHz: 500,
  186. CFARTrainHz: 5000,
  187. CFARGuardCells: 3,
  188. CFARTrainCells: 24,
  189. CFARRank: 36,
  190. CFARScaleDb: 6,
  191. CFARWrapAround: true,
  192. EdgeMarginDb: 3.0,
  193. MaxSignalBwHz: 150000,
  194. MergeGapHz: 5000,
  195. ClassHistorySize: 10,
  196. ClassSwitchRatio: 0.6,
  197. },
  198. Recorder: RecorderConfig{
  199. Enabled: false,
  200. MinSNRDb: 10,
  201. MinDuration: "1s",
  202. MaxDuration: "300s",
  203. PrerollMs: 500,
  204. RecordIQ: true,
  205. RecordAudio: false,
  206. AutoDemod: true,
  207. AutoDecode: false,
  208. MaxDiskMB: 0,
  209. OutputDir: "data/recordings",
  210. RingSeconds: 8,
  211. DeemphasisUs: 50,
  212. ExtractionTaps: 101,
  213. ExtractionBwMult: 1.2,
  214. },
  215. Decoder: DecoderConfig{},
  216. WebAddr: ":8080",
  217. EventPath: "data/events.jsonl",
  218. FrameRate: 15,
  219. WaterfallLines: 200,
  220. WebRoot: "web",
  221. }
  222. }
  223. func Load(path string) (Config, error) {
  224. cfg := Default()
  225. if b, err := os.ReadFile(autosavePath(path)); err == nil {
  226. if err := yaml.Unmarshal(b, &cfg); err == nil {
  227. return applyDefaults(cfg), nil
  228. }
  229. }
  230. b, err := os.ReadFile(path)
  231. if err != nil {
  232. return cfg, err
  233. }
  234. if err := yaml.Unmarshal(b, &cfg); err != nil {
  235. return cfg, err
  236. }
  237. return applyDefaults(cfg), nil
  238. }
  239. func applyDefaults(cfg Config) Config {
  240. if cfg.Detector.MinDurationMs <= 0 {
  241. cfg.Detector.MinDurationMs = 250
  242. }
  243. if cfg.Detector.HoldMs <= 0 {
  244. cfg.Detector.HoldMs = 500
  245. }
  246. if cfg.Detector.MinStableFrames <= 0 {
  247. cfg.Detector.MinStableFrames = 3
  248. }
  249. if cfg.Detector.GapToleranceMs <= 0 {
  250. cfg.Detector.GapToleranceMs = cfg.Detector.HoldMs
  251. }
  252. if cfg.Detector.CFARMode == "" {
  253. if cfg.Detector.CFAREnabled != nil {
  254. if *cfg.Detector.CFAREnabled {
  255. cfg.Detector.CFARMode = "OS"
  256. } else {
  257. cfg.Detector.CFARMode = "OFF"
  258. }
  259. } else {
  260. cfg.Detector.CFARMode = "GOSCA"
  261. }
  262. }
  263. if cfg.Detector.CFARGuardHz <= 0 && cfg.Detector.CFARGuardCells > 0 {
  264. cfg.Detector.CFARGuardHz = float64(cfg.Detector.CFARGuardCells) * 62.5
  265. }
  266. if cfg.Detector.CFARTrainHz <= 0 && cfg.Detector.CFARTrainCells > 0 {
  267. cfg.Detector.CFARTrainHz = float64(cfg.Detector.CFARTrainCells) * 62.5
  268. }
  269. if cfg.Detector.CFARGuardHz <= 0 {
  270. cfg.Detector.CFARGuardHz = 500
  271. }
  272. if cfg.Detector.CFARTrainHz <= 0 {
  273. cfg.Detector.CFARTrainHz = 5000
  274. }
  275. if cfg.Detector.CFARGuardCells <= 0 {
  276. cfg.Detector.CFARGuardCells = 3
  277. }
  278. if cfg.Detector.CFARTrainCells <= 0 {
  279. cfg.Detector.CFARTrainCells = 24
  280. }
  281. if cfg.Detector.CFARRank <= 0 || cfg.Detector.CFARRank > 2*cfg.Detector.CFARTrainCells {
  282. cfg.Detector.CFARRank = int(math.Round(0.75 * float64(2*cfg.Detector.CFARTrainCells)))
  283. if cfg.Detector.CFARRank <= 0 {
  284. cfg.Detector.CFARRank = 1
  285. }
  286. }
  287. if cfg.Detector.CFARScaleDb <= 0 {
  288. cfg.Detector.CFARScaleDb = 6
  289. }
  290. if cfg.Detector.EdgeMarginDb <= 0 {
  291. cfg.Detector.EdgeMarginDb = 3.0
  292. }
  293. if cfg.Detector.MaxSignalBwHz <= 0 {
  294. cfg.Detector.MaxSignalBwHz = 150000
  295. }
  296. if cfg.Detector.MergeGapHz <= 0 {
  297. cfg.Detector.MergeGapHz = 5000
  298. }
  299. if cfg.Detector.ClassHistorySize <= 0 {
  300. cfg.Detector.ClassHistorySize = 10
  301. }
  302. if cfg.Detector.ClassSwitchRatio <= 0 || cfg.Detector.ClassSwitchRatio > 1 {
  303. cfg.Detector.ClassSwitchRatio = 0.6
  304. }
  305. if cfg.Pipeline.Mode == "" {
  306. cfg.Pipeline.Mode = "legacy"
  307. }
  308. if cfg.Pipeline.Goals.Intent == "" {
  309. cfg.Pipeline.Goals.Intent = "general-monitoring"
  310. }
  311. if cfg.Pipeline.Goals.MonitorSpanHz <= 0 && cfg.Pipeline.Goals.MonitorStartHz != 0 && cfg.Pipeline.Goals.MonitorEndHz != 0 && cfg.Pipeline.Goals.MonitorEndHz > cfg.Pipeline.Goals.MonitorStartHz {
  312. cfg.Pipeline.Goals.MonitorSpanHz = cfg.Pipeline.Goals.MonitorEndHz - cfg.Pipeline.Goals.MonitorStartHz
  313. }
  314. if cfg.Surveillance.AnalysisFFTSize <= 0 {
  315. cfg.Surveillance.AnalysisFFTSize = cfg.FFTSize
  316. }
  317. if cfg.Surveillance.FrameRate <= 0 {
  318. cfg.Surveillance.FrameRate = cfg.FrameRate
  319. }
  320. if cfg.Surveillance.Strategy == "" {
  321. cfg.Surveillance.Strategy = "single-resolution"
  322. }
  323. if cfg.Surveillance.DisplayBins <= 0 {
  324. cfg.Surveillance.DisplayBins = cfg.FFTSize
  325. }
  326. if cfg.Surveillance.DisplayFPS <= 0 {
  327. cfg.Surveillance.DisplayFPS = cfg.FrameRate
  328. }
  329. if !cfg.Refinement.Enabled {
  330. // keep explicit false if user disabled it; enable by default only when unset-like zero config
  331. if cfg.Refinement.MaxConcurrent == 0 && cfg.Refinement.MinCandidateSNRDb == 0 {
  332. cfg.Refinement.Enabled = true
  333. }
  334. }
  335. if cfg.Refinement.MaxConcurrent <= 0 {
  336. cfg.Refinement.MaxConcurrent = 8
  337. }
  338. if cfg.Refinement.MinSpanHz < 0 {
  339. cfg.Refinement.MinSpanHz = 0
  340. }
  341. if cfg.Refinement.MaxSpanHz < 0 {
  342. cfg.Refinement.MaxSpanHz = 0
  343. }
  344. if cfg.Resources.MaxRefinementJobs <= 0 {
  345. cfg.Resources.MaxRefinementJobs = cfg.Refinement.MaxConcurrent
  346. }
  347. if cfg.Resources.MaxRecordingStreams <= 0 {
  348. cfg.Resources.MaxRecordingStreams = 16
  349. }
  350. if cfg.FrameRate <= 0 {
  351. cfg.FrameRate = 15
  352. }
  353. if cfg.WaterfallLines <= 0 {
  354. cfg.WaterfallLines = 200
  355. }
  356. if cfg.WebRoot == "" {
  357. cfg.WebRoot = "web"
  358. }
  359. if cfg.WebAddr == "" {
  360. cfg.WebAddr = ":8080"
  361. }
  362. if cfg.EventPath == "" {
  363. cfg.EventPath = "data/events.jsonl"
  364. }
  365. if cfg.SampleRate <= 0 {
  366. cfg.SampleRate = 2_048_000
  367. }
  368. if cfg.ClassifierMode == "" {
  369. cfg.ClassifierMode = "combined"
  370. }
  371. switch cfg.ClassifierMode {
  372. case "rule", "math", "combined":
  373. default:
  374. cfg.ClassifierMode = "combined"
  375. }
  376. if cfg.FFTSize <= 0 {
  377. cfg.FFTSize = 2048
  378. }
  379. if cfg.Surveillance.AnalysisFFTSize > 0 {
  380. cfg.FFTSize = cfg.Surveillance.AnalysisFFTSize
  381. } else {
  382. cfg.Surveillance.AnalysisFFTSize = cfg.FFTSize
  383. }
  384. if cfg.TunerBwKHz <= 0 {
  385. cfg.TunerBwKHz = 1536
  386. }
  387. if cfg.CenterHz == 0 {
  388. cfg.CenterHz = 100.0e6
  389. }
  390. if cfg.Recorder.OutputDir == "" {
  391. cfg.Recorder.OutputDir = "data/recordings"
  392. }
  393. if cfg.Recorder.RingSeconds <= 0 {
  394. cfg.Recorder.RingSeconds = 8
  395. }
  396. if cfg.Recorder.DeemphasisUs == 0 {
  397. cfg.Recorder.DeemphasisUs = 50
  398. }
  399. if cfg.Recorder.ExtractionTaps <= 0 {
  400. cfg.Recorder.ExtractionTaps = 101
  401. }
  402. if cfg.Recorder.ExtractionTaps > 301 {
  403. cfg.Recorder.ExtractionTaps = 301
  404. }
  405. if cfg.Recorder.ExtractionTaps%2 == 0 {
  406. cfg.Recorder.ExtractionTaps++ // must be odd
  407. }
  408. if cfg.Recorder.ExtractionBwMult <= 0 {
  409. cfg.Recorder.ExtractionBwMult = 1.2
  410. }
  411. return cfg
  412. }
  413. func (c Config) FrameInterval() time.Duration {
  414. fps := c.FrameRate
  415. if fps <= 0 {
  416. fps = 15
  417. }
  418. return time.Second / time.Duration(fps)
  419. }