79 lines
1.8 KiB
C++
79 lines
1.8 KiB
C++
#ifndef _HELPER_H_
|
|
#define _HELPER_H
|
|
|
|
#pragma once
|
|
|
|
#ifndef _CATENA_H_
|
|
#include <Catena.h>
|
|
#endif
|
|
|
|
using namespace McciCatena;
|
|
|
|
// the primary object
|
|
Catena gCatena;
|
|
|
|
|
|
//Following functions are based on "https://github.com/dndubins/QuickStats", by David Dubins
|
|
|
|
void bubbleSort(long A[], int len) {
|
|
unsigned long newn;
|
|
unsigned long n = len;
|
|
long temp = 0;
|
|
do {
|
|
newn = 1;
|
|
for (int p = 1; p < len; p++) {
|
|
if (A[p - 1] > A[p]) {
|
|
temp = A[p]; //swap places in array
|
|
A[p] = A[p - 1];
|
|
A[p - 1] = temp;
|
|
newn = p;
|
|
} //end if
|
|
} //end for
|
|
n = newn;
|
|
} while (n > 1);
|
|
}
|
|
|
|
long median(long samples[], int m) //calculate the median
|
|
{
|
|
//First bubble sort the values: https://en.wikipedia.org/wiki/Bubble_sort
|
|
long sorted[m]; // Define and initialize sorted array.
|
|
long temp = 0; // Temporary float for swapping elements
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
sorted[i] = samples[i];
|
|
}
|
|
bubbleSort(sorted, m); // Sort the values
|
|
|
|
if (bitRead(m, 0) == 1) { //If the last bit of a number is 1, it's odd. This is equivalent to "TRUE". Also use if m%2!=0.
|
|
return sorted[m / 2]; //If the number of data points is odd, return middle number.
|
|
} else {
|
|
return (sorted[(m / 2) - 1] + sorted[m / 2]) / 2; //If the number of data points is even, return avg of the middle two numbers.
|
|
}
|
|
}
|
|
|
|
// Joergs STDDEV
|
|
float stddev(long samples[], int m) //calculate the stdandard deviation
|
|
{
|
|
float sum_x;
|
|
float sum_x2;
|
|
float mean;
|
|
|
|
float stdev;
|
|
sum_x = 0;
|
|
sum_x2 = 0;
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
sum_x = sum_x + samples[i];
|
|
}
|
|
mean = sum_x / m;
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
sum_x2 = sum_x2 + ((samples[i] - mean) * (samples[i] - mean));
|
|
}
|
|
|
|
stdev = sqrt(sum_x2 / m);
|
|
|
|
return stdev;
|
|
}
|
|
#endif
|