UnivTools.cc
Go to the documentation of this file.
1 #include <src/UnivTools.h>
2 #include <src/Functions.h>
3 
4 
5 namespace un2 {
6 
7  // https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data
8  std::vector<std::vector<double>>
9  FindPeaks(const std::vector<double>& input, const double totalThreshold)
10  {
11  // lag = n time bins, over which the local average will be calculated to detect peaks
12  const unsigned int lag = 5;
13  // threshold in standard deviations from local average to detect peaks
14  const double threshold = 2.5;
15  // between 0 and 1, where 1 is normal influence, 0.5 is half
16  const double influence = 0.25;
17 
18  if (input.size() <= lag + 2)
19  return std::vector<std::vector<double>>();
20 
21  // Initialise variables
22  std::vector<std::vector<double>> peaks;
23  std::vector<double> filteredY(input.size());
24  std::vector<double> avgFilter(input.size());
25  std::vector<double> stdFilter(input.size());
26  std::vector<double> subVecStart(input.begin(), input.begin() + lag);
27  const double mean = Mean(subVecStart);
28  avgFilter[lag] = mean;
29  stdFilter[lag] = StandardDeviation(subVecStart, mean);
30 
31  for (size_t i = lag + 1, n = input.size(); i < n; ++i) {
32  // if current value is 3.5 standard deviations away from the mean of the last 5 time bins
33  // and above the total signal threshold, check further
34  const auto& inp = input[i];
35  if (std::abs(inp - avgFilter[i - 1]) > threshold * stdFilter[i - 1] && inp > totalThreshold) {
36  // positive peak is detected if current value is above absolutely larger than the average
37  // of the last 5 time bins and if there is no further upwards trend
38  if (inp > avgFilter[i - 1] && inp > input[i - 1] && inp > input[i + 1]) {
39  peaks.push_back({ double(i), input[i] }); // Positive signal
40  } else {
41  // negative peaks could be stored here
42  // peaks.push_back({i*1., input[i]}); // Negative signal
43  }
44  // Make influence lower
45  // larger influence (closer to one) reduces the likelihood of peak detection based
46  // when there were many peaks in the current area
47  filteredY[i] = influence * inp + (1 - influence) * filteredY[i - 1];
48  } else
49  filteredY[i] = inp;
50 
51  //Adjust the filters
52  const std::vector<double> subVec(filteredY.begin() + (i - lag), filteredY.begin() + i);
53  const double mean = Mean(subVec);
54  avgFilter[i] = mean;
55  stdFilter[i] = StandardDeviation(subVec, mean);
56  }
57 
58  return peaks;
59  }
60 
61 
62  double
63  MIPtoMIPpersqm(const double mIPval, const double theta)
64  {
65  return mIPval / (4 * cos(theta));
66  }
67 
68 
69  std::vector<double>
70  MIPtoMIPpersqm(const std::vector<double>& mIPvals, const double theta)
71  {
72  std::vector<double> returnVec;
73  for (const auto m : mIPvals)
74  returnVec.push_back(MIPtoMIPpersqm(m, theta));
75  return returnVec;
76  }
77 
78 
79  double
80  VEMtoVEMpersqm(const double VEMval, const double theta)
81  {
82  return VEMval / (10 * cos(theta) + (1.2 + 2*1.8) * sin(theta));
83  }
84 
85 
86  std::vector<double>
87  VEMtoVEMpersqm(const std::vector<double>& mIPvals, const double theta)
88  {
89  std::vector<double> returnVec;
90  for (const auto m : mIPvals)
91  returnVec.push_back(VEMtoVEMpersqm(m, theta));
92  return returnVec;
93  }
94 
95 }
double StandardDeviation(const std::vector< double > &v, const double mean)
Definition: Functions.cc:26
double VEMtoVEMpersqm(const double VEMval, const double theta)
Definition: UnivTools.cc:80
double abs(const SVector< n, T > &v)
double MIPtoMIPpersqm(const double mIPval, const double theta)
Definition: UnivTools.cc:63
double Mean(const std::vector< double > &v)
Definition: Functions.h:31
std::vector< std::vector< double > > FindPeaks(const std::vector< double > &input, const double totalThreshold)
Definition: UnivTools.cc:9
constexpr double m
Definition: AugerUnits.h:121

, generated on Tue Sep 26 2023.