SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <numeric>
  3. #include <fstream>
  4. #include <iterator>
  5. #include <algorithm>
  6.  
  7. template<typename T>
  8. class FindPeaks{
  9. private:
  10.     std::vector<T> m_input_signal;                      // stores input vector
  11.     std::vector<T> m_array_peak_positive;               // stores array consisting of  positive peaks
  12.     std::vector<T> m_array_peak_negative;               // stores array consisting of  negative peaks
  13.    
  14. public:
  15.     FindPeaks(const std::vector<T>& t_input_signal): m_input_signal{t_input_signal}{ }
  16.    
  17.     void estimate()
  18.     {
  19.         int lag{5};
  20.         T threshold{ 5 };                                                                                       // set a threshold
  21.         T influence{ 0.5 };                                                                                    // value between 0 to 1, 1 is normal influence and 0.5 is half the influence
  22.  
  23.         std::vector<T> filtered_signal(m_input_signal.size(), 0.0);                                             // placeholdered for smooth signal, initialie with all zeros
  24.         std::vector<int> signal(m_input_signal.size(), 0);                                                          // vector that stores where the negative and positive located
  25.         std::vector<T> avg_filtered(m_input_signal.size(), 0.0);                                                // moving averages
  26.         std::vector<T> std_filtered(m_input_signal.size(), 0.0);                                                // moving standard deviation
  27.  
  28.         avg_filtered[lag] = findMean(m_input_signal.begin(), m_input_signal.begin() + lag);                         // pass the iteartor to vector
  29.         std_filtered[lag] = findStandardDeviation(m_input_signal.begin(), m_input_signal.begin() + lag);
  30.  
  31.         for (size_t iLag = lag + 1; iLag < m_input_signal.size(); ++iLag) {                                         // start index frm
  32.             if (std::abs(m_input_signal[iLag] - avg_filtered[iLag - 1]) > threshold * std_filtered[iLag - 1]) {     // check if value is above threhold            
  33.                 if ((m_input_signal[iLag]) > avg_filtered[iLag - 1]) {
  34.                     signal[iLag] = 1;                                                                               // assign positive signal
  35.                 }
  36.                 else {
  37.                     signal[iLag] = -1;                                                                                  // assign negative signal
  38.                 }
  39.                 filtered_signal[iLag] = influence * m_input_signal[iLag] + (1 - influence) * filtered_signal[iLag - 1];        // exponential smoothing
  40.             }
  41.             else {
  42.                 signal[iLag] = 0;                                                                                         // no signal
  43.                 filtered_signal[iLag] = m_input_signal[iLag];
  44.             }
  45.  
  46.             avg_filtered[iLag] = findMean(filtered_signal.begin() + (iLag - lag), filtered_signal.begin() + iLag);
  47.             std_filtered[iLag] = findStandardDeviation(filtered_signal.begin() + (iLag - lag), filtered_signal.begin() + iLag);
  48.  
  49.         }
  50.  
  51.         for (size_t iSignal = 0; iSignal < m_input_signal.size(); ++iSignal) {
  52.             if (signal[iSignal] == 1) {
  53.                 m_array_peak_positive.emplace_back(m_input_signal[iSignal]);                                        // store the positive peaks
  54.             }
  55.             else if (signal[iSignal] == -1) {
  56.                 m_array_peak_negative.emplace_back(m_input_signal[iSignal]);                                         // store the negative peaks
  57.             }
  58.         }
  59.         printVoltagePeaks(signal, m_input_signal);
  60.        
  61.     }
  62.  
  63.     std::pair< std::vector<T>, std::vector<T> > get_peaks()
  64.     {
  65.         return std::make_pair(m_array_peak_negative, m_array_peak_negative);
  66.     }
  67.  
  68. };
  69.  
  70.  
  71. template<typename T1, typename T2 >
  72. void printVoltagePeaks(std::vector<T1>& m_signal, std::vector<T2>& m_input_signal) {
  73.     std::ofstream output_file("./voltage_peak.csv");
  74.     std::ostream_iterator<T2> output_iterator_voltage(output_file, ",");
  75.     std::ostream_iterator<T1> output_iterator_signal(output_file, ",");
  76.     std::copy(m_input_signal.begin(), m_input_signal.end(), output_iterator_voltage);
  77.     output_file << "\n";
  78.     std::copy(m_signal.begin(), m_signal.end(), output_iterator_signal);
  79. }
  80.  
  81. template<typename iterator_type>
  82. typename std::iterator_traits<iterator_type>::value_type findMean(iterator_type it, iterator_type end)
  83. {
  84.     /* function that receives iterator to*/
  85.     typename std::iterator_traits<iterator_type>::value_type sum{ 0.0 };
  86.     int counter = 0;
  87.     while (it != end) {
  88.         sum += *(it++);
  89.         counter++;
  90.     }
  91.     return sum / counter;
  92. }
  93.  
  94. template<typename iterator_type>
  95. typename std::iterator_traits<iterator_type>::value_type findStandardDeviation(iterator_type it, iterator_type end)
  96. {
  97.     auto mean = findMean(it, end);
  98.     typename std::iterator_traits<iterator_type>::value_type sum_squared_error{ 0.0 };
  99.     int counter{ 0 };
  100.     while (it != end) {
  101.         sum_squared_error += std::pow((*(it++) - mean), 2);
  102.         counter++;
  103.     }
  104.     auto standard_deviation = std::sqrt(sum_squared_error / (counter - 1));
  105.     return standard_deviation;
  106. }
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