Skip to content

Commit

Permalink
Fix for resampler
Browse files Browse the repository at this point in the history
  • Loading branch information
adityauj committed Aug 25, 2024
1 parent b186dca commit 8cefaba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 15 additions & 4 deletions pkg/resampler/resampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resampler

import (
"errors"
"fmt"
"math"

"github.com/ClusterCockpit/cc-metric-store/internal/util"
Expand Down Expand Up @@ -34,14 +35,14 @@ func SimpleResampler(data []util.Float, old_frequency int64, new_frequency int64

// Inspired by one of the algorithms from https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf
// Adapted from https://github.com/haoel/downsampling/blob/master/core/lttb.go
func LargestTriangleThreeBucket(data []util.Float, old_frequency int64, new_frequency int64) ([]util.Float, int64, error) {
func LargestTriangleThreeBucket(data []util.Float, old_frequency int, new_frequency int) ([]util.Float, int, error) {

if old_frequency == 0 || new_frequency == 0 {
return data, old_frequency, nil
}

if new_frequency%old_frequency != 0 {
return nil, 0, errors.New("new sampling frequency should be multiple of the old frequency")
return nil, 0, errors.New(fmt.Sprintf("new sampling frequency : %d should be multiple of the old frequency : %d", new_frequency, old_frequency))
}

var step int = int(new_frequency / old_frequency)
Expand Down Expand Up @@ -89,17 +90,27 @@ func LargestTriangleThreeBucket(data []util.Float, old_frequency int64, new_freq
maxArea := -1.0

var maxAreaPoint int
flag_ := 0
for ; currBucketStart < currBucketEnd; currBucketStart++ {

area := calculateTriangleArea(util.Float(pointX), pointY, avgPointX, avgPointY, util.Float(currBucketStart), data[currBucketStart])
if area > maxArea {
maxArea = area
maxAreaPoint = currBucketStart
}
if math.IsNaN(float64(avgPointY)) {
flag_ = 1

}
}

new_data = append(new_data, data[maxAreaPoint]) // Pick this point from the bucket
prevMaxAreaPoint = maxAreaPoint // This MaxArea point is the next's prevMAxAreaPoint
if flag_ == 1 {
new_data = append(new_data, util.NaN) // Pick this point from the bucket

} else {
new_data = append(new_data, data[maxAreaPoint]) // Pick this point from the bucket
}
prevMaxAreaPoint = maxAreaPoint // This MaxArea point is the next's prevMAxAreaPoint

//move to the next window
bucketLow = bucketMiddle
Expand Down
14 changes: 12 additions & 2 deletions pkg/resampler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ func calculateTriangleArea(paX, paY, pbX, pbY, pcX, pcY util.Float) float64 {
}

func calculateAverageDataPoint(points []util.Float, xStart int64) (avgX util.Float, avgY util.Float) {

flag := 0
for _, point := range points {
avgX += util.Float(xStart)
avgY += point
xStart++
if math.IsNaN(float64(point)) {
flag = 1
}
}

l := util.Float(len(points))

avgX /= l
avgY /= l
return avgX, avgY

if flag == 1 {
return avgX, util.NaN
} else {
return avgX, avgY
}
}

0 comments on commit 8cefaba

Please sign in to comment.