Isabella Gomez Torres / Mbed OS TEST_SideToF

Dependencies:   QEI2 PID Watchdog VL53L1X_Filter ros_lib_kinetic

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers statistics.cpp Source File

statistics.cpp

00001 
00002 #include "statistics.h"
00003 #include "mbed.h"
00004 
00005     /**************************************************************************
00006      * This function initializes the private members of the class Statistics  *
00007      **************************************************************************/
00008     statistics::statistics(int* Input, double dataLengthIn, int firstDataPointIn){
00009         data = Input;
00010         dataLength = dataLengthIn;
00011         firstDataPoint = firstDataPointIn;
00012         }
00013         
00014     /**************************************************************************
00015      * This function returns the mean(average) of an array.                   *
00016      **************************************************************************/
00017     double statistics::mean(){
00018         double sum;
00019         for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i)
00020         {
00021             sum += data[i];
00022         }
00023         //printf("sum %f, data length %f\r\n", sum, dataLength);
00024         wait(.001);
00025         double average = sum/dataLength;
00026         return average;
00027     }
00028     
00029     /**************************************************************************
00030      * This function returns the standard deviation of an array. This is      *
00031      * used to determine whether the data is valid or not.                    *
00032      **************************************************************************/
00033     double statistics::stdev(){
00034         double sum = 0.0, mean, standardDeviation = 0.0;
00035         //printf("The length of the array is %d\n", dataLength);
00036         //double num = (pow(2.0,2.0));
00037         //printf("2^2 is %f\n", num);
00038         //int i;
00039 
00040         for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i)
00041         {
00042             sum += data[i];
00043             //printf("%d\n", data[i]);
00044         }
00045     
00046         mean = sum/dataLength;
00047     
00048         for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i) {
00049             //standardDeviation += pow(data[i] - mean, 2);
00050             standardDeviation += (data[i] - mean)*(data[i] - mean);
00051             }
00052     
00053         return sqrt(standardDeviation / dataLength);
00054     }