Wideband autonomous SDR analysis engine forked from sdr-visual-suite
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
13KB

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