Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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