summaryrefslogtreecommitdiff
path: root/server/internal/detect/decimate.go
blob: fbcae988fcff74f9f74969cdf5ba444366631c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package detect

// Decimate downsamples by averaging fixed-size blocks rather than dropping samples.
//
// Only a cheap pre-filter ahead of a coarse alignment search: averaging keeps the signal's
// overall shape, which is all a coarse search needs, where keeping every Nth sample would
// alias. Correctness of the real match still comes from re-checking at full resolution.
func Decimate(samples []float64, factor int) []float64 {
	if factor <= 1 {
		return samples
	}
	out := make([]float64, len(samples)/factor)
	for i := range out {
		sum := 0.0
		base := i * factor
		for j := 0; j < factor; j++ {
			sum += samples[base+j]
		}
		out[i] = sum / float64(factor)
	}
	return out
}