Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
Maths.cpp
00001 #include "Maths.h" 00002 #include "mbed.h" 00003 00004 int Maths::comp(const void* elem1, const void* elem2) 00005 { 00006 if(*(const float*)elem1 < *(const float*)elem2) 00007 return -1; 00008 return *(const float*)elem1 > *(const float*)elem2; 00009 } 00010 00011 float Maths::mean(float *samples, int n, int n2) 00012 { 00013 float sum = 0.0; 00014 for (int i=0; i<n2-n; i++) { 00015 sum += samples[n+i]; 00016 } 00017 return sum / (float)(n2-n); 00018 } 00019 00020 float Maths::median(float *samples, int n) 00021 { 00022 qsort(samples, n, sizeof(float), Maths::comp); 00023 00024 return mean(samples, (n - 1)/2 - 990,(n - 1)/2 + 990); 00025 } 00026 00027 float Maths::stdev(float data[] , int n) 00028 { 00029 float sum = 0.0, mean2, standardDeviation = 0.0; 00030 int i; 00031 mean2 = Maths::mean(data,0,n); 00032 00033 for(i = 0; i < n; ++i) 00034 standardDeviation += pow(data[i] - mean2, 2); 00035 00036 return sqrt(standardDeviation / n); 00037 } 00038 00039 00040 float Maths::meanNoOutliers(float data[] , int n) 00041 { 00042 float mean = Maths::mean(data,0,n); 00043 float stdev = Maths::stdev(data,n); 00044 float amount = 0; 00045 float sum = 0; 00046 for(int i = 0; i < n; ++i) { 00047 if (data[i] <= mean + 1*stdev && data[i] >= mean - 1*stdev) { 00048 amount ++; 00049 sum += data[i]; 00050 } 00051 } 00052 return sum/amount; 00053 00054 00055 }
Generated on Tue Jul 19 2022 01:01:54 by
