Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

419 строки
15KB

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