[text] step

Viewer

  1. /**
  2. *
  3. * This program will quickly read a CSV file into the correct data types.
  4. * Testied and working in Win10 and OSX.
  5. * To compile on OSX use: g++ -std=c++0x parseCSV.cpp -lpthread
  6. */
  7.  
  8.  
  9. //required so will compile in M$FT VS. Must be listed before
  10. //the inclusion of "csv.h"
  11. #define _CRT_SECURE_NO_WARNINGS
  12.  
  13. //from https://github.com/ben-strasser/fast-cpp-csv-parser
  14. #include "csv.h"
  15. #include <iostream>
  16. #include <string>
  17. #include <cmath>
  18. using namespace std;
  19.  
  20. int CountSteps(int MovingAvg[], int start, int end) //Function for countingsteps
  21. {
  22.         int steps = 0;
  23.         int Mid = (start + end) / 2;
  24.         //cout << "\n" << Mid;
  25.         if ((MovingAvg[Mid] >= MovingAvg[Mid + 1]) && (MovingAvg[Mid] >= MovingAvg[Mid - 1]))
  26.         {
  27.                 steps++;
  28.                 //cout  << "Step @: " << Mid << "\n" << endl;
  29.         } else if (MovingAvg[Mid] < MovingAvg[Mid - 1])
  30.         {
  31.                 CountSteps(MovingAvg, start, Mid-1);
  32.         }
  33.         else if (MovingAvg[Mid] < MovingAvg[Mid + 1])
  34.         {
  35.                 CountSteps(MovingAvg, Mid+1, end);
  36.         }
  37.         return steps;
  38. }
  39. int main()
  40. {
  41.         //create a string with the full/absolute file path for the data to be read.
  42.         string fileName = "70Steps.csv";
  43.  
  44.         //create the reader object to parse a file with 3 columns
  45.         io::CSVReader<3> in(fileName);
  46.  
  47.         //tell the class what are the names of the three columns
  48.         in.read_header(io::ignore_extra_column, "x", "y", "z");
  49.  
  50.         //create variables to hold the data points for each row
  51.         int x; int y; int z;
  52.  
  53.         //read through the file and do whatever you like with the data
  54.         bool available = true;
  55.         const int len = 10;
  56.         int Vector[len+1];
  57.         int Diff[len];
  58.         int Square[len];
  59.         int MovingAvg[len];
  60.         int index = 0;
  61.         int counter = 0;
  62.         int totalsteps = 0;
  63.         int time = 0;
  64.         while (available)
  65.         {
  66.                 int sum = 0;
  67.                 available = in.read_row(x, y, z);
  68.                 Vector[index % (len+1)] = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));//get the vector of each point and store in the vector array
  69.                 Diff[index % len] = Vector[index % (len+1)] - Vector[(index-1) % (len+1)]; //Get difference of  i+1 to i
  70.                 Square[index % len] = Diff[index % len] * Diff[index % len]; //Get square value of each Diff number
  71.                
  72.                
  73.                 for (int i = 0; i < len; i++)
  74.                 {
  75.                                 sum += Square[i];
  76.                 }
  77.                 MovingAvg[index % len] = sum/len; 
  78.                 //cout << index << ": MovAvg: " << MovingAvg[index & len] << endl;
  79.                 //cout << "\n";
  80.                 if (index == 7) //waits til 5 data points are filled in array
  81.                 {
  82.                         if (time == 1) {
  83.                                 counter = CountSteps(MovingAvg, 0, 20); // Call the function CountSteps
  84.                                 totalsteps += counter; //addes counter to total steps
  85.                                 index = 0;
  86.                                 time = 0;
  87.                         }
  88.                         else {
  89.                                 time++;
  90.                                 index = 0;
  91.                         }
  92.                 }
  93.                
  94.                 index++;
  95.         }
  96.         cout << "\nStep = " << totalsteps;
  97.         return 0;
  98. }

Editor

You can edit this paste and save as new:


File Description
  • step
  • Paste Code
  • 02 Dec-2022
  • 2.63 Kb
You can Share it: