Added variable Test SideToF sensore
Dependencies: QEI2 chair_BNO055 PID Watchdog VL53L1X_Filter ros_lib_kinetic
Statistics/statistics.cpp@31:06f2362caf12, 2019-07-01 (annotated)
- Committer:
- t1jain
- Date:
- Mon Jul 01 23:20:48 2019 +0000
- Revision:
- 31:06f2362caf12
- Parent:
- 30:c25b2556e84d
aa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jvfausto | 28:6d6bd8ad04dc | 1 | |
jvfausto | 27:da718b990837 | 2 | #include "statistics.h" |
jvfausto | 27:da718b990837 | 3 | #include "mbed.h" |
jvfausto | 27:da718b990837 | 4 | |
jvfausto | 28:6d6bd8ad04dc | 5 | /************************************************************************** |
jvfausto | 28:6d6bd8ad04dc | 6 | * This function initializes the private members of the class Statistics * |
jvfausto | 28:6d6bd8ad04dc | 7 | **************************************************************************/ |
t1jain | 31:06f2362caf12 | 8 | statistics::statistics(int* Input, double dataLengthIn, int firstDataPointIn){ |
jvfausto | 27:da718b990837 | 9 | data = Input; |
jvfausto | 27:da718b990837 | 10 | dataLength = dataLengthIn; |
jvfausto | 28:6d6bd8ad04dc | 11 | firstDataPoint = firstDataPointIn; |
jvfausto | 27:da718b990837 | 12 | } |
jvfausto | 28:6d6bd8ad04dc | 13 | |
jvfausto | 28:6d6bd8ad04dc | 14 | /************************************************************************** |
jvfausto | 28:6d6bd8ad04dc | 15 | * This function returns the mean(average) of an array. * |
jvfausto | 28:6d6bd8ad04dc | 16 | **************************************************************************/ |
jvfausto | 27:da718b990837 | 17 | double statistics::mean(){ |
jvfausto | 27:da718b990837 | 18 | double sum; |
jvfausto | 28:6d6bd8ad04dc | 19 | for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i) |
jvfausto | 27:da718b990837 | 20 | { |
jvfausto | 27:da718b990837 | 21 | sum += data[i]; |
jvfausto | 27:da718b990837 | 22 | } |
t1jain | 31:06f2362caf12 | 23 | //printf("sum %f, data length %f\r\n", sum, dataLength); |
t1jain | 31:06f2362caf12 | 24 | wait(.001); |
jvfausto | 27:da718b990837 | 25 | double average = sum/dataLength; |
jvfausto | 27:da718b990837 | 26 | return average; |
jvfausto | 27:da718b990837 | 27 | } |
jvfausto | 28:6d6bd8ad04dc | 28 | |
jvfausto | 28:6d6bd8ad04dc | 29 | /************************************************************************** |
jvfausto | 28:6d6bd8ad04dc | 30 | * This function returns the standard deviation of an array. This is * |
jvfausto | 28:6d6bd8ad04dc | 31 | * used to determine whether the data is valid or not. * |
jvfausto | 28:6d6bd8ad04dc | 32 | **************************************************************************/ |
jvfausto | 27:da718b990837 | 33 | double statistics::stdev(){ |
jvfausto | 28:6d6bd8ad04dc | 34 | double sum = 0.0, mean, standardDeviation = 0.0; |
t1jain | 31:06f2362caf12 | 35 | //printf("The length of the array is %d\n", dataLength); |
jvfausto | 28:6d6bd8ad04dc | 36 | //double num = (pow(2.0,2.0)); |
jvfausto | 28:6d6bd8ad04dc | 37 | //printf("2^2 is %f\n", num); |
jvfausto | 28:6d6bd8ad04dc | 38 | //int i; |
jvfausto | 27:da718b990837 | 39 | |
jvfausto | 28:6d6bd8ad04dc | 40 | for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i) |
jvfausto | 27:da718b990837 | 41 | { |
jvfausto | 27:da718b990837 | 42 | sum += data[i]; |
t1jain | 30:c25b2556e84d | 43 | //printf("%d\n", data[i]); |
jvfausto | 27:da718b990837 | 44 | } |
jvfausto | 27:da718b990837 | 45 | |
jvfausto | 27:da718b990837 | 46 | mean = sum/dataLength; |
jvfausto | 27:da718b990837 | 47 | |
jvfausto | 28:6d6bd8ad04dc | 48 | for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i) { |
jvfausto | 28:6d6bd8ad04dc | 49 | //standardDeviation += pow(data[i] - mean, 2); |
jvfausto | 28:6d6bd8ad04dc | 50 | standardDeviation += (data[i] - mean)*(data[i] - mean); |
jvfausto | 28:6d6bd8ad04dc | 51 | } |
jvfausto | 27:da718b990837 | 52 | |
jvfausto | 27:da718b990837 | 53 | return sqrt(standardDeviation / dataLength); |
jvfausto | 28:6d6bd8ad04dc | 54 | } |