diff --git a/main.cpp b/main.cpp index c004ed5..4e400bf 100644 --- a/main.cpp +++ b/main.cpp @@ -14,8 +14,8 @@ namespace plt = matplot; int MIN_ZERO_SEPARATION = 10; -vector findZeroCrossings(const std::vector& data) { - std::vector zeroCrossings; +vector findZeroCrossings(const vector& data) { + vector zeroCrossings; for (int i = 1; i < data.size(); ++i) { if (data[i - 1] * data[i] < 0 || data[i - 1] * data[i] == 0) { @@ -33,6 +33,65 @@ vector findZeroCrossings(const std::vector& data) { return zeroCrossings; } +pair poweredMax(const vector& data, int n) { + if (data.empty()) { + return {0.0, false}; // If vector is empty, return false + } + + unordered_map freqMap; + for (double value : data) { + freqMap[value]++; + } + + double maxVal = data[0]; // Initialize maxVal with the first element + int maxFreq = freqMap[maxVal]; + + for (const auto& entry : freqMap) { + if (entry.second >= n && entry.first > maxVal) { + maxVal = entry.first; + maxFreq = entry.second; + } + } + + bool found = maxFreq >= n; + + return {maxVal, found}; +} + +std::pair poweredMin(const std::vector& data, int n) { + if (data.empty()) { + return {0.0, false}; // If vector is empty, return false + } + + std::unordered_map freqMap; + for (double value : data) { + freqMap[value]++; + } + + double minVal = std::numeric_limits::max(); // Initialize minVal with maximum double value + int minFreq = freqMap[minVal]; + + for (const auto& entry : freqMap) { + if (entry.second >= n && entry.first < minVal) { + minVal = entry.first; + minFreq = entry.second; + } + } + + bool found = minFreq >= n; + + return {minVal, found}; +} + +std::vector slice(const std::vector& data, int start, int end) { + if (start < 0 || end >= static_cast(data.size()) || start > end) { + std::cerr << "Invalid slice indices." << std::endl; + return {}; + } + + return {data.begin() + start, data.begin() + end + 1}; +} + int main() { ifstream dataFile; @@ -88,6 +147,13 @@ int main() { plt::line(xData[zero], -2, xData[zero], 2); } + // Finding the powered max between 2 zeros + auto max = poweredMax(slice(yData, zeroCrossings[0], zeroCrossings[1]), 5); + auto min = poweredMin(slice(yData, zeroCrossings[1], zeroCrossings[2]), 5); + + cout << max.first << endl; + cout << min.first << endl; + // Show the plot plt::show();