Advertisement
N3rdsWithGame

advanced lab code

Jul 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <filesystem>
  4. #include <string>
  5. #include <strstream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. namespace fs = std::experimental::filesystem;
  10.  
  11. void calcReg(vector<double>&, vector<double>&, double &, double&);
  12.  
  13. int main(int argc, char** argv) {
  14.  
  15.    
  16.     //return 0;
  17.    
  18.     vector<double> I;
  19.     vector<double> V;
  20.  
  21.     vector<double> x;
  22.     vector<double> y;
  23.  
  24.     string path = ".";
  25.     string avg = " averged.csv";
  26.  
  27.     ifstream in;
  28.     ofstream out;
  29.     double iTemp = 0, vTemp = 0, iTempOld = 0, vTempOld = 0, dTemp=0;
  30.     bool first = true;
  31.     int i = 0, j = 0;
  32.  
  33.     int n = 0;
  34.  
  35.     double a = 0, b = 0;
  36.  
  37.     int tail = 2000;
  38.  
  39.     int numToAvg = 12;
  40.     int shift = numToAvg;
  41.  
  42.     std::cout << "How many do I average?  >>";
  43.     std::cin >> numToAvg;
  44.  
  45.     std::cout << "How much to shift the average? >>";
  46.     std::cin >> shift;
  47.  
  48.     double threshold = .15;
  49.     for (auto & p : fs::directory_iterator(path)) {
  50.        
  51.  
  52.         if (fs::is_directory(p))
  53.             continue;
  54.         if (fs::path(p).extension() != ".csv")
  55.             continue;
  56.         first = false;
  57.         in.open(p.path(), ios::in);
  58.         std::cout << in.is_open() << "\t\tbegin reading data from " << p.path() << std::endl;
  59.         out.open(path + "\\ average\\" + p.path().filename().string(), ios::out | ios::trunc);
  60.         std::cout << out.is_open() << "\t\tout file dir " << path + "average\\" + p.path().filename().string() << std::endl;
  61.         while (!in.eof()) {
  62.             in >> iTemp; in.get();
  63.             in >> vTemp; in.get();
  64.             if (vTemp > 0) {
  65.                 I.push_back(iTemp);
  66.                 V.push_back(vTemp);
  67.             }
  68.         }
  69.         /*
  70.         //Calcualte lin reg of the tail end, then decide what the turning point is
  71.         n = V.size();
  72.         for (i = 0; i < n; i++) {
  73.             if (V[i] > 1.2) {
  74.                 x.push_back(V[i]);
  75.                 y.push_back(I[i]);
  76.             }
  77.         }
  78.         calcReg(x, y, a, b);
  79.  
  80.         for (int i = 0; i < n; i++) {
  81.             vTempOld = a + b*V[i];
  82.             vTemp = I[i] - vTempOld;
  83.            
  84.             iTemp = vTemp/vTempOld;
  85.             out << I[i] << "," << V[i] << ", ," <<vTemp<< ","<< iTemp*100<<"," << vTempOld << endl;
  86.         }
  87.         */
  88.         //-------------------------------------------------------------
  89.  
  90.         //Do some averaging to use with GNUPlot for final figures.
  91.         for (i = 0; i < I.size() && j < I.size()-i; i+=shift) {
  92.             iTemp = vTemp = 0;
  93.             for (j = 0; j < numToAvg && j < I.size()-i; j += 1) {
  94.                 iTemp += I[i+j];
  95.                 vTemp += V[i+j];
  96.                 //std::cout<<i+j<<"\t" << I[i+j] << "\t" << V[i+j] << std::endl
  97.             }
  98.             if(i==0)
  99.                 out << vTemp / numToAvg << "," << iTemp / numToAvg << ","<< 0 << std::endl;
  100.             else {
  101.                 dTemp = (iTempOld - iTemp) / (vTempOld - vTemp);
  102.                 if (abs(dTemp) > 1)
  103.                     dTemp = 0;
  104.                 out << vTemp / numToAvg << "," << iTemp / numToAvg << "," << dTemp << std::endl;
  105.             }
  106.             vTempOld = vTemp;
  107.             iTempOld = iTemp;
  108.         }//*/
  109.         I.clear();
  110.         V.clear();
  111.         in.close();
  112.         out.close();
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.     return 0;
  119. }
  120.  
  121.  
  122. void calcReg(vector<double>& x, vector<double>& y, double & a, double& b) {
  123.    
  124.     int n = x.size();
  125.  
  126.     double xi = 0, xi2 = 0, yi = 0, xiyi = 0;  
  127.     for (int i = 0; i < n; i++) {
  128.         xi += x[i];
  129.         xi2 += x[i] * x[i];
  130.         yi += y[i];
  131.         xiyi += x[i] * y[i];
  132.     }
  133.    
  134.     double det = (n*xi2 - xi*xi);
  135.  
  136.     a = yi*xi2 - xi*xiyi;
  137.     b = n*xiyi - xi*yi;
  138.  
  139.     a = a / det;
  140.     b = b / det;
  141.    
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement