No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

284 líneas
8.7KB

  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. }
  52. type DecoderConfig struct {
  53. FT8Cmd string `yaml:"ft8_cmd" json:"ft8_cmd"`
  54. WSPRCmd string `yaml:"wspr_cmd" json:"wspr_cmd"`
  55. DMRCmd string `yaml:"dmr_cmd" json:"dmr_cmd"`
  56. DStarCmd string `yaml:"dstar_cmd" json:"dstar_cmd"`
  57. FSKCmd string `yaml:"fsk_cmd" json:"fsk_cmd"`
  58. PSKCmd string `yaml:"psk_cmd" json:"psk_cmd"`
  59. }
  60. type Config struct {
  61. Bands []Band `yaml:"bands" json:"bands"`
  62. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  63. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  64. FFTSize int `yaml:"fft_size" json:"fft_size"`
  65. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  66. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  67. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  68. ClassifierMode string `yaml:"classifier_mode" json:"classifier_mode"`
  69. AGC bool `yaml:"agc" json:"agc"`
  70. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  71. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  72. Detector DetectorConfig `yaml:"detector" json:"detector"`
  73. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  74. Decoder DecoderConfig `yaml:"decoder" json:"decoder"`
  75. WebAddr string `yaml:"web_addr" json:"web_addr"`
  76. EventPath string `yaml:"event_path" json:"event_path"`
  77. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  78. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  79. WebRoot string `yaml:"web_root" json:"web_root"`
  80. }
  81. func Default() Config {
  82. return Config{
  83. Bands: []Band{
  84. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  85. },
  86. CenterHz: 100.0e6,
  87. SampleRate: 2_048_000,
  88. FFTSize: 2048,
  89. GainDb: 30,
  90. TunerBwKHz: 1536,
  91. UseGPUFFT: false,
  92. ClassifierMode: "combined",
  93. AGC: false,
  94. DCBlock: false,
  95. IQBalance: false,
  96. Detector: DetectorConfig{
  97. ThresholdDb: -20,
  98. MinDurationMs: 250,
  99. HoldMs: 500,
  100. EmaAlpha: 0.2,
  101. HysteresisDb: 3,
  102. MinStableFrames: 3,
  103. GapToleranceMs: 500,
  104. CFARMode: "GOSCA",
  105. CFARGuardHz: 500,
  106. CFARTrainHz: 5000,
  107. CFARGuardCells: 3,
  108. CFARTrainCells: 24,
  109. CFARRank: 36,
  110. CFARScaleDb: 6,
  111. CFARWrapAround: true,
  112. EdgeMarginDb: 3.0,
  113. MaxSignalBwHz: 150000,
  114. MergeGapHz: 5000,
  115. ClassHistorySize: 10,
  116. ClassSwitchRatio: 0.6,
  117. },
  118. Recorder: RecorderConfig{
  119. Enabled: false,
  120. MinSNRDb: 10,
  121. MinDuration: "1s",
  122. MaxDuration: "300s",
  123. PrerollMs: 500,
  124. RecordIQ: true,
  125. RecordAudio: false,
  126. AutoDemod: true,
  127. AutoDecode: false,
  128. MaxDiskMB: 0,
  129. OutputDir: "data/recordings",
  130. RingSeconds: 8,
  131. },
  132. Decoder: DecoderConfig{},
  133. WebAddr: ":8080",
  134. EventPath: "data/events.jsonl",
  135. FrameRate: 15,
  136. WaterfallLines: 200,
  137. WebRoot: "web",
  138. }
  139. }
  140. func Load(path string) (Config, error) {
  141. cfg := Default()
  142. if b, err := os.ReadFile(autosavePath(path)); err == nil {
  143. if err := yaml.Unmarshal(b, &cfg); err == nil {
  144. return applyDefaults(cfg), nil
  145. }
  146. }
  147. b, err := os.ReadFile(path)
  148. if err != nil {
  149. return cfg, err
  150. }
  151. if err := yaml.Unmarshal(b, &cfg); err != nil {
  152. return cfg, err
  153. }
  154. return applyDefaults(cfg), nil
  155. }
  156. func applyDefaults(cfg Config) Config {
  157. if cfg.Detector.MinDurationMs <= 0 {
  158. cfg.Detector.MinDurationMs = 250
  159. }
  160. if cfg.Detector.HoldMs <= 0 {
  161. cfg.Detector.HoldMs = 500
  162. }
  163. if cfg.Detector.MinStableFrames <= 0 {
  164. cfg.Detector.MinStableFrames = 3
  165. }
  166. if cfg.Detector.GapToleranceMs <= 0 {
  167. cfg.Detector.GapToleranceMs = cfg.Detector.HoldMs
  168. }
  169. if cfg.Detector.CFARMode == "" {
  170. if cfg.Detector.CFAREnabled != nil {
  171. if *cfg.Detector.CFAREnabled {
  172. cfg.Detector.CFARMode = "OS"
  173. } else {
  174. cfg.Detector.CFARMode = "OFF"
  175. }
  176. } else {
  177. cfg.Detector.CFARMode = "GOSCA"
  178. }
  179. }
  180. if cfg.Detector.CFARGuardHz <= 0 && cfg.Detector.CFARGuardCells > 0 {
  181. cfg.Detector.CFARGuardHz = float64(cfg.Detector.CFARGuardCells) * 62.5
  182. }
  183. if cfg.Detector.CFARTrainHz <= 0 && cfg.Detector.CFARTrainCells > 0 {
  184. cfg.Detector.CFARTrainHz = float64(cfg.Detector.CFARTrainCells) * 62.5
  185. }
  186. if cfg.Detector.CFARGuardHz <= 0 {
  187. cfg.Detector.CFARGuardHz = 500
  188. }
  189. if cfg.Detector.CFARTrainHz <= 0 {
  190. cfg.Detector.CFARTrainHz = 5000
  191. }
  192. if cfg.Detector.CFARGuardCells <= 0 {
  193. cfg.Detector.CFARGuardCells = 3
  194. }
  195. if cfg.Detector.CFARTrainCells <= 0 {
  196. cfg.Detector.CFARTrainCells = 24
  197. }
  198. if cfg.Detector.CFARRank <= 0 || cfg.Detector.CFARRank > 2*cfg.Detector.CFARTrainCells {
  199. cfg.Detector.CFARRank = int(math.Round(0.75 * float64(2*cfg.Detector.CFARTrainCells)))
  200. if cfg.Detector.CFARRank <= 0 {
  201. cfg.Detector.CFARRank = 1
  202. }
  203. }
  204. if cfg.Detector.CFARScaleDb <= 0 {
  205. cfg.Detector.CFARScaleDb = 6
  206. }
  207. if cfg.Detector.EdgeMarginDb <= 0 {
  208. cfg.Detector.EdgeMarginDb = 3.0
  209. }
  210. if cfg.Detector.MaxSignalBwHz <= 0 {
  211. cfg.Detector.MaxSignalBwHz = 150000
  212. }
  213. if cfg.Detector.MergeGapHz <= 0 {
  214. cfg.Detector.MergeGapHz = 5000
  215. }
  216. if cfg.Detector.ClassHistorySize <= 0 {
  217. cfg.Detector.ClassHistorySize = 10
  218. }
  219. if cfg.Detector.ClassSwitchRatio <= 0 || cfg.Detector.ClassSwitchRatio > 1 {
  220. cfg.Detector.ClassSwitchRatio = 0.6
  221. }
  222. if cfg.FrameRate <= 0 {
  223. cfg.FrameRate = 15
  224. }
  225. if cfg.WaterfallLines <= 0 {
  226. cfg.WaterfallLines = 200
  227. }
  228. if cfg.WebRoot == "" {
  229. cfg.WebRoot = "web"
  230. }
  231. if cfg.WebAddr == "" {
  232. cfg.WebAddr = ":8080"
  233. }
  234. if cfg.EventPath == "" {
  235. cfg.EventPath = "data/events.jsonl"
  236. }
  237. if cfg.SampleRate <= 0 {
  238. cfg.SampleRate = 2_048_000
  239. }
  240. if cfg.ClassifierMode == "" {
  241. cfg.ClassifierMode = "combined"
  242. }
  243. switch cfg.ClassifierMode {
  244. case "rule", "math", "combined":
  245. default:
  246. cfg.ClassifierMode = "combined"
  247. }
  248. if cfg.FFTSize <= 0 {
  249. cfg.FFTSize = 2048
  250. }
  251. if cfg.TunerBwKHz <= 0 {
  252. cfg.TunerBwKHz = 1536
  253. }
  254. if cfg.CenterHz == 0 {
  255. cfg.CenterHz = 100.0e6
  256. }
  257. if cfg.Recorder.OutputDir == "" {
  258. cfg.Recorder.OutputDir = "data/recordings"
  259. }
  260. if cfg.Recorder.RingSeconds <= 0 {
  261. cfg.Recorder.RingSeconds = 8
  262. }
  263. return cfg
  264. }
  265. func (c Config) FrameInterval() time.Duration {
  266. fps := c.FrameRate
  267. if fps <= 0 {
  268. fps = 15
  269. }
  270. return time.Second / time.Duration(fps)
  271. }