2020-10-29 17:33:18 +00:00
|
|
|
#ifndef _MATH_GUARD
|
|
|
|
#define _MATH_GUARD
|
|
|
|
|
|
|
|
typedef struct Stats {
|
|
|
|
float mean;
|
|
|
|
float std;
|
|
|
|
} Stats;
|
|
|
|
|
|
|
|
int
|
2020-11-16 22:05:44 +00:00
|
|
|
ceil(float in) // self-implement ceil func, no math.h
|
2020-10-29 17:33:18 +00:00
|
|
|
{
|
|
|
|
int num = (int) in;
|
|
|
|
if(in - num > 0) num++;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2020-11-23 18:59:27 +00:00
|
|
|
sqrt(float in) // self-implement sqrt func, no math.h
|
2020-10-29 17:33:18 +00:00
|
|
|
{
|
|
|
|
float sqrt = in/2;
|
|
|
|
float temp = 0;
|
|
|
|
|
|
|
|
while(sqrt != temp)
|
|
|
|
{
|
|
|
|
temp = sqrt;
|
|
|
|
sqrt = (in/temp + temp) / 2;
|
|
|
|
}
|
|
|
|
return sqrt;
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
calculateMean(float buffer[], int length)
|
|
|
|
{
|
|
|
|
if(length <= 0)
|
|
|
|
{
|
|
|
|
printf("%i items is not valid length\n", length);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-18 19:02:24 +00:00
|
|
|
|
|
|
|
/* SUM */
|
2020-10-29 17:33:18 +00:00
|
|
|
float sum = 0;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
sum += buffer[i];
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:02:24 +00:00
|
|
|
return sum / length; // DIVIDE ON RETURN
|
2020-10-29 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Stats
|
|
|
|
calculateStdDev(float buffer[], int length)
|
|
|
|
{
|
|
|
|
Stats stats;
|
|
|
|
if(length <= 0)
|
|
|
|
{
|
|
|
|
printf("%i items is not valid length\n", length);
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.mean = calculateMean(buffer, length);
|
|
|
|
|
|
|
|
float sum = 0;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
2020-11-18 19:02:24 +00:00
|
|
|
float diffFromMean = buffer[i] - stats.mean; // (xi - mu)
|
|
|
|
sum += diffFromMean*diffFromMean; // Sum(diff squared)
|
2020-10-29 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stats.std = sqrt(sum/length);
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|