From 07c8d4774af55fa25f7481858a7ba6265a10fa54 Mon Sep 17 00:00:00 2001 From: Russell Stanley Date: Mon, 10 Jan 2022 10:08:54 +1030 Subject: [PATCH] add standard deviation function --- turbidity/plot.go | 11 +++++++++++ turbidity/turbidity.go | 2 +- turbidity/turbidity_test.go | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/turbidity/plot.go b/turbidity/plot.go index 1c950ec1..f0e49c13 100644 --- a/turbidity/plot.go +++ b/turbidity/plot.go @@ -34,6 +34,17 @@ import ( "gonum.org/v1/plot/vg" ) +// standarDeviation will return the standard deviation of a float slice +func standarDeviation(slice []float64) float64 { + mean := average(slice) + variance := 0.0 + + for _, i := range slice { + variance += math.Pow(i-mean, 2.0) + } + return math.Sqrt(variance / float64(len(slice))) +} + // Normalize values in a slice between 0 and 1. func normalize(slice []float64) []float64 { max := -math.MaxFloat64 diff --git a/turbidity/turbidity.go b/turbidity/turbidity.go index a8e3a9a7..11741c18 100644 --- a/turbidity/turbidity.go +++ b/turbidity/turbidity.go @@ -142,7 +142,7 @@ func (ts TurbiditySensor) minMax(img gocv.Mat, xStart, yStart, xEnd, yEnd int) ( for j := yStart; j < yEnd; j++ { value := float64(img.GetUCharAt(i, j)) - // Check max/min conditions, zero values are ignored. + // Check max/min conditions, zero values are ignoredt to avoid divison by 0. if value > max && value != 0.0 { max = value } diff --git a/turbidity/turbidity_test.go b/turbidity/turbidity_test.go index 82f38643..830c4866 100644 --- a/turbidity/turbidity_test.go +++ b/turbidity/turbidity_test.go @@ -76,7 +76,7 @@ func TestImages(t *testing.T) { // Evaluate camera burst. sample_result, err := ts.Evaluate(imgs[i]) if err != nil { - t.Fatalf("evaluation Failed: %w", err) + t.Fatalf("evaluation Failed: %v", err) } // Add the average result from camera burst. @@ -86,7 +86,7 @@ func TestImages(t *testing.T) { // Plot the final results. err = plotResults(results.Turbidity, normalize(results.Sharpness), normalize(results.Contrast)) if err != nil { - t.Fatalf("plotting Failed: %w", err) + t.Fatalf("plotting Failed: %v", err) } t.Logf("Sharpness: %v", results.Sharpness)