From 5fb151ac296d3ca824954b4cfa94a44c04eb3a6c Mon Sep 17 00:00:00 2001 From: noriah Date: Mon, 3 Apr 2023 11:40:50 -0700 Subject: [PATCH] fix some window and math stuffs --- graphic/display.go | 12 ++++++------ util/window.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/graphic/display.go b/graphic/display.go index 3a4d583..8c94652 100644 --- a/graphic/display.go +++ b/graphic/display.go @@ -2,7 +2,6 @@ package graphic import ( "context" - "sync" "sync/atomic" "github.com/noriah/catnip/util" @@ -27,7 +26,7 @@ const ( NumRunes = 8 // ScalingWindow in seconds - ScalingWindow = 5 + ScalingWindow = 2.5 // PeakThreshold is the threshold to not draw if the peak is less. PeakThreshold = 0.01 ) @@ -97,7 +96,6 @@ type Display struct { drawType DrawType styles Styles styleBuffer []termbox.Attribute - wg sync.WaitGroup ctx context.Context cancel context.CancelFunc } @@ -302,7 +300,11 @@ func (d *Display) Write(buffers [][]float64, channels int) error { if peak >= PeakThreshold { vMean, vSD := d.window.Update(peak) - if t := vMean + (1.5 * vSD); peak > t { + if t := vMean + (1.25 * vSD); peak > t { + vMean, vSD = d.window.Drop(5) + } + + if t := vMean - (1.5 * vSD); peak < t { vMean, vSD = d.window.Drop(5) } @@ -311,8 +313,6 @@ func (d *Display) Write(buffers [][]float64, channels int) error { } } - d.wg.Add(channels) - switch d.drawType { case DrawUp: d.drawUp(buffers, channels, scale) diff --git a/util/window.go b/util/window.go index 65e5cc4..aa45ef9 100644 --- a/util/window.go +++ b/util/window.go @@ -77,8 +77,8 @@ func (mw *MovingWindow) Drop(count int) (mean float64, stddev float64) { old := mw.data[idx] + mw.variance -= math.Pow(old-mw.average, 2.0) mw.average -= old / float64(mw.length) - mw.variance -= old * old mw.length-- count--