Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

687 wiersze
23KB

  1. package config
  2. import (
  3. "math"
  4. "os"
  5. "strings"
  6. "time"
  7. "gopkg.in/yaml.v3"
  8. )
  9. type Band struct {
  10. Name string `yaml:"name" json:"name"`
  11. StartHz float64 `yaml:"start_hz" json:"start_hz"`
  12. EndHz float64 `yaml:"end_hz" json:"end_hz"`
  13. }
  14. type MonitorWindow struct {
  15. Label string `yaml:"label" json:"label"`
  16. Zone string `yaml:"zone" json:"zone"`
  17. StartHz float64 `yaml:"start_hz" json:"start_hz"`
  18. EndHz float64 `yaml:"end_hz" json:"end_hz"`
  19. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  20. SpanHz float64 `yaml:"span_hz" json:"span_hz"`
  21. Priority float64 `yaml:"priority" json:"priority"`
  22. AutoRecord bool `yaml:"auto_record" json:"auto_record"`
  23. AutoDecode bool `yaml:"auto_decode" json:"auto_decode"`
  24. }
  25. type DetectorConfig struct {
  26. ThresholdDb float64 `yaml:"threshold_db" json:"threshold_db"`
  27. MinDurationMs int `yaml:"min_duration_ms" json:"min_duration_ms"`
  28. HoldMs int `yaml:"hold_ms" json:"hold_ms"`
  29. EmaAlpha float64 `yaml:"ema_alpha" json:"ema_alpha"`
  30. HysteresisDb float64 `yaml:"hysteresis_db" json:"hysteresis_db"`
  31. MinStableFrames int `yaml:"min_stable_frames" json:"min_stable_frames"`
  32. GapToleranceMs int `yaml:"gap_tolerance_ms" json:"gap_tolerance_ms"`
  33. CFARMode string `yaml:"cfar_mode" json:"cfar_mode"`
  34. CFARGuardHz float64 `yaml:"cfar_guard_hz" json:"cfar_guard_hz"`
  35. CFARTrainHz float64 `yaml:"cfar_train_hz" json:"cfar_train_hz"`
  36. CFARGuardCells int `yaml:"cfar_guard_cells,omitempty" json:"cfar_guard_cells,omitempty"`
  37. CFARTrainCells int `yaml:"cfar_train_cells,omitempty" json:"cfar_train_cells,omitempty"`
  38. CFARRank int `yaml:"cfar_rank" json:"cfar_rank"`
  39. CFARScaleDb float64 `yaml:"cfar_scale_db" json:"cfar_scale_db"`
  40. CFARWrapAround bool `yaml:"cfar_wrap_around" json:"cfar_wrap_around"`
  41. EdgeMarginDb float64 `yaml:"edge_margin_db" json:"edge_margin_db"`
  42. MaxSignalBwHz float64 `yaml:"max_signal_bw_hz" json:"max_signal_bw_hz"`
  43. MergeGapHz float64 `yaml:"merge_gap_hz" json:"merge_gap_hz"`
  44. ClassHistorySize int `yaml:"class_history_size" json:"class_history_size"`
  45. ClassSwitchRatio float64 `yaml:"class_switch_ratio" json:"class_switch_ratio"`
  46. // Deprecated (backward compatibility)
  47. CFAREnabled *bool `yaml:"cfar_enabled,omitempty" json:"cfar_enabled,omitempty"`
  48. }
  49. type LogConfig struct {
  50. Level string `yaml:"level" json:"level"`
  51. Categories []string `yaml:"categories" json:"categories"`
  52. RateLimitMs int `yaml:"rate_limit_ms" json:"rate_limit_ms"`
  53. Stdout bool `yaml:"stdout" json:"stdout"`
  54. StdoutColor bool `yaml:"stdout_color" json:"stdout_color"`
  55. File string `yaml:"file" json:"file"`
  56. FileLevel string `yaml:"file_level" json:"file_level"`
  57. TimeFormat string `yaml:"time_format" json:"time_format"`
  58. DisableTime bool `yaml:"disable_time" json:"disable_time"`
  59. }
  60. type RecorderConfig struct {
  61. Enabled bool `yaml:"enabled" json:"enabled"`
  62. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  63. MinDuration string `yaml:"min_duration" json:"min_duration"`
  64. MaxDuration string `yaml:"max_duration" json:"max_duration"`
  65. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  66. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  67. RecordAudio bool `yaml:"record_audio" json:"record_audio"`
  68. AutoDemod bool `yaml:"auto_demod" json:"auto_demod"`
  69. AutoDecode bool `yaml:"auto_decode" json:"auto_decode"`
  70. MaxDiskMB int `yaml:"max_disk_mb" json:"max_disk_mb"`
  71. OutputDir string `yaml:"output_dir" json:"output_dir"`
  72. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  73. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  74. // Audio quality settings (AQ-2, AQ-3, AQ-5)
  75. DeemphasisUs float64 `yaml:"deemphasis_us" json:"deemphasis_us"` // De-emphasis time constant in µs. 50=Europe, 75=US/Japan, 0=disabled. Default: 50
  76. ExtractionTaps int `yaml:"extraction_fir_taps" json:"extraction_fir_taps"` // FIR tap count for extraction filter. Default: 101, max 301
  77. ExtractionBwMult float64 `yaml:"extraction_bw_mult" json:"extraction_bw_mult"` // BW multiplier for extraction. Default: 1.2 (20% wider than detected)
  78. DebugLiveAudio bool `yaml:"debug_live_audio" json:"debug_live_audio"`
  79. }
  80. type DecoderConfig struct {
  81. FT8Cmd string `yaml:"ft8_cmd" json:"ft8_cmd"`
  82. WSPRCmd string `yaml:"wspr_cmd" json:"wspr_cmd"`
  83. DMRCmd string `yaml:"dmr_cmd" json:"dmr_cmd"`
  84. DStarCmd string `yaml:"dstar_cmd" json:"dstar_cmd"`
  85. FSKCmd string `yaml:"fsk_cmd" json:"fsk_cmd"`
  86. PSKCmd string `yaml:"psk_cmd" json:"psk_cmd"`
  87. }
  88. type DebugConfig struct {
  89. AudioDumpEnabled bool `yaml:"audio_dump_enabled" json:"audio_dump_enabled"`
  90. CPUMonitoring bool `yaml:"cpu_monitoring" json:"cpu_monitoring"`
  91. }
  92. type PipelineGoalConfig struct {
  93. Intent string `yaml:"intent" json:"intent"`
  94. MonitorStartHz float64 `yaml:"monitor_start_hz" json:"monitor_start_hz"`
  95. MonitorEndHz float64 `yaml:"monitor_end_hz" json:"monitor_end_hz"`
  96. MonitorSpanHz float64 `yaml:"monitor_span_hz" json:"monitor_span_hz"`
  97. MonitorWindows []MonitorWindow `yaml:"monitor_windows" json:"monitor_windows"`
  98. SignalPriorities []string `yaml:"signal_priorities" json:"signal_priorities"`
  99. AutoRecordClasses []string `yaml:"auto_record_classes" json:"auto_record_classes"`
  100. AutoDecodeClasses []string `yaml:"auto_decode_classes" json:"auto_decode_classes"`
  101. }
  102. type PipelineConfig struct {
  103. Mode string `yaml:"mode" json:"mode"`
  104. Profile string `yaml:"profile,omitempty" json:"profile,omitempty"`
  105. Goals PipelineGoalConfig `yaml:"goals" json:"goals"`
  106. }
  107. type SurveillanceConfig struct {
  108. AnalysisFFTSize int `yaml:"analysis_fft_size" json:"analysis_fft_size"`
  109. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  110. Strategy string `yaml:"strategy" json:"strategy"`
  111. DisplayBins int `yaml:"display_bins" json:"display_bins"`
  112. DisplayFPS int `yaml:"display_fps" json:"display_fps"`
  113. DerivedDetection string `yaml:"derived_detection" json:"derived_detection"`
  114. }
  115. type RefinementConfig struct {
  116. Enabled bool `yaml:"enabled" json:"enabled"`
  117. MaxConcurrent int `yaml:"max_concurrent" json:"max_concurrent"`
  118. DetailFFTSize int `yaml:"detail_fft_size" json:"detail_fft_size"`
  119. MinCandidateSNRDb float64 `yaml:"min_candidate_snr_db" json:"min_candidate_snr_db"`
  120. MinSpanHz float64 `yaml:"min_span_hz" json:"min_span_hz"`
  121. MaxSpanHz float64 `yaml:"max_span_hz" json:"max_span_hz"`
  122. AutoSpan *bool `yaml:"auto_span" json:"auto_span"`
  123. }
  124. type ResourceConfig struct {
  125. PreferGPU bool `yaml:"prefer_gpu" json:"prefer_gpu"`
  126. MaxRefinementJobs int `yaml:"max_refinement_jobs" json:"max_refinement_jobs"`
  127. MaxRecordingStreams int `yaml:"max_recording_streams" json:"max_recording_streams"`
  128. MaxDecodeJobs int `yaml:"max_decode_jobs" json:"max_decode_jobs"`
  129. DecisionHoldMs int `yaml:"decision_hold_ms" json:"decision_hold_ms"`
  130. }
  131. type ProfileConfig struct {
  132. Name string `yaml:"name" json:"name"`
  133. Description string `yaml:"description" json:"description"`
  134. Pipeline *PipelineConfig `yaml:"pipeline,omitempty" json:"pipeline,omitempty"`
  135. Surveillance *SurveillanceConfig `yaml:"surveillance,omitempty" json:"surveillance,omitempty"`
  136. Refinement *RefinementConfig `yaml:"refinement,omitempty" json:"refinement,omitempty"`
  137. Resources *ResourceConfig `yaml:"resources,omitempty" json:"resources,omitempty"`
  138. }
  139. type Config struct {
  140. Bands []Band `yaml:"bands" json:"bands"`
  141. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  142. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  143. FFTSize int `yaml:"fft_size" json:"fft_size"`
  144. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  145. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  146. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  147. ClassifierMode string `yaml:"classifier_mode" json:"classifier_mode"`
  148. AGC bool `yaml:"agc" json:"agc"`
  149. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  150. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  151. Pipeline PipelineConfig `yaml:"pipeline" json:"pipeline"`
  152. Surveillance SurveillanceConfig `yaml:"surveillance" json:"surveillance"`
  153. Refinement RefinementConfig `yaml:"refinement" json:"refinement"`
  154. Resources ResourceConfig `yaml:"resources" json:"resources"`
  155. Profiles []ProfileConfig `yaml:"profiles" json:"profiles"`
  156. Detector DetectorConfig `yaml:"detector" json:"detector"`
  157. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  158. Decoder DecoderConfig `yaml:"decoder" json:"decoder"`
  159. Debug DebugConfig `yaml:"debug" json:"debug"`
  160. Logging LogConfig `yaml:"logging" json:"logging"`
  161. WebAddr string `yaml:"web_addr" json:"web_addr"`
  162. EventPath string `yaml:"event_path" json:"event_path"`
  163. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  164. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  165. WebRoot string `yaml:"web_root" json:"web_root"`
  166. }
  167. func Default() Config {
  168. return Config{
  169. Bands: []Band{
  170. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  171. },
  172. CenterHz: 100.0e6,
  173. SampleRate: 2_048_000,
  174. FFTSize: 2048,
  175. GainDb: 30,
  176. TunerBwKHz: 1536,
  177. UseGPUFFT: false,
  178. ClassifierMode: "combined",
  179. AGC: false,
  180. DCBlock: false,
  181. IQBalance: false,
  182. Pipeline: PipelineConfig{
  183. Mode: "legacy",
  184. Goals: PipelineGoalConfig{
  185. Intent: "general-monitoring",
  186. },
  187. },
  188. Surveillance: SurveillanceConfig{
  189. AnalysisFFTSize: 2048,
  190. FrameRate: 15,
  191. Strategy: "single-resolution",
  192. DisplayBins: 2048,
  193. DisplayFPS: 15,
  194. DerivedDetection: "auto",
  195. },
  196. Refinement: RefinementConfig{
  197. Enabled: true,
  198. MaxConcurrent: 8,
  199. DetailFFTSize: 0,
  200. MinCandidateSNRDb: 0,
  201. MinSpanHz: 0,
  202. MaxSpanHz: 0,
  203. AutoSpan: boolPtr(true),
  204. },
  205. Resources: ResourceConfig{
  206. PreferGPU: true,
  207. MaxRefinementJobs: 8,
  208. MaxRecordingStreams: 16,
  209. MaxDecodeJobs: 16,
  210. DecisionHoldMs: 2000,
  211. },
  212. Profiles: []ProfileConfig{
  213. {
  214. Name: "legacy",
  215. Description: "Current single-band pipeline behavior",
  216. Pipeline: &PipelineConfig{Mode: "legacy", Profile: "legacy", Goals: PipelineGoalConfig{Intent: "general-monitoring"}},
  217. Surveillance: &SurveillanceConfig{
  218. AnalysisFFTSize: 2048,
  219. FrameRate: 15,
  220. Strategy: "single-resolution",
  221. DisplayBins: 2048,
  222. DisplayFPS: 15,
  223. DerivedDetection: "auto",
  224. },
  225. Refinement: &RefinementConfig{
  226. Enabled: true,
  227. MaxConcurrent: 8,
  228. DetailFFTSize: 0,
  229. MinCandidateSNRDb: 0,
  230. MinSpanHz: 0,
  231. MaxSpanHz: 0,
  232. AutoSpan: boolPtr(true),
  233. },
  234. Resources: &ResourceConfig{
  235. PreferGPU: false,
  236. MaxRefinementJobs: 8,
  237. MaxRecordingStreams: 16,
  238. MaxDecodeJobs: 16,
  239. DecisionHoldMs: 2000,
  240. },
  241. },
  242. {
  243. Name: "wideband-balanced",
  244. Description: "Baseline multi-resolution wideband surveillance",
  245. Pipeline: &PipelineConfig{Mode: "wideband-balanced", Profile: "wideband-balanced", Goals: PipelineGoalConfig{
  246. Intent: "wideband-surveillance",
  247. SignalPriorities: []string{"digital", "wfm"},
  248. }},
  249. Surveillance: &SurveillanceConfig{
  250. AnalysisFFTSize: 4096,
  251. FrameRate: 12,
  252. Strategy: "multi-resolution",
  253. DisplayBins: 2048,
  254. DisplayFPS: 12,
  255. DerivedDetection: "auto",
  256. },
  257. Refinement: &RefinementConfig{
  258. Enabled: true,
  259. MaxConcurrent: 16,
  260. DetailFFTSize: 0,
  261. MinCandidateSNRDb: 0,
  262. MinSpanHz: 4000,
  263. MaxSpanHz: 200000,
  264. AutoSpan: boolPtr(true),
  265. },
  266. Resources: &ResourceConfig{
  267. PreferGPU: true,
  268. MaxRefinementJobs: 16,
  269. MaxRecordingStreams: 16,
  270. MaxDecodeJobs: 12,
  271. DecisionHoldMs: 2000,
  272. },
  273. },
  274. {
  275. Name: "wideband-aggressive",
  276. Description: "Higher surveillance/refinement budgets for dense wideband monitoring",
  277. Pipeline: &PipelineConfig{Mode: "wideband-aggressive", Profile: "wideband-aggressive", Goals: PipelineGoalConfig{
  278. Intent: "high-density-wideband-surveillance",
  279. SignalPriorities: []string{"digital", "wfm", "trunk"},
  280. }},
  281. Surveillance: &SurveillanceConfig{
  282. AnalysisFFTSize: 8192,
  283. FrameRate: 10,
  284. Strategy: "multi-resolution",
  285. DisplayBins: 4096,
  286. DisplayFPS: 10,
  287. DerivedDetection: "auto",
  288. },
  289. Refinement: &RefinementConfig{
  290. Enabled: true,
  291. MaxConcurrent: 32,
  292. DetailFFTSize: 0,
  293. MinCandidateSNRDb: 0,
  294. MinSpanHz: 6000,
  295. MaxSpanHz: 250000,
  296. AutoSpan: boolPtr(true),
  297. },
  298. Resources: &ResourceConfig{
  299. PreferGPU: true,
  300. MaxRefinementJobs: 32,
  301. MaxRecordingStreams: 24,
  302. MaxDecodeJobs: 16,
  303. DecisionHoldMs: 2000,
  304. },
  305. },
  306. {
  307. Name: "archive",
  308. Description: "Record-first monitoring profile",
  309. Pipeline: &PipelineConfig{Mode: "archive", Profile: "archive", Goals: PipelineGoalConfig{
  310. Intent: "archive-and-triage",
  311. SignalPriorities: []string{"wfm", "nfm", "digital"},
  312. }},
  313. Surveillance: &SurveillanceConfig{
  314. AnalysisFFTSize: 4096,
  315. FrameRate: 12,
  316. Strategy: "single-resolution",
  317. DisplayBins: 2048,
  318. DisplayFPS: 12,
  319. DerivedDetection: "auto",
  320. },
  321. Refinement: &RefinementConfig{
  322. Enabled: true,
  323. MaxConcurrent: 12,
  324. DetailFFTSize: 0,
  325. MinCandidateSNRDb: 0,
  326. MinSpanHz: 4000,
  327. MaxSpanHz: 200000,
  328. AutoSpan: boolPtr(true),
  329. },
  330. Resources: &ResourceConfig{
  331. PreferGPU: true,
  332. MaxRefinementJobs: 12,
  333. MaxRecordingStreams: 24,
  334. MaxDecodeJobs: 12,
  335. DecisionHoldMs: 2500,
  336. },
  337. },
  338. {
  339. Name: "digital-hunting",
  340. Description: "Digital-first refinement and decode focus",
  341. Pipeline: &PipelineConfig{Mode: "digital-hunting", Profile: "digital-hunting", Goals: PipelineGoalConfig{
  342. Intent: "digital-surveillance",
  343. SignalPriorities: []string{"ft8", "wspr", "fsk", "psk", "dmr"},
  344. }},
  345. Surveillance: &SurveillanceConfig{
  346. AnalysisFFTSize: 4096,
  347. FrameRate: 12,
  348. Strategy: "multi-resolution",
  349. DisplayBins: 2048,
  350. DisplayFPS: 12,
  351. DerivedDetection: "auto",
  352. },
  353. Refinement: &RefinementConfig{
  354. Enabled: true,
  355. MaxConcurrent: 16,
  356. DetailFFTSize: 0,
  357. MinCandidateSNRDb: 0,
  358. MinSpanHz: 3000,
  359. MaxSpanHz: 120000,
  360. AutoSpan: boolPtr(true),
  361. },
  362. Resources: &ResourceConfig{
  363. PreferGPU: true,
  364. MaxRefinementJobs: 16,
  365. MaxRecordingStreams: 12,
  366. MaxDecodeJobs: 16,
  367. DecisionHoldMs: 2000,
  368. },
  369. },
  370. },
  371. Detector: DetectorConfig{
  372. ThresholdDb: -20,
  373. MinDurationMs: 250,
  374. HoldMs: 500,
  375. EmaAlpha: 0.2,
  376. HysteresisDb: 3,
  377. MinStableFrames: 3,
  378. GapToleranceMs: 500,
  379. CFARMode: "GOSCA",
  380. CFARGuardHz: 500,
  381. CFARTrainHz: 5000,
  382. CFARGuardCells: 3,
  383. CFARTrainCells: 24,
  384. CFARRank: 36,
  385. CFARScaleDb: 6,
  386. CFARWrapAround: true,
  387. EdgeMarginDb: 3.0,
  388. MaxSignalBwHz: 150000,
  389. MergeGapHz: 5000,
  390. ClassHistorySize: 10,
  391. ClassSwitchRatio: 0.6,
  392. },
  393. Recorder: RecorderConfig{
  394. Enabled: false,
  395. MinSNRDb: 10,
  396. MinDuration: "1s",
  397. MaxDuration: "300s",
  398. PrerollMs: 500,
  399. RecordIQ: true,
  400. RecordAudio: false,
  401. AutoDemod: true,
  402. AutoDecode: false,
  403. MaxDiskMB: 0,
  404. OutputDir: "data/recordings",
  405. RingSeconds: 8,
  406. DeemphasisUs: 50,
  407. ExtractionTaps: 101,
  408. ExtractionBwMult: 1.2,
  409. },
  410. Decoder: DecoderConfig{},
  411. Debug: DebugConfig{
  412. AudioDumpEnabled: false,
  413. CPUMonitoring: false,
  414. },
  415. Logging: LogConfig{
  416. Level: "informal",
  417. Categories: []string{},
  418. RateLimitMs: 500,
  419. Stdout: true,
  420. StdoutColor: true,
  421. File: "logs/trace.log",
  422. FileLevel: "",
  423. TimeFormat: "15:04:05",
  424. DisableTime: false,
  425. },
  426. WebAddr: ":8080",
  427. EventPath: "data/events.jsonl",
  428. FrameRate: 15,
  429. WaterfallLines: 200,
  430. WebRoot: "web",
  431. }
  432. }
  433. func Load(path string) (Config, error) {
  434. cfg := Default()
  435. if b, err := os.ReadFile(autosavePath(path)); err == nil {
  436. if err := yaml.Unmarshal(b, &cfg); err == nil {
  437. return applyDefaults(cfg), nil
  438. }
  439. }
  440. b, err := os.ReadFile(path)
  441. if err != nil {
  442. return cfg, err
  443. }
  444. if err := yaml.Unmarshal(b, &cfg); err != nil {
  445. return cfg, err
  446. }
  447. return applyDefaults(cfg), nil
  448. }
  449. func applyDefaults(cfg Config) Config {
  450. if cfg.Detector.MinDurationMs <= 0 {
  451. cfg.Detector.MinDurationMs = 250
  452. }
  453. if cfg.Detector.HoldMs <= 0 {
  454. cfg.Detector.HoldMs = 500
  455. }
  456. if cfg.Detector.MinStableFrames <= 0 {
  457. cfg.Detector.MinStableFrames = 3
  458. }
  459. if cfg.Detector.GapToleranceMs <= 0 {
  460. cfg.Detector.GapToleranceMs = cfg.Detector.HoldMs
  461. }
  462. if cfg.Detector.CFARMode == "" {
  463. if cfg.Detector.CFAREnabled != nil {
  464. if *cfg.Detector.CFAREnabled {
  465. cfg.Detector.CFARMode = "OS"
  466. } else {
  467. cfg.Detector.CFARMode = "OFF"
  468. }
  469. } else {
  470. cfg.Detector.CFARMode = "GOSCA"
  471. }
  472. }
  473. if cfg.Detector.CFARGuardHz <= 0 && cfg.Detector.CFARGuardCells > 0 {
  474. cfg.Detector.CFARGuardHz = float64(cfg.Detector.CFARGuardCells) * 62.5
  475. }
  476. if cfg.Detector.CFARTrainHz <= 0 && cfg.Detector.CFARTrainCells > 0 {
  477. cfg.Detector.CFARTrainHz = float64(cfg.Detector.CFARTrainCells) * 62.5
  478. }
  479. if cfg.Detector.CFARGuardHz <= 0 {
  480. cfg.Detector.CFARGuardHz = 500
  481. }
  482. if cfg.Detector.CFARTrainHz <= 0 {
  483. cfg.Detector.CFARTrainHz = 5000
  484. }
  485. if cfg.Detector.CFARGuardCells <= 0 {
  486. cfg.Detector.CFARGuardCells = 3
  487. }
  488. if cfg.Detector.CFARTrainCells <= 0 {
  489. cfg.Detector.CFARTrainCells = 24
  490. }
  491. if cfg.Detector.CFARRank <= 0 || cfg.Detector.CFARRank > 2*cfg.Detector.CFARTrainCells {
  492. cfg.Detector.CFARRank = int(math.Round(0.75 * float64(2*cfg.Detector.CFARTrainCells)))
  493. if cfg.Detector.CFARRank <= 0 {
  494. cfg.Detector.CFARRank = 1
  495. }
  496. }
  497. if cfg.Detector.CFARScaleDb <= 0 {
  498. cfg.Detector.CFARScaleDb = 6
  499. }
  500. if cfg.Detector.EdgeMarginDb <= 0 {
  501. cfg.Detector.EdgeMarginDb = 3.0
  502. }
  503. if cfg.Detector.MaxSignalBwHz <= 0 {
  504. cfg.Detector.MaxSignalBwHz = 150000
  505. }
  506. if cfg.Detector.MergeGapHz <= 0 {
  507. cfg.Detector.MergeGapHz = 5000
  508. }
  509. if cfg.Detector.ClassHistorySize <= 0 {
  510. cfg.Detector.ClassHistorySize = 10
  511. }
  512. if cfg.Detector.ClassSwitchRatio <= 0 || cfg.Detector.ClassSwitchRatio > 1 {
  513. cfg.Detector.ClassSwitchRatio = 0.6
  514. }
  515. if cfg.Pipeline.Mode == "" {
  516. cfg.Pipeline.Mode = "legacy"
  517. }
  518. if cfg.Pipeline.Goals.Intent == "" {
  519. cfg.Pipeline.Goals.Intent = "general-monitoring"
  520. }
  521. if cfg.Pipeline.Goals.MonitorSpanHz <= 0 && cfg.Pipeline.Goals.MonitorStartHz != 0 && cfg.Pipeline.Goals.MonitorEndHz != 0 && cfg.Pipeline.Goals.MonitorEndHz > cfg.Pipeline.Goals.MonitorStartHz {
  522. cfg.Pipeline.Goals.MonitorSpanHz = cfg.Pipeline.Goals.MonitorEndHz - cfg.Pipeline.Goals.MonitorStartHz
  523. }
  524. if cfg.Surveillance.AnalysisFFTSize <= 0 {
  525. cfg.Surveillance.AnalysisFFTSize = cfg.FFTSize
  526. }
  527. if cfg.Surveillance.FrameRate <= 0 {
  528. cfg.Surveillance.FrameRate = cfg.FrameRate
  529. }
  530. if cfg.Surveillance.Strategy == "" {
  531. cfg.Surveillance.Strategy = "single-resolution"
  532. }
  533. if cfg.Logging.Level == "" {
  534. cfg.Logging.Level = "informal"
  535. }
  536. if cfg.Logging.RateLimitMs <= 0 {
  537. cfg.Logging.RateLimitMs = 500
  538. }
  539. if cfg.Logging.File == "" {
  540. cfg.Logging.File = "logs/trace.log"
  541. }
  542. if cfg.Surveillance.DerivedDetection == "" {
  543. cfg.Surveillance.DerivedDetection = "auto"
  544. }
  545. switch strings.ToLower(strings.TrimSpace(cfg.Surveillance.DerivedDetection)) {
  546. case "auto", "on", "off", "true", "false", "enabled", "disabled", "enable", "disable":
  547. default:
  548. cfg.Surveillance.DerivedDetection = "auto"
  549. }
  550. if cfg.Surveillance.DisplayBins <= 0 {
  551. cfg.Surveillance.DisplayBins = cfg.FFTSize
  552. }
  553. if cfg.Surveillance.DisplayFPS <= 0 {
  554. cfg.Surveillance.DisplayFPS = cfg.FrameRate
  555. }
  556. if !cfg.Refinement.Enabled {
  557. // keep explicit false if user disabled it; enable by default only when unset-like zero config
  558. if cfg.Refinement.MaxConcurrent == 0 && cfg.Refinement.MinCandidateSNRDb == 0 {
  559. cfg.Refinement.Enabled = true
  560. }
  561. }
  562. if cfg.Refinement.MaxConcurrent <= 0 {
  563. cfg.Refinement.MaxConcurrent = 8
  564. }
  565. if cfg.Refinement.DetailFFTSize <= 0 {
  566. cfg.Refinement.DetailFFTSize = cfg.Surveillance.AnalysisFFTSize
  567. }
  568. if cfg.Refinement.DetailFFTSize&(cfg.Refinement.DetailFFTSize-1) != 0 {
  569. cfg.Refinement.DetailFFTSize = cfg.Surveillance.AnalysisFFTSize
  570. }
  571. if cfg.Refinement.MinSpanHz < 0 {
  572. cfg.Refinement.MinSpanHz = 0
  573. }
  574. if cfg.Refinement.MaxSpanHz < 0 {
  575. cfg.Refinement.MaxSpanHz = 0
  576. }
  577. if cfg.Refinement.MaxSpanHz > 0 && cfg.Refinement.MinSpanHz > cfg.Refinement.MaxSpanHz {
  578. cfg.Refinement.MaxSpanHz = cfg.Refinement.MinSpanHz
  579. }
  580. if cfg.Refinement.AutoSpan == nil {
  581. cfg.Refinement.AutoSpan = boolPtr(true)
  582. }
  583. if cfg.Resources.MaxRefinementJobs <= 0 {
  584. cfg.Resources.MaxRefinementJobs = cfg.Refinement.MaxConcurrent
  585. }
  586. if cfg.Resources.MaxRecordingStreams <= 0 {
  587. cfg.Resources.MaxRecordingStreams = 16
  588. }
  589. if cfg.Resources.DecisionHoldMs < 0 {
  590. cfg.Resources.DecisionHoldMs = 0
  591. }
  592. if cfg.Resources.DecisionHoldMs == 0 {
  593. cfg.Resources.DecisionHoldMs = 2000
  594. }
  595. if cfg.FrameRate <= 0 {
  596. cfg.FrameRate = 15
  597. }
  598. if cfg.WaterfallLines <= 0 {
  599. cfg.WaterfallLines = 200
  600. }
  601. if cfg.WebRoot == "" {
  602. cfg.WebRoot = "web"
  603. }
  604. if cfg.WebAddr == "" {
  605. cfg.WebAddr = ":8080"
  606. }
  607. if cfg.EventPath == "" {
  608. cfg.EventPath = "data/events.jsonl"
  609. }
  610. if cfg.SampleRate <= 0 {
  611. cfg.SampleRate = 2_048_000
  612. }
  613. if cfg.ClassifierMode == "" {
  614. cfg.ClassifierMode = "combined"
  615. }
  616. switch cfg.ClassifierMode {
  617. case "rule", "math", "combined":
  618. default:
  619. cfg.ClassifierMode = "combined"
  620. }
  621. if cfg.FFTSize <= 0 {
  622. cfg.FFTSize = 2048
  623. }
  624. if cfg.Surveillance.AnalysisFFTSize > 0 {
  625. cfg.FFTSize = cfg.Surveillance.AnalysisFFTSize
  626. } else {
  627. cfg.Surveillance.AnalysisFFTSize = cfg.FFTSize
  628. }
  629. if cfg.TunerBwKHz <= 0 {
  630. cfg.TunerBwKHz = 1536
  631. }
  632. if cfg.CenterHz == 0 {
  633. cfg.CenterHz = 100.0e6
  634. }
  635. if cfg.Recorder.OutputDir == "" {
  636. cfg.Recorder.OutputDir = "data/recordings"
  637. }
  638. if cfg.Recorder.RingSeconds <= 0 {
  639. cfg.Recorder.RingSeconds = 8
  640. }
  641. if cfg.Recorder.DeemphasisUs == 0 {
  642. cfg.Recorder.DeemphasisUs = 50
  643. }
  644. if cfg.Recorder.ExtractionTaps <= 0 {
  645. cfg.Recorder.ExtractionTaps = 101
  646. }
  647. if cfg.Recorder.ExtractionTaps > 301 {
  648. cfg.Recorder.ExtractionTaps = 301
  649. }
  650. if cfg.Recorder.ExtractionTaps%2 == 0 {
  651. cfg.Recorder.ExtractionTaps++ // must be odd
  652. }
  653. if cfg.Recorder.ExtractionBwMult <= 0 {
  654. cfg.Recorder.ExtractionBwMult = 1.2
  655. }
  656. return cfg
  657. }
  658. func (c Config) FrameInterval() time.Duration {
  659. fps := c.FrameRate
  660. if fps <= 0 {
  661. fps = 15
  662. }
  663. return time.Second / time.Duration(fps)
  664. }