Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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