Split out hhio

This commit is contained in:
Ian Gulliver
2022-09-24 16:35:03 -07:00
parent 65788a6620
commit f958e7d1c0
5 changed files with 20 additions and 8 deletions

36
hhio/medianfilter.go Normal file
View File

@@ -0,0 +1,36 @@
package hhio
import "sort"
func NewMedianFilter(in chan float64, num int) chan float64 {
out := make(chan float64)
go func() {
defer close(out)
buf := make([]float64, num, num)
srt := make([]float64, num, num)
next := 0
for {
v, ok := <-in
if !ok {
return
}
buf[next%num] = v
next++
if next < num {
continue
}
copy(srt, buf)
sort.Float64s(srt)
out <- srt[num/2+1]
}
}()
return out
}