Q3D-Calibration/include/utils.h

97 lines
2.1 KiB
C++

#pragma once
#ifndef utils_h
#define utils_h
#include <Eigen/Dense>
#include <iostream>
#define DEBUG 0
#define INF 1e9
#define CHANNEL_NUMBER 4096
using namespace std;
using std::string;
using std::to_string;
double dataMax(std::vector<double> data) {
double x, m = -INF;
for (int i = 0; i < data.size(); i++) {
x = data.at(i);
m = std::max(m, x);
}
return m;
}
double dataAvg(std::vector<double> data) {
double m = 0;
for (int i = 0; i < data.size(); i++) m += data.at(i);
return m / data.size();
}
double dataStd(std::vector<double> data) {
double m = 0;
double mu = dataAvg(data);
for (int i = 0; i < data.size(); i++) m += std::pow(data.at(i) - mu, 2);
return m / (data.size() - 1);
}
double dataMax2D(std::vector<Eigen::Vector2d> data) {
double m = -INF;
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
m = std::max(m, point(1));
}
return m;
}
double dataAvg2D(std::vector<Eigen::Vector2d> data) {
int n = 0;
double m = 0;
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
n += point(1);
m += point(0) * point(1);
}
return m / n;
}
double dataStd2D(std::vector<Eigen::Vector2d> data) {
int n = 0;
double m = 0;
double mu = dataAvg2D(data);
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
n += point(1);
m += std::pow(point(0) - mu, 2) * point(1);
}
return std::sqrt(m / (n - 1));
}
double dataMax2DInd(std::vector<Eigen::Vector2d> data) {
double x = 0, m = -INF;
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
if (point(1) > m) {
x = point(0);
m = point(1);
}
}
return x;
}
double dataStd2DSQRT(std::vector<Eigen::Vector2d> data) { return std::sqrt(dataMax2D(data)); }
string rmString(string str, string substr) {
int pos;
int len = substr.length();
while (true) {
pos = str.find(substr);
if (pos < 0) break;
str.erase(pos, len);
}
return str;
}
#endif