fixed issue on udpstreamer trigger logic
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package wshub
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A short window at a high sample rate fits in a ring's initial capacity, so the
|
||||
// retune sweep used to leave it there — and a ring holding exactly the window has
|
||||
// already rolled past the front of a capture by the time that capture is read,
|
||||
// which happens a post-window plus captureMarginSec after the trigger fires.
|
||||
//
|
||||
// 1 MSps over a 200 ms window: 200 k points fit in the 250 k initial ring, and
|
||||
// every shot came back missing its first 123 ms.
|
||||
func TestCaptureWholeAtHighRateShortWindow(t *testing.T) {
|
||||
const (
|
||||
key = "s1:Ch1"
|
||||
rate = 1e6
|
||||
window = 0.2
|
||||
prePct = 20.0
|
||||
batchSec = 1.0 / 30.0
|
||||
simSec = 6.0
|
||||
)
|
||||
|
||||
h := NewHub()
|
||||
h.SetRingBudget(defaultRingPts)
|
||||
h.rings[key] = newSigRing(ringCapInitial)
|
||||
h.trigger.SetConfig(trigConfig{signalKey: key, edge: "rising", threshold: 0,
|
||||
windowSec: window, prePercent: prePct, mode: "normal", holdoffSec: 0.2})
|
||||
|
||||
rateHz := float64(rate)
|
||||
nBatch := int(rateHz * batchSec)
|
||||
ts := make([]float64, nBatch)
|
||||
vs := make([]float64, nBatch)
|
||||
|
||||
armed, shots := false, 0
|
||||
for now := 0.0; now < simSec; now += batchSec {
|
||||
for i := range ts {
|
||||
ts[i] = now + float64(i)/rateHz
|
||||
vs[i] = math.Sin(2 * math.Pi * 5 * ts[i]) // a rising crossing every 200 ms
|
||||
}
|
||||
h.ingest(key, 1, ts, vs)
|
||||
h.retuneRings(now)
|
||||
h.refreshTriggerFill()
|
||||
|
||||
if !armed && now > 2 {
|
||||
h.trigger.Arm()
|
||||
armed = true
|
||||
}
|
||||
trigTime, pre, post, ok := h.trigger.dueCapture(now + batchSec)
|
||||
if !ok {
|
||||
if h.trigger.dueRearm(now + batchSec) {
|
||||
h.trigger.Arm()
|
||||
}
|
||||
continue
|
||||
}
|
||||
t0 := trigTime - pre
|
||||
buf := h.buildTriggerCapture(trigTime, pre, post)
|
||||
if buf == nil {
|
||||
t.Fatalf("shot at t=%.4f produced no frame at all", trigTime)
|
||||
}
|
||||
first, last, n := decodeCaptureSpan(t, buf, key)
|
||||
shots++
|
||||
if lost := first - t0; lost > shortCaptureTol*window {
|
||||
_, span := h.rings[key].stats()
|
||||
t.Errorf("shot at t=%.4f is missing %.0f ms at the front of its %.0f ms window "+
|
||||
"(got [%.4f,%.4f], %d pts; ring holds %.4f s in %d points)",
|
||||
trigTime, 1e3*lost, 1e3*window, first, last, n, span, h.rings[key].capacity())
|
||||
}
|
||||
h.trigger.markTriggered(now + batchSec)
|
||||
}
|
||||
if shots < 3 {
|
||||
t.Fatalf("only %d shots in %.0f s", shots, simSec)
|
||||
}
|
||||
}
|
||||
@@ -842,10 +842,7 @@ func (hf *histFile) readAfter(after, t0, t1 float64, max int) ([]byte, float64,
|
||||
// The run wraps at most once, so it costs at most two reads.
|
||||
buf := make([]byte, n*histPairSize)
|
||||
start := (oldest + lo) % capacity
|
||||
head := int(capacity-start) * histPairSize
|
||||
if head > len(buf) {
|
||||
head = len(buf)
|
||||
}
|
||||
head := min(int(capacity-start)*histPairSize, len(buf))
|
||||
if _, err := hf.f.ReadAt(buf[:head], int64(histHeaderSize)+int64(start)*histPairSize); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -898,10 +895,8 @@ func (hf *histFile) writePairs(t, v []float64) error {
|
||||
binary.LittleEndian.PutUint64(buf[i*histPairSize+8:], math.Float64bits(v[i]))
|
||||
}
|
||||
|
||||
first := int(hf.capacity - hf.head)
|
||||
if first > n {
|
||||
first = n
|
||||
}
|
||||
first := min(int(hf.capacity-hf.head), n)
|
||||
|
||||
off := int64(histHeaderSize) + int64(hf.head)*histPairSize
|
||||
if _, err := hf.f.WriteAt(buf[:first*histPairSize], off); err != nil {
|
||||
return err
|
||||
@@ -1089,10 +1084,7 @@ func (hw *historyWriter) readRange(key string, t0, t1 float64, maxOut int) ([]fl
|
||||
// Read in contiguous runs: the range wraps at most once.
|
||||
buf := make([]byte, n*histPairSize)
|
||||
start := (oldest + lo) % capacity
|
||||
first := int(capacity - start)
|
||||
if first > n {
|
||||
first = n
|
||||
}
|
||||
first := min(int(capacity-start), n)
|
||||
if _, err := hf.f.ReadAt(buf[:first*histPairSize],
|
||||
int64(histHeaderSize)+int64(start)*histPairSize); err != nil {
|
||||
return nil, nil
|
||||
@@ -1265,7 +1257,7 @@ func (h *Hub) handleSetHistoryBudget(env map[string]interface{}) {
|
||||
// handleHistoryZoom answers a historyZoom request from disk. Same request and
|
||||
// reply shape as "zoom", so clients can fall back to it transparently when a
|
||||
// window reaches further back than the in-memory rings hold.
|
||||
func (h *Hub) handleHistoryZoom(c *wsClient, env map[string]interface{}) {
|
||||
func (h *Hub) handleHistoryZoom(c *wsClient, env map[string]any) {
|
||||
if !h.hist.enabled() {
|
||||
msg, _ := json.Marshal(map[string]any{
|
||||
"type": "historyZoom", "reqId": env["reqId"],
|
||||
@@ -1287,10 +1279,7 @@ func (h *Hub) handleHistoryZoom(c *wsClient, env map[string]interface{}) {
|
||||
// oversampled relative to the plot's point budget and thinned afterwards.
|
||||
// The cap keeps a request for "no decimation" over a multi-hour window from
|
||||
// pulling the whole file into memory.
|
||||
readCap := n * histReadOversample
|
||||
if readCap > histMaxReadPoints {
|
||||
readCap = histMaxReadPoints
|
||||
}
|
||||
readCap := min(n*histReadOversample, histDefaultMaxPoints)
|
||||
|
||||
signals := make(map[string]sigData)
|
||||
for _, k := range strings.Split(sigCSV, ",") {
|
||||
|
||||
@@ -221,6 +221,19 @@ func ringCoverage(bucket, capacity int) int {
|
||||
return capacity / 2 * bucket
|
||||
}
|
||||
|
||||
// captureLagSec is how much further back than the window itself a ring has to
|
||||
// reach to deliver a capture of it.
|
||||
//
|
||||
// A capture is not read out when its last sample arrives but captureMarginSec
|
||||
// later, and then only on the next push tick — so by the time the window is
|
||||
// extracted, its oldest sample is that much deeper in the ring. A ring holding
|
||||
// exactly the window has already overwritten the front of its own capture, which
|
||||
// is what made every shot at a short window come back missing its head. The
|
||||
// pre/post split does not enter into it: the harvest is a post-window after the
|
||||
// trigger and the read reaches a pre-window before it, so the two sum to the
|
||||
// window whatever the split.
|
||||
const captureLagSec = captureMarginSec + 1.0/30.0
|
||||
|
||||
// activeWindowSec is the timespan the buffers must cover. An armed trigger owns
|
||||
// it: its pre-window has to already be in the ring when the trigger fires or
|
||||
// there is nothing to back-fill the capture from. Otherwise it is the widest
|
||||
@@ -228,7 +241,7 @@ func ringCoverage(bucket, capacity int) int {
|
||||
func (h *Hub) activeWindowSec() float64 {
|
||||
if h.trigger != nil && h.trigger.Active() {
|
||||
if cfg := h.trigger.Config(); cfg.windowSec > 0 {
|
||||
return cfg.windowSec
|
||||
return cfg.windowSec + captureLagSec
|
||||
}
|
||||
}
|
||||
widest := 0.0
|
||||
|
||||
@@ -120,7 +120,9 @@ func TestActiveWindowSecTakesTheWidestClientWindow(t *testing.T) {
|
||||
}
|
||||
|
||||
// An armed trigger owns the window: its pre-window has to be in the buffer
|
||||
// before the trigger fires or the capture has nothing to back-fill from.
|
||||
// before the trigger fires or the capture has nothing to back-fill from. The
|
||||
// buffers must reach back past the window itself, because the capture is read
|
||||
// out a margin and a tick after its last sample lands.
|
||||
func TestActiveWindowSecPrefersTheArmedTrigger(t *testing.T) {
|
||||
h := NewHub()
|
||||
c := &wsClient{}
|
||||
@@ -128,8 +130,9 @@ func TestActiveWindowSecPrefersTheArmedTrigger(t *testing.T) {
|
||||
h.clients[c] = true
|
||||
h.trigger.SetConfig(trigConfig{signalKey: "s1:sig", windowSec: 45, mode: "normal"})
|
||||
|
||||
if got := h.activeWindowSec(); got != 45 {
|
||||
t.Fatalf("activeWindowSec = %v, want the trigger's 45", got)
|
||||
if got := h.activeWindowSec(); got != 45+captureLagSec {
|
||||
t.Fatalf("activeWindowSec = %v, want the trigger's 45 plus the %v harvest lag",
|
||||
got, captureLagSec)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,22 @@ type triggerEngine struct {
|
||||
firedPost float64
|
||||
firedValid bool
|
||||
|
||||
// The edge to fire on as soon as the FSM rearms, in sample time. Recorded
|
||||
// while a capture is still being collected or handed out, for edges late
|
||||
// enough that a capture of them would not overlap the one in flight.
|
||||
//
|
||||
// Without this the trigger is deaf from its own trigger point until the
|
||||
// capture has been harvested — a post-window plus captureMarginSec — and
|
||||
// then for the holdoff on top of that, and afterwards waits for a FRESH
|
||||
// edge. On a sparse pulse train that rounds the capture spacing up to a
|
||||
// whole pulse period: at the default 1 s window the blind stretch comes to
|
||||
// 1.15 s, so a 1 Hz train was caught at 0.5 Hz and a wider window lost whole
|
||||
// multiples. Remembering the edge instead makes the blind stretch exactly
|
||||
// the post-window it has to be, since the capture is built from the edge's
|
||||
// own timestamp and the ring still holds everything around it.
|
||||
pendingT float64
|
||||
pendingValid bool
|
||||
|
||||
rearmAt float64 // wall-clock seconds; 0 when no rearm is pending
|
||||
}
|
||||
|
||||
@@ -167,6 +183,9 @@ func (te *triggerEngine) SetConfig(cfg trigConfig) {
|
||||
te.baseKey, te.elemIdx = base, idx
|
||||
te.prevValid = false
|
||||
te.prevValue = 0
|
||||
// An edge held over from the old configuration would be latched against the
|
||||
// new window, whose fill the gate has not vouched for.
|
||||
te.pendingValid = false
|
||||
}
|
||||
|
||||
func (te *triggerEngine) Config() trigConfig {
|
||||
@@ -175,15 +194,37 @@ func (te *triggerEngine) Config() trigConfig {
|
||||
return te.cfg
|
||||
}
|
||||
|
||||
// Arm starts a fresh acquisition. It is the user's own arm, so it discards any
|
||||
// edge remembered during the previous capture: the user asked for the next
|
||||
// event, not for one that has already been and gone.
|
||||
func (te *triggerEngine) Arm() {
|
||||
te.mu.Lock()
|
||||
te.state = trigArmed
|
||||
te.prevValid = false
|
||||
te.prevValue = 0
|
||||
te.pendingValid = false
|
||||
te.rearmAt = 0
|
||||
te.mu.Unlock()
|
||||
}
|
||||
|
||||
// rearm is the automatic arm at the end of a capture. Unlike Arm it honours an
|
||||
// edge that arrived while the capture was being collected, firing on it at once
|
||||
// rather than waiting for the next one — see pendingT. It also keeps the level
|
||||
// tracked through the dead time, so the first sample after rearming is compared
|
||||
// against its real predecessor instead of being spent seeding one.
|
||||
func (te *triggerEngine) rearm() {
|
||||
te.mu.Lock()
|
||||
te.rearmAt = 0
|
||||
if te.pendingValid {
|
||||
t := te.pendingT
|
||||
te.pendingValid = false
|
||||
te.latchWindowLocked(t)
|
||||
} else {
|
||||
te.state = trigArmed
|
||||
}
|
||||
te.mu.Unlock()
|
||||
}
|
||||
|
||||
func (te *triggerEngine) Disarm() {
|
||||
te.mu.Lock()
|
||||
te.state = trigIdle
|
||||
@@ -191,6 +232,7 @@ func (te *triggerEngine) Disarm() {
|
||||
te.prevValid = false
|
||||
te.prevValue = 0
|
||||
te.firedValid = false
|
||||
te.pendingValid = false
|
||||
te.rearmAt = 0
|
||||
te.mu.Unlock()
|
||||
}
|
||||
@@ -366,7 +408,11 @@ func (te *triggerEngine) feed(key string, nElem int, t, v []float64) {
|
||||
te.lastT = t[len(t)-1]
|
||||
te.lastTOK = true
|
||||
te.lastFeedWall = float64(time.Now().UnixNano()) / 1e9
|
||||
if te.state != trigArmed {
|
||||
|
||||
// A capture in flight does not stop the comparator; it only changes what an
|
||||
// edge does. See pendingT.
|
||||
inFlight := te.state == trigCollecting || te.state == trigTriggered
|
||||
if te.state != trigArmed && !inFlight {
|
||||
return
|
||||
}
|
||||
step, start := 1, 0
|
||||
@@ -381,12 +427,20 @@ func (te *triggerEngine) feed(key string, nElem int, t, v []float64) {
|
||||
// which is what made the first shot after a window change come back short.
|
||||
// Track the level meanwhile, so the first edge once the buffer is deep
|
||||
// enough is still measured against the right previous sample.
|
||||
if te.fillLocked() < 1 {
|
||||
if !inFlight && te.fillLocked() < 1 {
|
||||
for i := start; i < len(v); i += step {
|
||||
te.prevValue, te.prevValid = v[i], true
|
||||
}
|
||||
return
|
||||
}
|
||||
// The earliest trigger point a new capture may take. The one in flight owns
|
||||
// everything up to the end of its own post-window, and the holdoff — a guard
|
||||
// against re-triggering on the ringing of the SAME event — is measured from
|
||||
// its trigger point too, so the two overlap rather than add.
|
||||
notBefore := math.Inf(1)
|
||||
if inFlight && te.firedValid {
|
||||
notBefore = te.trigTime + math.Max(te.firedPost, te.cfg.holdoffSec)
|
||||
}
|
||||
thr := te.cfg.threshold
|
||||
for i := start; i < len(t); i += step {
|
||||
if !te.prevValid {
|
||||
@@ -406,10 +460,19 @@ func (te *triggerEngine) feed(key string, nElem int, t, v []float64) {
|
||||
default:
|
||||
fired = up
|
||||
}
|
||||
if fired {
|
||||
if !fired {
|
||||
continue
|
||||
}
|
||||
if !inFlight {
|
||||
te.latchWindowLocked(t[i])
|
||||
return
|
||||
}
|
||||
// Keep the FIRST qualifying edge and go on tracking the level: a later
|
||||
// one would be no more use, and stopping here would leave prevValue
|
||||
// stale by the time the FSM rearms.
|
||||
if !te.pendingValid && t[i] >= notBefore {
|
||||
te.pendingT, te.pendingValid = t[i], true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -640,7 +703,7 @@ func (h *Hub) triggerTick() {
|
||||
// file of its own, where nothing overwrites it until the next trigger.
|
||||
h.hist.captureRange(trigTime-pre, trigTime+post)
|
||||
} else if h.trigger.dueRearm(nowSec) {
|
||||
h.trigger.Arm()
|
||||
h.trigger.rearm()
|
||||
}
|
||||
|
||||
if h.trigger.stateUnsent() {
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
package wshub
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/*
|
||||
Sporadic-signal trigger coverage.
|
||||
|
||||
Every other trigger test in this package feeds a periodic waveform, or a
|
||||
hand-built two-sample batch. Neither can show a trigger that is blind most of
|
||||
the time: a sine crosses the threshold again a few milliseconds after every
|
||||
missed crossing, so a trigger losing 80 % of its edges still fires steadily and
|
||||
looks healthy. A sparse train — 0000000111000000000000, one short burst in a
|
||||
long flat run — has nothing to fall back on, so every missed edge is a missed
|
||||
capture and the yield is a direct measure of how long the FSM was deaf.
|
||||
|
||||
That deafness is what these tests pin down. It is not a bug in itself: a capture
|
||||
cannot be harvested before the samples after its trigger point exist, so the
|
||||
trigger is necessarily blind for its own post-trigger window. What must NOT
|
||||
happen is for edges arriving after that window to be thrown away as well.
|
||||
*/
|
||||
|
||||
// pulseTrainSim drives a Hub the way Run() does — ingest on one side, the
|
||||
// trigger tick on the other — on a simulated clock.
|
||||
type pulseTrainSim struct {
|
||||
rateHz float64
|
||||
batchSec float64
|
||||
pulsePeriod float64
|
||||
pulseSamples int
|
||||
simSec float64
|
||||
windowSec float64
|
||||
prePercent float64
|
||||
holdoffSec float64
|
||||
armAt float64
|
||||
// When windowChangeAt > 0 the window is switched to windowChangeTo at that
|
||||
// time and the trigger re-armed, as a user editing the trigger bar would.
|
||||
windowChangeAt float64
|
||||
windowChangeTo float64
|
||||
}
|
||||
|
||||
type pulseTrainResult struct {
|
||||
pulses int // pulse starts presented after the trigger was armed
|
||||
shots int // captures actually delivered
|
||||
trigTimes []float64 // the sample time each capture triggered on
|
||||
|
||||
worstCov float64 // smallest fraction of its window a capture spanned
|
||||
holdDeclined int // captures the zoom hold would not answer for
|
||||
drawnPulses int // pulses visible in the delivered frames
|
||||
wantPulses int // pulses those frames' windows really contained
|
||||
gatedPulses int // pulses that arrived armed but with the fill gate shut
|
||||
}
|
||||
|
||||
// yield is the fraction of presented pulses that produced a capture.
|
||||
func (r pulseTrainResult) yield() float64 {
|
||||
if r.pulses == 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(r.shots) / float64(r.pulses)
|
||||
}
|
||||
|
||||
// run executes the simulation and returns what the trigger caught.
|
||||
func (s pulseTrainSim) run(t *testing.T, key string) pulseTrainResult {
|
||||
t.Helper()
|
||||
h := NewHub()
|
||||
h.rings[key] = newSigRing(ringCapInitial)
|
||||
h.trigger.SetConfig(trigConfig{
|
||||
signalKey: key, edge: "rising", threshold: 0.5,
|
||||
windowSec: s.windowSec, prePercent: s.prePercent,
|
||||
mode: "normal", holdoffSec: s.holdoffSec,
|
||||
})
|
||||
|
||||
res := pulseTrainResult{worstCov: 1}
|
||||
nBatch := int(s.rateHz * s.batchSec)
|
||||
ts := make([]float64, nBatch)
|
||||
vs := make([]float64, nBatch)
|
||||
|
||||
armed, changed := false, false
|
||||
for now := 0.0; now < s.simSec; now += s.batchSec {
|
||||
nPulseStarts := 0
|
||||
for i := range ts {
|
||||
ts[i] = now + float64(i)/s.rateHz
|
||||
// Position within the current pulse period, in samples.
|
||||
k := int((ts[i] - math.Floor(ts[i]/s.pulsePeriod)*s.pulsePeriod) * s.rateHz)
|
||||
if k < s.pulseSamples {
|
||||
vs[i] = 1
|
||||
if k == 0 {
|
||||
nPulseStarts++
|
||||
}
|
||||
} else {
|
||||
vs[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
if !armed && now >= s.armAt {
|
||||
h.trigger.Arm()
|
||||
armed = true
|
||||
}
|
||||
if s.windowChangeAt > 0 && !changed && now >= s.windowChangeAt {
|
||||
cfg := h.trigger.Config()
|
||||
cfg.windowSec = s.windowChangeTo
|
||||
h.trigger.SetConfig(cfg)
|
||||
h.trigger.Arm()
|
||||
changed = true
|
||||
}
|
||||
|
||||
if armed {
|
||||
res.pulses += nPulseStarts
|
||||
if nPulseStarts > 0 && h.trigger.State() == trigArmed {
|
||||
h.trigger.mu.Lock()
|
||||
f := h.trigger.fillLocked()
|
||||
h.trigger.mu.Unlock()
|
||||
if f < 1 {
|
||||
res.gatedPulses += nPulseStarts
|
||||
}
|
||||
}
|
||||
}
|
||||
h.ingest(key, 1, ts, vs)
|
||||
|
||||
// Mirror triggerTick, on the simulated clock.
|
||||
tick := now + s.batchSec
|
||||
h.retuneRings(tick)
|
||||
_, span := h.rings[key].stats()
|
||||
h.trigger.setBuffered(span, true, tick)
|
||||
|
||||
if trigTime, pre, post, ok := h.trigger.dueCapture(tick); ok {
|
||||
if buf := h.buildTriggerCapture(trigTime, pre, post); buf != nil {
|
||||
res.shots++
|
||||
res.trigTimes = append(res.trigTimes, trigTime)
|
||||
first, last, _ := decodeCaptureSpan(t, buf, key)
|
||||
if cov := (last - first) / (pre + post); cov < res.worstCov {
|
||||
res.worstCov = cov
|
||||
}
|
||||
if _, _, ok := h.capture.slice(key, trigTime-pre, trigTime+post); !ok {
|
||||
res.holdDeclined++
|
||||
}
|
||||
// What the client would actually draw, against what the window
|
||||
// really contained. A wide window holds several pulses, and a
|
||||
// frame showing only the one it triggered on has lost the rest
|
||||
// between the ring, the bucketing and the decimation.
|
||||
_, fv := decodeCaptureSig(t, buf, key)
|
||||
res.drawnPulses += countPulses(fv, 0.5)
|
||||
res.wantPulses += countPulseStarts(trigTime-pre, trigTime+post,
|
||||
s.pulsePeriod, 1/s.rateHz)
|
||||
}
|
||||
h.trigger.markTriggered(tick)
|
||||
} else if h.trigger.dueRearm(tick) {
|
||||
h.trigger.rearm()
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// decodeCaptureSig pulls one signal's samples out of a v2 capture frame.
|
||||
func decodeCaptureSig(t *testing.T, buf []byte, key string) (ts, vs []float64) {
|
||||
t.Helper()
|
||||
off := 1 + 8 + 8 + 8
|
||||
nSig := int(binary.LittleEndian.Uint32(buf[off:]))
|
||||
off += 4
|
||||
for i := 0; i < nSig; i++ {
|
||||
kl := int(binary.LittleEndian.Uint16(buf[off:]))
|
||||
off += 2
|
||||
k := string(buf[off : off+kl])
|
||||
off += kl
|
||||
cnt := int(binary.LittleEndian.Uint32(buf[off:]))
|
||||
off += 4
|
||||
if k == key {
|
||||
ts = make([]float64, cnt)
|
||||
vs = make([]float64, cnt)
|
||||
for j := 0; j < cnt; j++ {
|
||||
ts[j] = math.Float64frombits(binary.LittleEndian.Uint64(buf[off+j*8:]))
|
||||
vs[j] = math.Float64frombits(binary.LittleEndian.Uint64(buf[off+cnt*8+j*8:]))
|
||||
}
|
||||
}
|
||||
off += cnt * 16
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// countPulses counts runs of samples at or above thr.
|
||||
func countPulses(v []float64, thr float64) int {
|
||||
n, in := 0, false
|
||||
for _, x := range v {
|
||||
if x >= thr {
|
||||
if !in {
|
||||
n, in = n+1, true
|
||||
}
|
||||
} else {
|
||||
in = false
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// countPulseStarts is how many pulse starts fall inside [t0, t1]. A pulse
|
||||
// starting within one sample of t1 is not counted: only its first sample is
|
||||
// inside the window, and the ring's min/max bucket for it may put that sample's
|
||||
// extremum just past the edge, which is a boundary artefact rather than a loss.
|
||||
func countPulseStarts(t0, t1, period, dt float64) int {
|
||||
n := 0
|
||||
for k := math.Floor(t0 / period); k*period <= t1; k++ {
|
||||
if p := k * period; p >= t0 && p < t1-2*dt {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// deadTimeSec is how long the FSM is blind after firing at t: it must acquire
|
||||
// the post-trigger window before the capture can be harvested, and the holdoff
|
||||
// guards against re-triggering on the same event. Both are measured from the
|
||||
// trigger point, so they overlap rather than add.
|
||||
func deadTimeSec(windowSec, prePercent, holdoffSec float64) float64 {
|
||||
return math.Max(windowSec*(1-prePercent/100), holdoffSec)
|
||||
}
|
||||
|
||||
// A trigger cannot show two windows at once, so pulses closer together than its
|
||||
// post-trigger window are necessarily lost. Pulses spaced FURTHER apart than
|
||||
// that are not: nothing about the acquisition prevents catching every one.
|
||||
//
|
||||
// This is the reported failure. The FSM used to go deaf from the trigger point
|
||||
// until the capture had been harvested (a post-window plus captureMarginSec)
|
||||
// and the holdoff had then elapsed on top of that, then wait for a fresh edge —
|
||||
// so the effective spacing was rounded UP to a whole pulse period. At the
|
||||
// default 1 s window and 0.2 s holdoff the blind stretch came to 1.15 s, which
|
||||
// is longer than a 1 s pulse period by a hair, and a pulse train at 1 Hz was
|
||||
// caught at 0.5 Hz. Widening the window made it worse in whole multiples.
|
||||
func TestSporadicPulsesWiderThanThePostWindowAreAllCaught(t *testing.T) {
|
||||
const key = "s1:Ch1"
|
||||
cases := []struct{ window, period float64 }{
|
||||
{0.5, 0.5}, // post 0.4 s
|
||||
{1.0, 1.0}, // post 0.8 s — the case the report was made against
|
||||
{2.0, 2.0}, // post 1.6 s
|
||||
{5.0, 5.0}, // post 4.0 s
|
||||
}
|
||||
for _, c := range cases {
|
||||
sim := pulseTrainSim{
|
||||
rateHz: 1000, batchSec: 1.0 / 30.0,
|
||||
pulsePeriod: c.period, pulseSamples: 3,
|
||||
simSec: 41 * c.period, windowSec: c.window, prePercent: 20,
|
||||
holdoffSec: autoRearmDelaySec, armAt: c.period,
|
||||
}
|
||||
res := sim.run(t, key)
|
||||
// Two pulses are always in flight rather than caught: the one that lands
|
||||
// as the trigger arms, and the one still being collected when the run
|
||||
// ends.
|
||||
if got := res.yield(); got < 0.94 {
|
||||
t.Errorf("window %.1f s, pulse every %.1f s: caught %d of %d (%.0f%%); "+
|
||||
"the post-trigger window is only %.2f s, so every pulse fits",
|
||||
c.window, c.period, res.shots, res.pulses, 100*got,
|
||||
c.window*0.8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The loss that remains must be the loss that has to remain. A capture cannot
|
||||
// start before the previous one's post-window is acquired, and it can only start
|
||||
// on a pulse, so consecutive captures are a dead time apart rounded UP to the
|
||||
// next pulse — never further. Any longer gap means an edge that the acquisition
|
||||
// no longer needed was thrown away anyway.
|
||||
//
|
||||
// The bound is stated as dead + period rather than ceil(dead/period)*period
|
||||
// because when the two divide exactly, whether the pulse at the boundary counts
|
||||
// comes down to the last bit of the sample timestamp. Both answers are correct;
|
||||
// a gap beyond either is not.
|
||||
func TestSporadicCaptureGapsStayWithinTheDeadTime(t *testing.T) {
|
||||
const key = "s1:Ch1"
|
||||
for _, window := range []float64{0.5, 1.0, 2.0, 5.0} {
|
||||
for _, period := range []float64{0.25, 0.5, 1.0, 2.0} {
|
||||
sim := pulseTrainSim{
|
||||
rateHz: 1000, batchSec: 1.0 / 30.0,
|
||||
pulsePeriod: period, pulseSamples: 3,
|
||||
simSec: 60, windowSec: window, prePercent: 20,
|
||||
holdoffSec: autoRearmDelaySec, armAt: 1.0,
|
||||
}
|
||||
res := sim.run(t, key)
|
||||
dead := deadTimeSec(window, 20, autoRearmDelaySec)
|
||||
limit := dead + period + 2*sim.batchSec
|
||||
worst, worstAt := 0.0, 0.0
|
||||
for i := 1; i < len(res.trigTimes); i++ {
|
||||
if g := res.trigTimes[i] - res.trigTimes[i-1]; g > worst {
|
||||
worst, worstAt = g, res.trigTimes[i-1]
|
||||
}
|
||||
}
|
||||
if worst > limit {
|
||||
t.Errorf("window %.1f s, pulse every %.2f s: %.2f s between the captures "+
|
||||
"at %.2f s and %.2f s; the dead time is only %.2f s, so %.2f s is the most "+
|
||||
"that can be missed",
|
||||
window, period, worst, worstAt, worstAt+worst, dead, limit)
|
||||
}
|
||||
t.Logf("window %.1f s, pulse every %.2f s: %d/%d captures (%.0f%%), "+
|
||||
"dead time %.2f s, worst gap %.2f s, gated %d",
|
||||
window, period, res.shots, res.pulses, 100*res.yield(), dead,
|
||||
worst, res.gatedPulses)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Whatever the trigger does catch has to come back whole: a window wide enough
|
||||
// to hold several pulses must show all of them, at every rate, including the
|
||||
// rates that force the ring into min/max bucketing.
|
||||
func TestSporadicCaptureShowsEveryPulseInItsWindow(t *testing.T) {
|
||||
const key = "s1:Ch1"
|
||||
for _, rate := range []float64{1000, 200e3} {
|
||||
for _, window := range []float64{1.0, 2.0, 5.0} {
|
||||
sim := pulseTrainSim{
|
||||
rateHz: rate, batchSec: 1.0 / 30.0,
|
||||
pulsePeriod: 0.5, pulseSamples: 3,
|
||||
simSec: 40, windowSec: window, prePercent: 20,
|
||||
holdoffSec: autoRearmDelaySec, armAt: 1.0,
|
||||
}
|
||||
res := sim.run(t, key)
|
||||
if res.shots == 0 {
|
||||
t.Fatalf("rate %.0f window %.1f s: no captures at all", rate, window)
|
||||
}
|
||||
// wantPulses excludes the pulse straddling each window's far edge,
|
||||
// whose bucket may place its extremum just past it, so the frames
|
||||
// may legitimately draw a few more than that — but never fewer.
|
||||
if res.drawnPulses < res.wantPulses {
|
||||
t.Errorf("rate %.0f window %.1f s: frames drew %d pulses, their windows held %d",
|
||||
rate, window, res.drawnPulses, res.wantPulses)
|
||||
}
|
||||
if res.worstCov < 0.98 {
|
||||
t.Errorf("rate %.0f window %.1f s: worst capture spanned %.0f%% of its window",
|
||||
rate, window, 100*res.worstCov)
|
||||
}
|
||||
if res.holdDeclined > 0 {
|
||||
t.Errorf("rate %.0f window %.1f s: the zoom hold declined %d of %d captures",
|
||||
rate, window, res.holdDeclined, res.shots)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Widening the window mid-run is the gesture the report came from. The fill gate
|
||||
// holds the first shot off until the ring reaches back far enough, which is
|
||||
// correct; what it must not do is stay shut, nor leave the trigger losing pulses
|
||||
// once the ring has caught up.
|
||||
func TestSporadicYieldRecoversAfterAWindowChange(t *testing.T) {
|
||||
const key = "s1:Ch1"
|
||||
for _, w := range []float64{1.0, 2.0, 5.0} {
|
||||
sim := pulseTrainSim{
|
||||
rateHz: 200e3, batchSec: 1.0 / 30.0,
|
||||
pulsePeriod: w, pulseSamples: 3,
|
||||
simSec: 30 * w, windowSec: 0.2, prePercent: 20,
|
||||
holdoffSec: autoRearmDelaySec, armAt: 1.0,
|
||||
windowChangeAt: 10 * w, windowChangeTo: w,
|
||||
}
|
||||
res := sim.run(t, key)
|
||||
// Count only what happened after the change settled.
|
||||
after, want := 0, 0
|
||||
for _, tt := range res.trigTimes {
|
||||
if tt > 11*w {
|
||||
after++
|
||||
}
|
||||
}
|
||||
for p := 11 * w; p < 30*w; p += w {
|
||||
want++
|
||||
}
|
||||
if float64(after) < 0.9*float64(want) {
|
||||
t.Errorf("window 0.2 -> %.1f s: %d captures in the %d pulses after the change",
|
||||
w, after, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user