Wideband autonomous SDR analysis engine forked from sdr-visual-suite
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

953 行
29KB

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "sdr-wideband-suite/internal/classifier"
  10. "sdr-wideband-suite/internal/config"
  11. "sdr-wideband-suite/internal/demod"
  12. "sdr-wideband-suite/internal/detector"
  13. "sdr-wideband-suite/internal/dsp"
  14. fftutil "sdr-wideband-suite/internal/fft"
  15. "sdr-wideband-suite/internal/fft/gpufft"
  16. "sdr-wideband-suite/internal/pipeline"
  17. "sdr-wideband-suite/internal/rds"
  18. "sdr-wideband-suite/internal/recorder"
  19. )
  20. type rdsState struct {
  21. dec rds.Decoder
  22. result rds.Result
  23. lastDecode time.Time
  24. busy int32
  25. mu sync.Mutex
  26. }
  27. type dspRuntime struct {
  28. cfg config.Config
  29. det *detector.Detector
  30. derivedDetectors map[string]*derivedDetector
  31. nextDerivedBase int64
  32. window []float64
  33. plan *fftutil.CmplxPlan
  34. detailWindow []float64
  35. detailPlan *fftutil.CmplxPlan
  36. detailFFT int
  37. survWindows map[int][]float64
  38. survPlans map[int]*fftutil.CmplxPlan
  39. survFIR map[int][]float64
  40. dcEnabled bool
  41. iqEnabled bool
  42. useGPU bool
  43. gpuEngine *gpufft.Engine
  44. rdsMap map[int64]*rdsState
  45. streamPhaseState map[int64]*streamExtractState
  46. streamOverlap *streamIQOverlap
  47. arbiter *pipeline.Arbiter
  48. arbitration pipeline.ArbitrationState
  49. gotSamples bool
  50. }
  51. type spectrumArtifacts struct {
  52. allIQ []complex64
  53. surveillanceIQ []complex64
  54. detailIQ []complex64
  55. surveillanceSpectrum []float64
  56. surveillanceSpectra []pipeline.SurveillanceLevelSpectrum
  57. surveillancePlan surveillancePlan
  58. detailSpectrum []float64
  59. finished []detector.Event
  60. detected []detector.Signal
  61. thresholds []float64
  62. noiseFloor float64
  63. now time.Time
  64. }
  65. type derivedDetector struct {
  66. det *detector.Detector
  67. sampleRate int
  68. fftSize int
  69. idBase int64
  70. }
  71. type surveillanceLevelSpec struct {
  72. Level pipeline.AnalysisLevel
  73. Decim int
  74. AllowGPU bool
  75. }
  76. type surveillancePlan struct {
  77. Primary pipeline.AnalysisLevel
  78. Levels []pipeline.AnalysisLevel
  79. LevelSet pipeline.SurveillanceLevelSet
  80. Presentation pipeline.AnalysisLevel
  81. Context pipeline.AnalysisContext
  82. Specs []surveillanceLevelSpec
  83. }
  84. const derivedIDBlock = int64(1_000_000_000)
  85. func newDSPRuntime(cfg config.Config, det *detector.Detector, window []float64, gpuState *gpuStatus) *dspRuntime {
  86. detailFFT := cfg.Refinement.DetailFFTSize
  87. if detailFFT <= 0 {
  88. detailFFT = cfg.FFTSize
  89. }
  90. rt := &dspRuntime{
  91. cfg: cfg,
  92. det: det,
  93. derivedDetectors: map[string]*derivedDetector{},
  94. nextDerivedBase: -derivedIDBlock,
  95. window: window,
  96. plan: fftutil.NewCmplxPlan(cfg.FFTSize),
  97. detailWindow: fftutil.Hann(detailFFT),
  98. detailPlan: fftutil.NewCmplxPlan(detailFFT),
  99. detailFFT: detailFFT,
  100. survWindows: map[int][]float64{},
  101. survPlans: map[int]*fftutil.CmplxPlan{},
  102. survFIR: map[int][]float64{},
  103. dcEnabled: cfg.DCBlock,
  104. iqEnabled: cfg.IQBalance,
  105. useGPU: cfg.UseGPUFFT,
  106. rdsMap: map[int64]*rdsState{},
  107. streamPhaseState: map[int64]*streamExtractState{},
  108. streamOverlap: &streamIQOverlap{},
  109. arbiter: pipeline.NewArbiter(),
  110. }
  111. if rt.useGPU && gpuState != nil {
  112. snap := gpuState.snapshot()
  113. if snap.Available {
  114. if eng, err := gpufft.New(cfg.FFTSize); err == nil {
  115. rt.gpuEngine = eng
  116. gpuState.set(true, nil)
  117. } else {
  118. gpuState.set(false, err)
  119. rt.useGPU = false
  120. }
  121. }
  122. }
  123. return rt
  124. }
  125. func (rt *dspRuntime) applyUpdate(upd dspUpdate, srcMgr *sourceManager, rec *recorder.Manager, gpuState *gpuStatus) {
  126. prevFFT := rt.cfg.FFTSize
  127. prevSampleRate := rt.cfg.SampleRate
  128. prevUseGPU := rt.useGPU
  129. prevDetailFFT := rt.detailFFT
  130. rt.cfg = upd.cfg
  131. if rec != nil {
  132. rec.Update(rt.cfg.SampleRate, rt.cfg.FFTSize, recorder.Policy{
  133. Enabled: rt.cfg.Recorder.Enabled,
  134. MinSNRDb: rt.cfg.Recorder.MinSNRDb,
  135. MinDuration: mustParseDuration(rt.cfg.Recorder.MinDuration, 1*time.Second),
  136. MaxDuration: mustParseDuration(rt.cfg.Recorder.MaxDuration, 300*time.Second),
  137. PrerollMs: rt.cfg.Recorder.PrerollMs,
  138. RecordIQ: rt.cfg.Recorder.RecordIQ,
  139. RecordAudio: rt.cfg.Recorder.RecordAudio,
  140. AutoDemod: rt.cfg.Recorder.AutoDemod,
  141. AutoDecode: rt.cfg.Recorder.AutoDecode,
  142. MaxDiskMB: rt.cfg.Recorder.MaxDiskMB,
  143. OutputDir: rt.cfg.Recorder.OutputDir,
  144. ClassFilter: rt.cfg.Recorder.ClassFilter,
  145. RingSeconds: rt.cfg.Recorder.RingSeconds,
  146. DeemphasisUs: rt.cfg.Recorder.DeemphasisUs,
  147. ExtractionTaps: rt.cfg.Recorder.ExtractionTaps,
  148. ExtractionBwMult: rt.cfg.Recorder.ExtractionBwMult,
  149. }, rt.cfg.CenterHz, buildDecoderMap(rt.cfg))
  150. }
  151. if upd.det != nil {
  152. rt.det = upd.det
  153. }
  154. if upd.window != nil {
  155. rt.window = upd.window
  156. rt.plan = fftutil.NewCmplxPlan(rt.cfg.FFTSize)
  157. }
  158. detailFFT := rt.cfg.Refinement.DetailFFTSize
  159. if detailFFT <= 0 {
  160. detailFFT = rt.cfg.FFTSize
  161. }
  162. if detailFFT != prevDetailFFT {
  163. rt.detailFFT = detailFFT
  164. rt.detailWindow = fftutil.Hann(detailFFT)
  165. rt.detailPlan = fftutil.NewCmplxPlan(detailFFT)
  166. }
  167. if prevSampleRate != rt.cfg.SampleRate {
  168. rt.survFIR = map[int][]float64{}
  169. }
  170. if prevFFT != rt.cfg.FFTSize {
  171. rt.survWindows = map[int][]float64{}
  172. rt.survPlans = map[int]*fftutil.CmplxPlan{}
  173. }
  174. if upd.det != nil || prevSampleRate != rt.cfg.SampleRate || prevFFT != rt.cfg.FFTSize {
  175. rt.derivedDetectors = map[string]*derivedDetector{}
  176. rt.nextDerivedBase = -derivedIDBlock
  177. }
  178. rt.dcEnabled = upd.dcBlock
  179. rt.iqEnabled = upd.iqBalance
  180. if rt.cfg.FFTSize != prevFFT || rt.cfg.UseGPUFFT != prevUseGPU {
  181. srcMgr.Flush()
  182. rt.gotSamples = false
  183. if rt.gpuEngine != nil {
  184. rt.gpuEngine.Close()
  185. rt.gpuEngine = nil
  186. }
  187. rt.useGPU = rt.cfg.UseGPUFFT
  188. if rt.useGPU && gpuState != nil {
  189. snap := gpuState.snapshot()
  190. if snap.Available {
  191. if eng, err := gpufft.New(rt.cfg.FFTSize); err == nil {
  192. rt.gpuEngine = eng
  193. gpuState.set(true, nil)
  194. } else {
  195. gpuState.set(false, err)
  196. rt.useGPU = false
  197. }
  198. } else {
  199. gpuState.set(false, nil)
  200. rt.useGPU = false
  201. }
  202. } else if gpuState != nil {
  203. gpuState.set(false, nil)
  204. }
  205. }
  206. }
  207. func (rt *dspRuntime) spectrumFromIQ(iq []complex64, gpuState *gpuStatus) []float64 {
  208. return rt.spectrumFromIQWithPlan(iq, rt.window, rt.plan, gpuState, true)
  209. }
  210. func (rt *dspRuntime) spectrumFromIQWithPlan(iq []complex64, window []float64, plan *fftutil.CmplxPlan, gpuState *gpuStatus, allowGPU bool) []float64 {
  211. if len(iq) == 0 {
  212. return nil
  213. }
  214. if allowGPU && rt.useGPU && rt.gpuEngine != nil {
  215. gpuBuf := make([]complex64, len(iq))
  216. if len(window) == len(iq) {
  217. for i := 0; i < len(iq); i++ {
  218. v := iq[i]
  219. w := float32(window[i])
  220. gpuBuf[i] = complex(real(v)*w, imag(v)*w)
  221. }
  222. } else {
  223. copy(gpuBuf, iq)
  224. }
  225. out, err := rt.gpuEngine.Exec(gpuBuf)
  226. if err != nil {
  227. if gpuState != nil {
  228. gpuState.set(false, err)
  229. }
  230. rt.useGPU = false
  231. return fftutil.SpectrumWithPlan(gpuBuf, nil, plan)
  232. }
  233. return fftutil.SpectrumFromFFT(out)
  234. }
  235. return fftutil.SpectrumWithPlan(iq, window, plan)
  236. }
  237. func (rt *dspRuntime) windowForFFT(fftSize int) []float64 {
  238. if fftSize <= 0 {
  239. return nil
  240. }
  241. if fftSize == rt.cfg.FFTSize {
  242. return rt.window
  243. }
  244. if rt.survWindows == nil {
  245. rt.survWindows = map[int][]float64{}
  246. }
  247. if window, ok := rt.survWindows[fftSize]; ok {
  248. return window
  249. }
  250. window := fftutil.Hann(fftSize)
  251. rt.survWindows[fftSize] = window
  252. return window
  253. }
  254. func (rt *dspRuntime) planForFFT(fftSize int) *fftutil.CmplxPlan {
  255. if fftSize <= 0 {
  256. return nil
  257. }
  258. if fftSize == rt.cfg.FFTSize {
  259. return rt.plan
  260. }
  261. if rt.survPlans == nil {
  262. rt.survPlans = map[int]*fftutil.CmplxPlan{}
  263. }
  264. if plan, ok := rt.survPlans[fftSize]; ok {
  265. return plan
  266. }
  267. plan := fftutil.NewCmplxPlan(fftSize)
  268. rt.survPlans[fftSize] = plan
  269. return plan
  270. }
  271. func (rt *dspRuntime) spectrumForLevel(iq []complex64, fftSize int, gpuState *gpuStatus, allowGPU bool) []float64 {
  272. if len(iq) == 0 || fftSize <= 0 {
  273. return nil
  274. }
  275. if len(iq) > fftSize {
  276. iq = iq[len(iq)-fftSize:]
  277. }
  278. window := rt.windowForFFT(fftSize)
  279. plan := rt.planForFFT(fftSize)
  280. return rt.spectrumFromIQWithPlan(iq, window, plan, gpuState, allowGPU)
  281. }
  282. func sanitizeSpectrum(spectrum []float64) {
  283. for i := range spectrum {
  284. if math.IsNaN(spectrum[i]) || math.IsInf(spectrum[i], 0) {
  285. spectrum[i] = -200
  286. }
  287. }
  288. }
  289. func (rt *dspRuntime) decimationTaps(factor int) []float64 {
  290. if factor <= 1 {
  291. return nil
  292. }
  293. if rt.survFIR == nil {
  294. rt.survFIR = map[int][]float64{}
  295. }
  296. if taps, ok := rt.survFIR[factor]; ok {
  297. return taps
  298. }
  299. cutoff := float64(rt.cfg.SampleRate/factor) * 0.5 * 0.8
  300. taps := dsp.LowpassFIR(cutoff, rt.cfg.SampleRate, 101)
  301. rt.survFIR[factor] = taps
  302. return taps
  303. }
  304. func (rt *dspRuntime) decimateSurveillanceIQ(iq []complex64, factor int) []complex64 {
  305. if factor <= 1 {
  306. return iq
  307. }
  308. taps := rt.decimationTaps(factor)
  309. if len(taps) == 0 {
  310. return dsp.Decimate(iq, factor)
  311. }
  312. filtered := dsp.ApplyFIR(iq, taps)
  313. return dsp.Decimate(filtered, factor)
  314. }
  315. func (rt *dspRuntime) captureSpectrum(srcMgr *sourceManager, rec *recorder.Manager, dcBlocker *dsp.DCBlocker, gpuState *gpuStatus) (*spectrumArtifacts, error) {
  316. required := rt.cfg.FFTSize
  317. if rt.detailFFT > required {
  318. required = rt.detailFFT
  319. }
  320. available := required
  321. st := srcMgr.Stats()
  322. if st.BufferSamples > required {
  323. available = (st.BufferSamples / required) * required
  324. if available < required {
  325. available = required
  326. }
  327. }
  328. allIQ, err := srcMgr.ReadIQ(available)
  329. if err != nil {
  330. return nil, err
  331. }
  332. if rec != nil {
  333. rec.Ingest(time.Now(), allIQ)
  334. }
  335. survIQ := allIQ
  336. if len(allIQ) > rt.cfg.FFTSize {
  337. survIQ = allIQ[len(allIQ)-rt.cfg.FFTSize:]
  338. }
  339. detailIQ := survIQ
  340. if rt.detailFFT > 0 && len(allIQ) >= rt.detailFFT {
  341. detailIQ = allIQ[len(allIQ)-rt.detailFFT:]
  342. }
  343. if rt.dcEnabled {
  344. dcBlocker.Apply(allIQ)
  345. }
  346. if rt.iqEnabled {
  347. dsp.IQBalance(survIQ)
  348. if !sameIQBuffer(detailIQ, survIQ) {
  349. detailIQ = append([]complex64(nil), detailIQ...)
  350. dsp.IQBalance(detailIQ)
  351. }
  352. }
  353. survSpectrum := rt.spectrumFromIQ(survIQ, gpuState)
  354. sanitizeSpectrum(survSpectrum)
  355. detailSpectrum := survSpectrum
  356. if !sameIQBuffer(detailIQ, survIQ) {
  357. detailSpectrum = rt.spectrumFromIQWithPlan(detailIQ, rt.detailWindow, rt.detailPlan, gpuState, false)
  358. sanitizeSpectrum(detailSpectrum)
  359. }
  360. policy := pipeline.PolicyFromConfig(rt.cfg)
  361. plan := rt.buildSurveillancePlan(policy)
  362. surveillanceSpectra := make([]pipeline.SurveillanceLevelSpectrum, 0, len(plan.Specs))
  363. for _, spec := range plan.Specs {
  364. if spec.Level.FFTSize <= 0 {
  365. continue
  366. }
  367. var spectrum []float64
  368. if spec.Decim <= 1 {
  369. if spec.Level.FFTSize == len(survSpectrum) {
  370. spectrum = survSpectrum
  371. } else {
  372. spectrum = rt.spectrumForLevel(survIQ, spec.Level.FFTSize, gpuState, spec.AllowGPU)
  373. sanitizeSpectrum(spectrum)
  374. }
  375. } else {
  376. required := spec.Level.FFTSize * spec.Decim
  377. if required > len(survIQ) {
  378. continue
  379. }
  380. src := survIQ
  381. if len(src) > required {
  382. src = src[len(src)-required:]
  383. }
  384. decimated := rt.decimateSurveillanceIQ(src, spec.Decim)
  385. spectrum = rt.spectrumForLevel(decimated, spec.Level.FFTSize, gpuState, false)
  386. sanitizeSpectrum(spectrum)
  387. }
  388. if len(spectrum) == 0 {
  389. continue
  390. }
  391. surveillanceSpectra = append(surveillanceSpectra, pipeline.SurveillanceLevelSpectrum{Level: spec.Level, Spectrum: spectrum})
  392. }
  393. now := time.Now()
  394. finished, detected := rt.det.Process(now, survSpectrum, rt.cfg.CenterHz)
  395. return &spectrumArtifacts{
  396. allIQ: allIQ,
  397. surveillanceIQ: survIQ,
  398. detailIQ: detailIQ,
  399. surveillanceSpectrum: survSpectrum,
  400. surveillanceSpectra: surveillanceSpectra,
  401. surveillancePlan: plan,
  402. detailSpectrum: detailSpectrum,
  403. finished: finished,
  404. detected: detected,
  405. thresholds: rt.det.LastThresholds(),
  406. noiseFloor: rt.det.LastNoiseFloor(),
  407. now: now,
  408. }, nil
  409. }
  410. func (rt *dspRuntime) buildSurveillanceResult(art *spectrumArtifacts) pipeline.SurveillanceResult {
  411. if art == nil {
  412. return pipeline.SurveillanceResult{}
  413. }
  414. policy := pipeline.PolicyFromConfig(rt.cfg)
  415. plan := art.surveillancePlan
  416. if plan.Primary.Name == "" {
  417. plan = rt.buildSurveillancePlan(policy)
  418. }
  419. primaryCandidates := pipeline.CandidatesFromSignalsWithLevel(art.detected, "surveillance-detector", plan.Primary)
  420. derivedCandidates := rt.detectDerivedCandidates(art, plan)
  421. candidates := pipeline.FuseCandidates(primaryCandidates, derivedCandidates)
  422. scheduled := pipeline.ScheduleCandidates(candidates, policy)
  423. return pipeline.SurveillanceResult{
  424. Level: plan.Primary,
  425. Levels: plan.Levels,
  426. LevelSet: plan.LevelSet,
  427. DisplayLevel: plan.Presentation,
  428. Context: plan.Context,
  429. Spectra: art.surveillanceSpectra,
  430. Candidates: candidates,
  431. Scheduled: scheduled,
  432. Finished: art.finished,
  433. Signals: art.detected,
  434. NoiseFloor: art.noiseFloor,
  435. Thresholds: art.thresholds,
  436. }
  437. }
  438. func (rt *dspRuntime) detectDerivedCandidates(art *spectrumArtifacts, plan surveillancePlan) []pipeline.Candidate {
  439. if art == nil || len(plan.LevelSet.Derived) == 0 {
  440. return nil
  441. }
  442. spectra := map[string][]float64{}
  443. for _, spec := range art.surveillanceSpectra {
  444. if spec.Level.Name == "" || len(spec.Spectrum) == 0 {
  445. continue
  446. }
  447. spectra[spec.Level.Name] = spec.Spectrum
  448. }
  449. if len(spectra) == 0 {
  450. return nil
  451. }
  452. out := make([]pipeline.Candidate, 0, len(plan.LevelSet.Derived))
  453. for _, level := range plan.LevelSet.Derived {
  454. if level.Name == "" {
  455. continue
  456. }
  457. spectrum := spectra[level.Name]
  458. if len(spectrum) == 0 {
  459. continue
  460. }
  461. entry := rt.derivedDetectorForLevel(level)
  462. if entry == nil || entry.det == nil {
  463. continue
  464. }
  465. _, signals := entry.det.Process(art.now, spectrum, level.CenterHz)
  466. if len(signals) == 0 {
  467. continue
  468. }
  469. cands := pipeline.CandidatesFromSignalsWithLevel(signals, "surveillance-derived", level)
  470. for i := range cands {
  471. if cands[i].ID == 0 {
  472. continue
  473. }
  474. cands[i].ID = entry.idBase - cands[i].ID
  475. }
  476. out = append(out, cands...)
  477. }
  478. if len(out) == 0 {
  479. return nil
  480. }
  481. return out
  482. }
  483. func (rt *dspRuntime) derivedDetectorForLevel(level pipeline.AnalysisLevel) *derivedDetector {
  484. if level.SampleRate <= 0 || level.FFTSize <= 0 {
  485. return nil
  486. }
  487. if rt.derivedDetectors == nil {
  488. rt.derivedDetectors = map[string]*derivedDetector{}
  489. }
  490. key := level.Name
  491. if key == "" {
  492. key = fmt.Sprintf("%d:%d", level.SampleRate, level.FFTSize)
  493. }
  494. entry := rt.derivedDetectors[key]
  495. if entry != nil && entry.sampleRate == level.SampleRate && entry.fftSize == level.FFTSize {
  496. return entry
  497. }
  498. if rt.nextDerivedBase == 0 {
  499. rt.nextDerivedBase = -derivedIDBlock
  500. }
  501. entry = &derivedDetector{
  502. det: detector.New(rt.cfg.Detector, level.SampleRate, level.FFTSize),
  503. sampleRate: level.SampleRate,
  504. fftSize: level.FFTSize,
  505. idBase: rt.nextDerivedBase,
  506. }
  507. rt.nextDerivedBase -= derivedIDBlock
  508. rt.derivedDetectors[key] = entry
  509. return entry
  510. }
  511. func (rt *dspRuntime) buildRefinementInput(surv pipeline.SurveillanceResult, now time.Time) pipeline.RefinementInput {
  512. policy := pipeline.PolicyFromConfig(rt.cfg)
  513. plan := pipeline.BuildRefinementPlan(surv.Candidates, policy)
  514. admission := rt.arbiter.AdmitRefinement(plan, policy, now)
  515. plan = admission.Plan
  516. workItems := make([]pipeline.RefinementWorkItem, 0, len(admission.WorkItems))
  517. if len(admission.WorkItems) > 0 {
  518. workItems = append(workItems, admission.WorkItems...)
  519. }
  520. scheduled := append([]pipeline.ScheduledCandidate(nil), admission.Admitted...)
  521. workIndex := map[int64]int{}
  522. for i := range workItems {
  523. if workItems[i].Candidate.ID == 0 {
  524. continue
  525. }
  526. workIndex[workItems[i].Candidate.ID] = i
  527. }
  528. windows := make([]pipeline.RefinementWindow, 0, len(scheduled))
  529. for _, sc := range scheduled {
  530. window := pipeline.RefinementWindowForCandidate(policy, sc.Candidate)
  531. windows = append(windows, window)
  532. if idx, ok := workIndex[sc.Candidate.ID]; ok {
  533. workItems[idx].Window = window
  534. }
  535. }
  536. detailFFT := rt.cfg.Refinement.DetailFFTSize
  537. if detailFFT <= 0 {
  538. detailFFT = rt.cfg.FFTSize
  539. }
  540. levelSpan := spanForPolicy(policy, float64(rt.cfg.SampleRate))
  541. if _, maxSpan, ok := windowSpanBounds(windows); ok {
  542. levelSpan = maxSpan
  543. }
  544. level := analysisLevel("refinement", "refinement", "refinement", rt.cfg.SampleRate, detailFFT, rt.cfg.CenterHz, levelSpan, "refinement-window", 1, rt.cfg.SampleRate)
  545. detailLevel := analysisLevel("detail", "detail", "refinement", rt.cfg.SampleRate, detailFFT, rt.cfg.CenterHz, levelSpan, "detail-spectrum", 1, rt.cfg.SampleRate)
  546. if len(workItems) > 0 {
  547. for i := range workItems {
  548. item := &workItems[i]
  549. if item.Window.SpanHz <= 0 {
  550. continue
  551. }
  552. item.Execution = &pipeline.RefinementExecution{
  553. Stage: "refine",
  554. SampleRate: rt.cfg.SampleRate,
  555. FFTSize: detailFFT,
  556. CenterHz: item.Window.CenterHz,
  557. SpanHz: item.Window.SpanHz,
  558. Source: detailLevel.Source,
  559. }
  560. }
  561. }
  562. input := pipeline.RefinementInput{
  563. Level: level,
  564. Detail: detailLevel,
  565. Context: surv.Context,
  566. Request: pipeline.RefinementRequest{Strategy: plan.Strategy, Reason: "surveillance-plan", SpanHintHz: levelSpan},
  567. Budgets: pipeline.BudgetModelFromPolicy(policy),
  568. Admission: admission.Admission,
  569. Candidates: append([]pipeline.Candidate(nil), surv.Candidates...),
  570. Scheduled: scheduled,
  571. WorkItems: workItems,
  572. Plan: plan,
  573. Windows: windows,
  574. SampleRate: rt.cfg.SampleRate,
  575. FFTSize: detailFFT,
  576. CenterHz: rt.cfg.CenterHz,
  577. Source: "surveillance-detector",
  578. }
  579. input.Context.Refinement = level
  580. input.Context.Detail = detailLevel
  581. if !policy.RefinementEnabled {
  582. for i := range input.WorkItems {
  583. item := &input.WorkItems[i]
  584. if item.Status == pipeline.RefinementStatusDropped {
  585. continue
  586. }
  587. item.Status = pipeline.RefinementStatusDropped
  588. item.Reason = pipeline.RefinementReasonDisabled
  589. }
  590. input.Scheduled = nil
  591. input.Request.Reason = pipeline.ReasonAdmissionDisabled
  592. input.Admission.Reason = pipeline.ReasonAdmissionDisabled
  593. input.Admission.Admitted = 0
  594. input.Admission.Skipped = 0
  595. input.Admission.Displaced = 0
  596. input.Plan.Selected = nil
  597. input.Plan.DroppedByBudget = 0
  598. }
  599. rt.setArbitration(policy, input.Budgets, input.Admission, rt.arbitration.Queue)
  600. return input
  601. }
  602. func (rt *dspRuntime) runRefinement(art *spectrumArtifacts, surv pipeline.SurveillanceResult, extractMgr *extractionManager, rec *recorder.Manager) pipeline.RefinementStep {
  603. input := rt.buildRefinementInput(surv, art.now)
  604. markWorkItemsStatus(input.WorkItems, pipeline.RefinementStatusAdmitted, pipeline.RefinementStatusRunning, pipeline.RefinementReasonRunning)
  605. result := rt.refineSignals(art, input, extractMgr, rec)
  606. markWorkItemsCompleted(input.WorkItems, result.Candidates)
  607. return pipeline.RefinementStep{Input: input, Result: result}
  608. }
  609. func (rt *dspRuntime) refineSignals(art *spectrumArtifacts, input pipeline.RefinementInput, extractMgr *extractionManager, rec *recorder.Manager) pipeline.RefinementResult {
  610. if art == nil || len(art.detailIQ) == 0 || len(input.Scheduled) == 0 {
  611. return pipeline.RefinementResult{}
  612. }
  613. policy := pipeline.PolicyFromConfig(rt.cfg)
  614. selectedCandidates := make([]pipeline.Candidate, 0, len(input.Scheduled))
  615. selectedSignals := make([]detector.Signal, 0, len(input.Scheduled))
  616. for _, sc := range input.Scheduled {
  617. selectedCandidates = append(selectedCandidates, sc.Candidate)
  618. selectedSignals = append(selectedSignals, detector.Signal{
  619. ID: sc.Candidate.ID,
  620. FirstBin: sc.Candidate.FirstBin,
  621. LastBin: sc.Candidate.LastBin,
  622. CenterHz: sc.Candidate.CenterHz,
  623. BWHz: sc.Candidate.BandwidthHz,
  624. PeakDb: sc.Candidate.PeakDb,
  625. SNRDb: sc.Candidate.SNRDb,
  626. NoiseDb: sc.Candidate.NoiseDb,
  627. })
  628. }
  629. sampleRate := input.SampleRate
  630. fftSize := input.FFTSize
  631. centerHz := input.CenterHz
  632. if sampleRate <= 0 {
  633. sampleRate = rt.cfg.SampleRate
  634. }
  635. if fftSize <= 0 {
  636. fftSize = rt.cfg.FFTSize
  637. }
  638. if centerHz == 0 {
  639. centerHz = rt.cfg.CenterHz
  640. }
  641. snips, snipRates := extractSignalIQBatch(extractMgr, art.detailIQ, sampleRate, centerHz, selectedSignals)
  642. refined := pipeline.RefineCandidates(selectedCandidates, input.Windows, art.detailSpectrum, sampleRate, fftSize, snips, snipRates, classifier.ClassifierMode(rt.cfg.ClassifierMode))
  643. signals := make([]detector.Signal, 0, len(refined))
  644. decisions := make([]pipeline.SignalDecision, 0, len(refined))
  645. for i, ref := range refined {
  646. sig := ref.Signal
  647. signals = append(signals, sig)
  648. cls := sig.Class
  649. snipRate := ref.SnippetRate
  650. decision := pipeline.DecideSignalAction(policy, ref.Candidate, cls)
  651. decisions = append(decisions, decision)
  652. if cls != nil {
  653. pll := classifier.PLLResult{}
  654. if i < len(snips) && snips[i] != nil && len(snips[i]) > 256 {
  655. pll = classifier.EstimateExactFrequency(snips[i], snipRate, signals[i].CenterHz, cls.ModType)
  656. cls.PLL = &pll
  657. signals[i].PLL = &pll
  658. if cls.ModType == classifier.ClassWFM && pll.Stereo {
  659. cls.ModType = classifier.ClassWFMStereo
  660. }
  661. }
  662. if (cls.ModType == classifier.ClassWFM || cls.ModType == classifier.ClassWFMStereo) && rec != nil {
  663. rt.updateRDS(art.now, rec, &signals[i], cls)
  664. }
  665. }
  666. }
  667. budget := pipeline.BudgetModelFromPolicy(policy)
  668. queueStats := rt.arbiter.ApplyDecisions(decisions, budget, art.now, policy)
  669. rt.setArbitration(policy, budget, input.Admission, queueStats)
  670. summary := summarizeDecisions(decisions)
  671. if rec != nil {
  672. if summary.RecordEnabled > 0 {
  673. rt.cfg.Recorder.Enabled = true
  674. }
  675. if summary.DecodeEnabled > 0 {
  676. rt.cfg.Recorder.AutoDecode = true
  677. }
  678. }
  679. rt.det.UpdateClasses(signals)
  680. return pipeline.RefinementResult{Level: input.Level, Signals: signals, Decisions: decisions, Candidates: selectedCandidates}
  681. }
  682. func (rt *dspRuntime) updateRDS(now time.Time, rec *recorder.Manager, sig *detector.Signal, cls *classifier.Classification) {
  683. if sig == nil || cls == nil {
  684. return
  685. }
  686. keyHz := sig.CenterHz
  687. if sig.PLL != nil && sig.PLL.ExactHz != 0 {
  688. keyHz = sig.PLL.ExactHz
  689. }
  690. key := int64(math.Round(keyHz / 25000.0))
  691. st := rt.rdsMap[key]
  692. if st == nil {
  693. st = &rdsState{}
  694. rt.rdsMap[key] = st
  695. }
  696. if now.Sub(st.lastDecode) >= 4*time.Second && atomic.LoadInt32(&st.busy) == 0 {
  697. st.lastDecode = now
  698. atomic.StoreInt32(&st.busy, 1)
  699. go func(st *rdsState, sigHz float64) {
  700. defer atomic.StoreInt32(&st.busy, 0)
  701. ringIQ, ringSR, ringCenter := rec.SliceRecent(4.0)
  702. if len(ringIQ) < ringSR || ringSR <= 0 {
  703. return
  704. }
  705. offset := sigHz - ringCenter
  706. shifted := dsp.FreqShift(ringIQ, ringSR, offset)
  707. decim1 := ringSR / 1000000
  708. if decim1 < 1 {
  709. decim1 = 1
  710. }
  711. lp1 := dsp.LowpassFIR(float64(ringSR/decim1)/2.0*0.8, ringSR, 51)
  712. f1 := dsp.ApplyFIR(shifted, lp1)
  713. d1 := dsp.Decimate(f1, decim1)
  714. rate1 := ringSR / decim1
  715. decim2 := rate1 / 250000
  716. if decim2 < 1 {
  717. decim2 = 1
  718. }
  719. lp2 := dsp.LowpassFIR(float64(rate1/decim2)/2.0*0.8, rate1, 101)
  720. f2 := dsp.ApplyFIR(d1, lp2)
  721. decimated := dsp.Decimate(f2, decim2)
  722. actualRate := rate1 / decim2
  723. rdsBase := demod.RDSBasebandComplex(decimated, actualRate)
  724. if len(rdsBase.Samples) == 0 {
  725. return
  726. }
  727. st.mu.Lock()
  728. result := st.dec.Decode(rdsBase.Samples, rdsBase.SampleRate)
  729. if result.PS != "" {
  730. st.result = result
  731. }
  732. st.mu.Unlock()
  733. }(st, sig.CenterHz)
  734. }
  735. st.mu.Lock()
  736. ps := st.result.PS
  737. st.mu.Unlock()
  738. if ps != "" && sig.PLL != nil {
  739. sig.PLL.RDSStation = strings.TrimSpace(ps)
  740. cls.PLL = sig.PLL
  741. }
  742. }
  743. func (rt *dspRuntime) maintenance(displaySignals []detector.Signal, rec *recorder.Manager) {
  744. if len(rt.rdsMap) > 0 {
  745. activeIDs := make(map[int64]bool, len(displaySignals))
  746. for _, s := range displaySignals {
  747. keyHz := s.CenterHz
  748. if s.PLL != nil && s.PLL.ExactHz != 0 {
  749. keyHz = s.PLL.ExactHz
  750. }
  751. activeIDs[int64(math.Round(keyHz/25000.0))] = true
  752. }
  753. for id := range rt.rdsMap {
  754. if !activeIDs[id] {
  755. delete(rt.rdsMap, id)
  756. }
  757. }
  758. }
  759. if len(rt.streamPhaseState) > 0 {
  760. sigIDs := make(map[int64]bool, len(displaySignals))
  761. for _, s := range displaySignals {
  762. sigIDs[s.ID] = true
  763. }
  764. for id := range rt.streamPhaseState {
  765. if !sigIDs[id] {
  766. delete(rt.streamPhaseState, id)
  767. }
  768. }
  769. }
  770. if rec != nil && len(displaySignals) > 0 {
  771. aqCfg := extractionConfig{firTaps: rt.cfg.Recorder.ExtractionTaps, bwMult: rt.cfg.Recorder.ExtractionBwMult}
  772. _ = aqCfg
  773. }
  774. }
  775. func spanForPolicy(policy pipeline.Policy, fallback float64) float64 {
  776. if policy.MonitorSpanHz > 0 {
  777. return policy.MonitorSpanHz
  778. }
  779. if policy.MonitorStartHz != 0 && policy.MonitorEndHz != 0 && policy.MonitorEndHz > policy.MonitorStartHz {
  780. return policy.MonitorEndHz - policy.MonitorStartHz
  781. }
  782. return fallback
  783. }
  784. func windowSpanBounds(windows []pipeline.RefinementWindow) (float64, float64, bool) {
  785. minSpan := 0.0
  786. maxSpan := 0.0
  787. ok := false
  788. for _, w := range windows {
  789. if w.SpanHz <= 0 {
  790. continue
  791. }
  792. if !ok || w.SpanHz < minSpan {
  793. minSpan = w.SpanHz
  794. }
  795. if !ok || w.SpanHz > maxSpan {
  796. maxSpan = w.SpanHz
  797. }
  798. ok = true
  799. }
  800. return minSpan, maxSpan, ok
  801. }
  802. func analysisLevel(name, role, truth string, sampleRate int, fftSize int, centerHz float64, spanHz float64, source string, decimation int, baseRate int) pipeline.AnalysisLevel {
  803. level := pipeline.AnalysisLevel{
  804. Name: name,
  805. Role: role,
  806. Truth: truth,
  807. SampleRate: sampleRate,
  808. FFTSize: fftSize,
  809. CenterHz: centerHz,
  810. SpanHz: spanHz,
  811. Source: source,
  812. }
  813. if level.SampleRate > 0 && level.FFTSize > 0 {
  814. level.BinHz = float64(level.SampleRate) / float64(level.FFTSize)
  815. }
  816. if decimation > 0 {
  817. level.Decimation = decimation
  818. } else if baseRate > 0 && level.SampleRate > 0 && baseRate%level.SampleRate == 0 {
  819. level.Decimation = baseRate / level.SampleRate
  820. }
  821. return level
  822. }
  823. func (rt *dspRuntime) buildSurveillancePlan(policy pipeline.Policy) surveillancePlan {
  824. baseRate := rt.cfg.SampleRate
  825. baseFFT := rt.cfg.Surveillance.AnalysisFFTSize
  826. if baseFFT <= 0 {
  827. baseFFT = rt.cfg.FFTSize
  828. }
  829. span := spanForPolicy(policy, float64(baseRate))
  830. primary := analysisLevel("surveillance", "surveillance", "surveillance", baseRate, baseFFT, rt.cfg.CenterHz, span, "baseband", 1, baseRate)
  831. levels := []pipeline.AnalysisLevel{primary}
  832. specs := []surveillanceLevelSpec{{Level: primary, Decim: 1, AllowGPU: true}}
  833. context := pipeline.AnalysisContext{Surveillance: primary}
  834. derivedLevels := make([]pipeline.AnalysisLevel, 0, 2)
  835. strategy := strings.ToLower(strings.TrimSpace(policy.SurveillanceStrategy))
  836. switch strategy {
  837. case "multi-res", "multi-resolution", "multi", "multi_res":
  838. decim := 2
  839. derivedRate := baseRate / decim
  840. derivedFFT := baseFFT / decim
  841. if derivedRate >= 200000 && derivedFFT >= 256 {
  842. derivedSpan := spanForPolicy(policy, float64(derivedRate))
  843. derived := analysisLevel("surveillance-lowres", "surveillance-lowres", "surveillance", derivedRate, derivedFFT, rt.cfg.CenterHz, derivedSpan, "decimated", decim, baseRate)
  844. levels = append(levels, derived)
  845. specs = append(specs, surveillanceLevelSpec{Level: derived, Decim: decim, AllowGPU: false})
  846. context.Derived = append(context.Derived, derived)
  847. derivedLevels = append(derivedLevels, derived)
  848. }
  849. }
  850. presentation := analysisLevel("presentation", "presentation", "presentation", baseRate, rt.cfg.Surveillance.DisplayBins, rt.cfg.CenterHz, span, "display", 1, baseRate)
  851. context.Presentation = presentation
  852. levelSet := pipeline.SurveillanceLevelSet{
  853. Primary: primary,
  854. Derived: append([]pipeline.AnalysisLevel(nil), derivedLevels...),
  855. Presentation: presentation,
  856. }
  857. allLevels := make([]pipeline.AnalysisLevel, 0, 1+len(derivedLevels)+1)
  858. allLevels = append(allLevels, primary)
  859. allLevels = append(allLevels, derivedLevels...)
  860. if presentation.Name != "" {
  861. allLevels = append(allLevels, presentation)
  862. }
  863. levelSet.All = allLevels
  864. return surveillancePlan{
  865. Primary: primary,
  866. Levels: levels,
  867. LevelSet: levelSet,
  868. Presentation: presentation,
  869. Context: context,
  870. Specs: specs,
  871. }
  872. }
  873. func sameIQBuffer(a []complex64, b []complex64) bool {
  874. if len(a) != len(b) {
  875. return false
  876. }
  877. if len(a) == 0 {
  878. return true
  879. }
  880. return &a[0] == &b[0]
  881. }
  882. func markWorkItemsStatus(items []pipeline.RefinementWorkItem, from string, to string, reason string) {
  883. for i := range items {
  884. if items[i].Status != from {
  885. continue
  886. }
  887. items[i].Status = to
  888. if reason != "" {
  889. items[i].Reason = reason
  890. }
  891. }
  892. }
  893. func markWorkItemsCompleted(items []pipeline.RefinementWorkItem, candidates []pipeline.Candidate) {
  894. if len(items) == 0 || len(candidates) == 0 {
  895. return
  896. }
  897. done := map[int64]struct{}{}
  898. for _, cand := range candidates {
  899. if cand.ID != 0 {
  900. done[cand.ID] = struct{}{}
  901. }
  902. }
  903. for i := range items {
  904. if _, ok := done[items[i].Candidate.ID]; !ok {
  905. continue
  906. }
  907. items[i].Status = pipeline.RefinementStatusCompleted
  908. items[i].Reason = pipeline.RefinementReasonCompleted
  909. }
  910. }
  911. func (rt *dspRuntime) setArbitration(policy pipeline.Policy, budget pipeline.BudgetModel, admission pipeline.RefinementAdmission, queue pipeline.DecisionQueueStats) {
  912. rt.arbitration = pipeline.BuildArbitrationState(policy, budget, admission, queue)
  913. }