Wideband autonomous SDR analysis engine forked from sdr-visual-suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
7.7KB

  1. package pipeline
  2. import (
  3. "math"
  4. "sdr-wideband-suite/internal/config"
  5. )
  6. const maxMonitorWindowBias = 0.2
  7. func NormalizeMonitorWindows(goals config.PipelineGoalConfig, centerHz float64) []MonitorWindow {
  8. if len(goals.MonitorWindows) > 0 {
  9. windows := make([]MonitorWindow, 0, len(goals.MonitorWindows))
  10. for _, raw := range goals.MonitorWindows {
  11. if win, ok := normalizeGoalWindow(raw, centerHz); ok {
  12. windows = append(windows, win)
  13. }
  14. }
  15. if len(windows) > 0 {
  16. return finalizeMonitorWindows(windows)
  17. }
  18. }
  19. if goals.MonitorStartHz > 0 && goals.MonitorEndHz > goals.MonitorStartHz {
  20. start := goals.MonitorStartHz
  21. end := goals.MonitorEndHz
  22. span := end - start
  23. return finalizeMonitorWindows([]MonitorWindow{{
  24. Label: "primary",
  25. StartHz: start,
  26. EndHz: end,
  27. CenterHz: (start + end) / 2,
  28. SpanHz: span,
  29. Source: "goals:bounds",
  30. }})
  31. }
  32. if goals.MonitorSpanHz > 0 && centerHz != 0 {
  33. half := goals.MonitorSpanHz / 2
  34. start := centerHz - half
  35. end := centerHz + half
  36. return finalizeMonitorWindows([]MonitorWindow{{
  37. Label: "primary",
  38. StartHz: start,
  39. EndHz: end,
  40. CenterHz: centerHz,
  41. SpanHz: goals.MonitorSpanHz,
  42. Source: "goals:span",
  43. }})
  44. }
  45. return nil
  46. }
  47. func finalizeMonitorWindows(windows []MonitorWindow) []MonitorWindow {
  48. if len(windows) == 0 {
  49. return nil
  50. }
  51. maxSpan := 0.0
  52. for _, w := range windows {
  53. if w.SpanHz > maxSpan {
  54. maxSpan = w.SpanHz
  55. }
  56. }
  57. for i := range windows {
  58. windows[i].Index = i
  59. priority := normalizeMonitorPriority(windows[i].Priority)
  60. windows[i].Priority = priority
  61. spanBias := 0.0
  62. if maxSpan > 0 && len(windows) > 1 && windows[i].SpanHz > 0 {
  63. spanBias = maxMonitorWindowBias * (1 - (windows[i].SpanHz / maxSpan))
  64. if spanBias < 0 {
  65. spanBias = 0
  66. }
  67. }
  68. policyBias := priority * maxMonitorWindowBias
  69. totalBias := spanBias + policyBias
  70. if totalBias > maxMonitorWindowBias {
  71. totalBias = maxMonitorWindowBias
  72. } else if totalBias < -maxMonitorWindowBias {
  73. totalBias = -maxMonitorWindowBias
  74. }
  75. windows[i].PriorityBias = totalBias
  76. }
  77. return windows
  78. }
  79. func MonitorWindowBounds(windows []MonitorWindow) (float64, float64, bool) {
  80. minStart := 0.0
  81. maxEnd := 0.0
  82. ok := false
  83. for _, w := range windows {
  84. if w.StartHz <= 0 || w.EndHz <= 0 || w.EndHz <= w.StartHz {
  85. continue
  86. }
  87. if !ok || w.StartHz < minStart {
  88. minStart = w.StartHz
  89. }
  90. if !ok || w.EndHz > maxEnd {
  91. maxEnd = w.EndHz
  92. }
  93. ok = true
  94. }
  95. return minStart, maxEnd, ok
  96. }
  97. func normalizeGoalWindow(raw config.MonitorWindow, fallbackCenter float64) (MonitorWindow, bool) {
  98. if raw.StartHz > 0 && raw.EndHz > raw.StartHz {
  99. span := raw.EndHz - raw.StartHz
  100. return MonitorWindow{
  101. Label: raw.Label,
  102. StartHz: raw.StartHz,
  103. EndHz: raw.EndHz,
  104. CenterHz: (raw.StartHz + raw.EndHz) / 2,
  105. SpanHz: span,
  106. Source: "goals:window:start_end",
  107. Priority: raw.Priority,
  108. AutoRecord: raw.AutoRecord,
  109. AutoDecode: raw.AutoDecode,
  110. }, true
  111. }
  112. center := raw.CenterHz
  113. if center == 0 {
  114. center = fallbackCenter
  115. }
  116. if center != 0 && raw.SpanHz > 0 {
  117. half := raw.SpanHz / 2
  118. source := "goals:window:center_span"
  119. if raw.CenterHz == 0 {
  120. source = "goals:window:span_default"
  121. }
  122. return MonitorWindow{
  123. Label: raw.Label,
  124. StartHz: center - half,
  125. EndHz: center + half,
  126. CenterHz: center,
  127. SpanHz: raw.SpanHz,
  128. Source: source,
  129. Priority: raw.Priority,
  130. AutoRecord: raw.AutoRecord,
  131. AutoDecode: raw.AutoDecode,
  132. }, true
  133. }
  134. return MonitorWindow{}, false
  135. }
  136. func normalizeMonitorPriority(priority float64) float64 {
  137. if math.IsNaN(priority) || math.IsInf(priority, 0) {
  138. return 0
  139. }
  140. if priority > 1 {
  141. return 1
  142. }
  143. if priority < -1 {
  144. return -1
  145. }
  146. return priority
  147. }
  148. func monitorBounds(policy Policy) (float64, float64, bool) {
  149. if len(policy.MonitorWindows) > 0 {
  150. return MonitorWindowBounds(policy.MonitorWindows)
  151. }
  152. start := policy.MonitorStartHz
  153. end := policy.MonitorEndHz
  154. if start != 0 && end != 0 && end > start {
  155. return start, end, true
  156. }
  157. if policy.MonitorSpanHz > 0 && policy.MonitorCenterHz != 0 {
  158. half := policy.MonitorSpanHz / 2
  159. return policy.MonitorCenterHz - half, policy.MonitorCenterHz + half, true
  160. }
  161. return 0, 0, false
  162. }
  163. func candidateInMonitor(policy Policy, candidate Candidate) bool {
  164. if len(policy.MonitorWindows) > 0 {
  165. matches := MonitorWindowMatchesForCandidate(policy.MonitorWindows, candidate)
  166. return len(matches) > 0
  167. }
  168. start, end, ok := monitorBounds(policy)
  169. if !ok {
  170. return true
  171. }
  172. left, right := candidateBounds(candidate)
  173. return right >= start && left <= end
  174. }
  175. func candidateBounds(candidate Candidate) (float64, float64) {
  176. left := candidate.CenterHz
  177. right := candidate.CenterHz
  178. if candidate.BandwidthHz > 0 {
  179. left = candidate.CenterHz - candidate.BandwidthHz/2
  180. right = candidate.CenterHz + candidate.BandwidthHz/2
  181. }
  182. return left, right
  183. }
  184. func ApplyMonitorWindowMatches(policy Policy, candidate *Candidate) bool {
  185. if candidate == nil {
  186. return true
  187. }
  188. if len(policy.MonitorWindows) == 0 {
  189. candidate.MonitorMatches = nil
  190. if start, end, ok := monitorBounds(policy); ok {
  191. left, right := candidateBounds(*candidate)
  192. if right < start || left > end {
  193. return false
  194. }
  195. }
  196. return true
  197. }
  198. matches := MonitorWindowMatchesForCandidate(policy.MonitorWindows, *candidate)
  199. if len(matches) == 0 {
  200. candidate.MonitorMatches = nil
  201. return false
  202. }
  203. candidate.MonitorMatches = matches
  204. return true
  205. }
  206. func ApplyMonitorWindowMatchesToCandidates(policy Policy, candidates []Candidate) {
  207. if len(candidates) == 0 || len(policy.MonitorWindows) == 0 {
  208. return
  209. }
  210. for i := range candidates {
  211. _ = ApplyMonitorWindowMatches(policy, &candidates[i])
  212. }
  213. }
  214. func MonitorWindowMatches(policy Policy, candidate Candidate) []MonitorWindowMatch {
  215. return MonitorWindowMatchesForCandidate(policy.MonitorWindows, candidate)
  216. }
  217. func MonitorWindowMatchesForCandidate(windows []MonitorWindow, candidate Candidate) []MonitorWindowMatch {
  218. if len(windows) == 0 {
  219. return nil
  220. }
  221. left, right := candidateBounds(candidate)
  222. pointCandidate := candidate.BandwidthHz <= 0
  223. matches := make([]MonitorWindowMatch, 0, len(windows))
  224. for _, win := range windows {
  225. if win.StartHz <= 0 || win.EndHz <= 0 || win.EndHz <= win.StartHz {
  226. continue
  227. }
  228. if right < win.StartHz || left > win.EndHz {
  229. continue
  230. }
  231. overlap := math.Min(right, win.EndHz) - math.Max(left, win.StartHz)
  232. coverage := 0.0
  233. if win.SpanHz > 0 && overlap > 0 {
  234. coverage = overlap / win.SpanHz
  235. }
  236. if pointCandidate && candidate.CenterHz >= win.StartHz && candidate.CenterHz <= win.EndHz {
  237. coverage = 1
  238. }
  239. if coverage < 0 {
  240. coverage = 0
  241. }
  242. if coverage > 1 {
  243. coverage = 1
  244. }
  245. center := win.CenterHz
  246. if center == 0 {
  247. center = (win.StartHz + win.EndHz) / 2
  248. }
  249. distance := math.Abs(candidate.CenterHz - center)
  250. bias := win.PriorityBias * coverage
  251. matches = append(matches, MonitorWindowMatch{
  252. Index: win.Index,
  253. Label: win.Label,
  254. Source: win.Source,
  255. StartHz: win.StartHz,
  256. EndHz: win.EndHz,
  257. CenterHz: center,
  258. SpanHz: win.SpanHz,
  259. OverlapHz: overlap,
  260. Coverage: coverage,
  261. DistanceHz: distance,
  262. Bias: bias,
  263. AutoRecord: win.AutoRecord,
  264. AutoDecode: win.AutoDecode,
  265. })
  266. }
  267. if len(matches) == 0 {
  268. return nil
  269. }
  270. return matches
  271. }
  272. func MonitorWindowBias(policy Policy, candidate Candidate) (float64, *MonitorWindowMatch) {
  273. matches := candidate.MonitorMatches
  274. if len(matches) == 0 {
  275. matches = MonitorWindowMatches(policy, candidate)
  276. }
  277. if len(matches) == 0 {
  278. return 0, nil
  279. }
  280. bestIdx := 0
  281. for i := 1; i < len(matches); i++ {
  282. if matches[i].Bias > matches[bestIdx].Bias {
  283. bestIdx = i
  284. continue
  285. }
  286. if matches[i].Bias == matches[bestIdx].Bias && matches[i].Coverage > matches[bestIdx].Coverage {
  287. bestIdx = i
  288. }
  289. }
  290. best := matches[bestIdx]
  291. return best.Bias, &best
  292. }