Initial commit
This commit is contained in:
36
medianfilter.go
Normal file
36
medianfilter.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user