Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <filesystem>
- #include <string>
- #include <strstream>
- #include <vector>
- using namespace std;
- namespace fs = std::experimental::filesystem;
- void calcReg(vector<double>&, vector<double>&, double &, double&);
- int main(int argc, char** argv) {
- //return 0;
- vector<double> I;
- vector<double> V;
- vector<double> x;
- vector<double> y;
- string path = ".";
- string avg = " averged.csv";
- ifstream in;
- ofstream out;
- double iTemp = 0, vTemp = 0, iTempOld = 0, vTempOld = 0, dTemp=0;
- bool first = true;
- int i = 0, j = 0;
- int n = 0;
- double a = 0, b = 0;
- int tail = 2000;
- int numToAvg = 12;
- int shift = numToAvg;
- std::cout << "How many do I average? >>";
- std::cin >> numToAvg;
- std::cout << "How much to shift the average? >>";
- std::cin >> shift;
- double threshold = .15;
- for (auto & p : fs::directory_iterator(path)) {
- if (fs::is_directory(p))
- continue;
- if (fs::path(p).extension() != ".csv")
- continue;
- first = false;
- in.open(p.path(), ios::in);
- std::cout << in.is_open() << "\t\tbegin reading data from " << p.path() << std::endl;
- out.open(path + "\\ average\\" + p.path().filename().string(), ios::out | ios::trunc);
- std::cout << out.is_open() << "\t\tout file dir " << path + "average\\" + p.path().filename().string() << std::endl;
- while (!in.eof()) {
- in >> iTemp; in.get();
- in >> vTemp; in.get();
- if (vTemp > 0) {
- I.push_back(iTemp);
- V.push_back(vTemp);
- }
- }
- /*
- //Calcualte lin reg of the tail end, then decide what the turning point is
- n = V.size();
- for (i = 0; i < n; i++) {
- if (V[i] > 1.2) {
- x.push_back(V[i]);
- y.push_back(I[i]);
- }
- }
- calcReg(x, y, a, b);
- for (int i = 0; i < n; i++) {
- vTempOld = a + b*V[i];
- vTemp = I[i] - vTempOld;
- iTemp = vTemp/vTempOld;
- out << I[i] << "," << V[i] << ", ," <<vTemp<< ","<< iTemp*100<<"," << vTempOld << endl;
- }
- */
- //-------------------------------------------------------------
- //Do some averaging to use with GNUPlot for final figures.
- for (i = 0; i < I.size() && j < I.size()-i; i+=shift) {
- iTemp = vTemp = 0;
- for (j = 0; j < numToAvg && j < I.size()-i; j += 1) {
- iTemp += I[i+j];
- vTemp += V[i+j];
- //std::cout<<i+j<<"\t" << I[i+j] << "\t" << V[i+j] << std::endl
- }
- if(i==0)
- out << vTemp / numToAvg << "," << iTemp / numToAvg << ","<< 0 << std::endl;
- else {
- dTemp = (iTempOld - iTemp) / (vTempOld - vTemp);
- if (abs(dTemp) > 1)
- dTemp = 0;
- out << vTemp / numToAvg << "," << iTemp / numToAvg << "," << dTemp << std::endl;
- }
- vTempOld = vTemp;
- iTempOld = iTemp;
- }//*/
- I.clear();
- V.clear();
- in.close();
- out.close();
- }
- return 0;
- }
- void calcReg(vector<double>& x, vector<double>& y, double & a, double& b) {
- int n = x.size();
- double xi = 0, xi2 = 0, yi = 0, xiyi = 0;
- for (int i = 0; i < n; i++) {
- xi += x[i];
- xi2 += x[i] * x[i];
- yi += y[i];
- xiyi += x[i] * y[i];
- }
- double det = (n*xi2 - xi*xi);
- a = yi*xi2 - xi*xiyi;
- b = n*xiyi - xi*yi;
- a = a / det;
- b = b / det;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement