SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <numeric>
  3. #include <stdio.h>
  4. #include <fstream>
  5. #include <string>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <armadillo>
  9. #include <iterator>
  10. #include <HeartClassModule.hpp>
  11.  
  12. // ------PRIVATE---------------
  13.  
  14. void HeartClassModule::runHeartClass() {
  15.     // tu wołacie swoje metody, robicie obliczenia i wpisujecie wynik do results
  16.  
  17.     results = {};
  18.  
  19.     // na koniec to
  20.     validateResults();
  21. }
  22.  
  23. std::vector<double> HeartClassModule::maxValue(std::vector<std::vector <double>> &signal)
  24. {
  25.     std::vector<double> maxes;
  26.     maxes.resize(signal.size());
  27.  
  28.     std::transform(std::cbegin(signal), std::cend(signal), std::begin(maxes), [](const std::vector<double> &sig)
  29.     {
  30.         return *(std::max_element(std::cbegin(sig), std::cend(sig)));
  31.     });
  32.     return maxes;
  33. }
  34.  
  35. std::vector<double> HeartClassModule::minValue(std::vector<std::vector <double>> &signal)
  36. {
  37.     std::vector<double> mins;
  38.     mins.resize(signal.size());
  39.    
  40.     std::transform(std::cbegin(signal),std::cend(signal),std::begin(mins), [](const std::vector<double> &sig)
  41.     {
  42.         return *(std::min_element(std::cbegin(sig), std::cend(sig)));
  43.     });
  44.  
  45.     return mins;
  46. }
  47.  
  48. std::vector<double> HeartClassModule::ptsAboveTh(std::vector<std::vector<double>> &signal)
  49. {
  50.     std::vector<double> ths;
  51.     ths.resize(signal.size());
  52.     auto maxes = maxValue(signal);
  53.  
  54.     std::transform(std::cbegin(maxes), std::cend(maxes), std::begin(ths), [](const double& max){
  55.         return 0.7*max;
  56.     });
  57.  
  58.     std::vector<std::vector <double>> ptsAbove;
  59.     ptsAbove.reserve(signal.size());
  60.  
  61.     std::transform(std::cbegin(signal),std::cend(signal),std::begin(ths),std::back_inserter(ptsAbove), [](const std::vector<double> &sig, const double &th)
  62.     {
  63.         std::vector<double> ptsA;
  64.         std::copy_if(std::cbegin(sig),std::cend(sig),std::back_inserter(ptsA),[&](const double& value)
  65.         {
  66.             return value > th;
  67.         });
  68.  
  69.         return ptsA.size();
  70.     } );
  71.  
  72.     return ptsAbove;
  73.    
  74. }
  75.  
  76. std::vector <double> HeartClassModule::qrsAuc(std::vector<std::vector<double>> &signal)
  77. {
  78.     std::vector<double> integrals;
  79.     integrals.resize(signal.size());
  80.  
  81.     std::transform(std::cbegin(signal), std::cend(signal), std::begin(integrals), [](const std::vector<double> &sig)
  82.     {
  83.         return std::accumulate(std::cbegin(sig), std::cend(sig), 0);
  84.     });
  85.     return integrals;
  86. }
  87.  
  88. std::vector <double> HeartClassModule::qrsPosRatio(std::vector<std::vector<double>> &signal)
  89. {
  90.     std::vector<double> posRatios;
  91.     posRatios.resize(signal.size());
  92.  
  93.     std::transform(std::cbegin(signal), std::cend(signal), std::begin(posRatios), [](const std::vector<double> &sig)
  94.     {
  95.         auto integral = std::accumulate(std::cbegin(sig), std::cend(sig), 0);
  96.         auto posIntegral = std::accumulate(std::cbegin(sig), std::cend(sig), 0, [](double acc,double val)
  97.         {
  98.             return val > 0 ? acc+val : acc;
  99.         });
  100.         float ratio = static_cast<float>(posIntegral)/integral;
  101.  
  102.         return ratio;
  103.     });
  104.     return posRatios;
  105. }
  106.  
  107. std::vector <double> HeartClassModule::qrsNegRatio(std::vector<std::vector<double>> &signal)
  108. {
  109.     std::vector<double> negRatios;
  110.     negRatios.resize(signal.size());
  111.  
  112.     std::transform(std::cbegin(signal), std::cend(signal), std::begin(negRatios), [](const std::vector<double> &sig)
  113.     {
  114.         auto integral = std::accumulate(std::cbegin(sig), std::cend(sig), 0);
  115.         auto negIntegral = std::accumulate(std::cbegin(sig), std::cend(sig), 0, [](double acc,double val)
  116.         {
  117.             return val<0 ? acc+val : acc;
  118.         });
  119.         float ratio = static_cast<float>(negIntegral)/integral;
  120.  
  121.         return ratio;
  122.     });
  123.     return negRatios;
  124. }
  125. std::vector <int> HeartClassModule::aucComparsion(std::vector<std::vector<double>> &signal)
  126. {
  127.     auto posRatios = qrsPosRatio(signal); //vector<float>
  128.     auto negRatios = qrsNegRatio(signal); //vector<float>
  129.     std::vector<double> comparsion(posRatios.size());
  130.  
  131.     std::transform(std::cbegin(posRatios), std::cend(posRatios),std::cbegin(negRatios), std::begin(comparsion),
  132.         [](const int& posRatio, const int& negRatio){
  133.         return posRatio >= negRatio ? 1 : 0;
  134.     });
  135.  
  136.      std::vector<int> comparsion1(comparsion.begin(), comparsion.end());
  137.  
  138.      return comparsion1;
  139. }
  140. std::vector<double> HeartClassModule::kurtosis(std::vector<std::vector<double>> &signal)
  141. {
  142.     std::vector <double> kurtosisVec;
  143.     double sum,n;
  144.     double var, S, kurtosis,avg;
  145.     for (int i = 0; i < signal.size(); i++)
  146.      {
  147.  
  148.          n = signal[i].size();
  149.          sum = accumulate(signal[i].begin(), signal[i].end(), 0);  
  150.          avg = (double)sum/n;
  151.          
  152.          for (int j = 0; j < n ; j++)
  153.           {
  154.              var += (signal[i][j] - avg)*(signal[i][j] - avg);
  155.                
  156.           }  
  157.           var = (double)(var)/(n - 1);  
  158.           S = (double)sqrt(var);
  159.          for (int j = 0; j < n; j++)
  160.           {
  161.             kurtosis += (signal[i][j] - avg)*(signal[i][j] - avg)*(signal[i][j] - avg)*(signal[i][j] - avg);
  162.           }
  163.          kurtosis = kurtosis/(n*S*S*S*S);
  164.          kurtosis -= 3;
  165.    
  166.         kurtosisVec.push_back(kurtosis);
  167.      }
  168.    
  169.     return kurtosisVec;
  170.  
  171. }
  172.  
  173. std::vector<double> HeartClassModule::skewness(std::vector<std::vector<double>> &signal)
  174. {
  175.     std::vector <double> skewnessVec;
  176.     double sum,n;
  177.     double var,skewness, S,avg;
  178.     for (int i = 0; i < signal.size(); i++)
  179.      {
  180.  
  181.          n = signal[i].size();
  182.          sum = accumulate(signal[i].begin(), signal[i].end(), 0);  
  183.          avg = (double)sum/n;
  184.          
  185.          for (int j = 0; j < n ; j++)
  186.           {
  187.              var += (signal[i][j] - avg)*(signal[i][j] - avg);
  188.                
  189.           }  
  190.           var = (double)(var)/(n - 1);  
  191.           S = (double)sqrt(var);
  192.          for (int j = 0; j < n; j++)
  193.           {
  194.             skewness += (signal[i][j] - avg)*(signal[i][j] - avg)*(signal[i][j] - avg);
  195.           }
  196.         skewness = skewness/(n * S * S * S);
  197.  
  198.         skewnessVec.push_back(skewness);
  199.          
  200.      }
  201.    
  202.     return skewnessVec;
  203. }
  204.  
  205. std::vector<std::vector<double>> HeartClassModule::prepareSignal(std::vector<double> &rawSignal, std::vector<int> &rPeaks, int &samplingFrequency)
  206. {
  207.     int frames;
  208.     std::vector<std::vector<double>> prepared;
  209.     frames = 0.2*samplingFrequency;
  210.  
  211.     for (int i=0; i<rPeaks.size(); i++)
  212.         {
  213.             std::vector <double> slice((rawSignal.data())[rPeaks[i]-frames],(rawSignal.data())[rPeaks[i]-frames]);
  214.             prepared[i] = slice;
  215.         }
  216.  
  217.  
  218.     return prepared;
  219. }
  220.  
  221.  
  222. // ------------------------PUBLIC------------------------------------
  223. HeartClassModule::HeartClassModule(
  224.     EcgBaselineModuleBase& ecgBaselineModule,
  225.     WavesModuleBase& wavesModule)
  226.     :  ecgBaselineModule{ecgBaselineModule}, wavesModule{wavesModule}
  227.     {
  228.         this->samplingFrequency = sF;
  229.         this->signal = prepareSignal(rawSignal, rPeaks, samplingFrequency);
  230.     }
  231.  
  232. HeartClassData HeartClassModule::getResults(){
  233.     if (!resultsValid()) {
  234.         runHeartClass();
  235.     }
  236.  
  237.     return results;
  238. }
  239.  
  240. void HeartClassModule::setFeatures(){
  241.     this->signalFeatures.maxValue = maxValue(signal);
  242.     this->signalFeatures.minValue = minValue(signal);
  243.     this->signalFeatures.ptsAboveTh = ptsAboveTh(signal);
  244.     this->signalFeatures.qrsAuc = qrsAuc(signal);
  245.     this->signalFeatures.qrsPosRatio = qrsPosRatio(signal);
  246.     this->signalFeatures.qrsNegRatio = qrsNegRatio(signal);
  247.     this->signalFeatures.aucComparsion = aucComparsion(signal);
  248.     this->signalFeatures.kurtosis = kurtosis(signal);
  249.     this->signalFeatures.skewness = skewness(signal);
  250. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top