2020-11-22 07:41:42 +08:00
|
|
|
#ifndef UTILH_H
|
|
|
|
#define UTILH_H
|
|
|
|
|
2021-06-28 05:40:50 +08:00
|
|
|
#include <complex>
|
|
|
|
#include <math.h>
|
|
|
|
#include <limits>
|
2021-12-11 06:36:28 +08:00
|
|
|
#include <vector>
|
2021-06-28 05:40:50 +08:00
|
|
|
|
2021-10-26 02:56:27 +08:00
|
|
|
#include <QColor>
|
|
|
|
|
2020-11-22 07:41:42 +08:00
|
|
|
namespace Util {
|
2022-01-05 21:59:25 +08:00
|
|
|
template<typename T> T Scale(T value, T from_low, T from_high, T to_low, T to_high, bool log_from = false, bool log_to = false) {
|
|
|
|
double normalized;
|
|
|
|
if(log_from) {
|
|
|
|
normalized = log10(value / from_low) / log10(from_high / from_low);
|
|
|
|
} else {
|
|
|
|
normalized = (value - from_low) / (from_high - from_low);
|
|
|
|
}
|
|
|
|
if(log_to) {
|
|
|
|
value = to_low * pow(10.0, normalized * log10(to_high / to_low));
|
|
|
|
} else {
|
|
|
|
value = normalized * (to_high - to_low) + to_low;
|
|
|
|
}
|
2020-11-22 07:41:42 +08:00
|
|
|
return value;
|
|
|
|
}
|
2021-06-28 05:40:50 +08:00
|
|
|
|
|
|
|
static inline double SparamTodB(double d) {
|
|
|
|
return 20*log10(d);
|
|
|
|
}
|
|
|
|
static inline double SparamTodB(std::complex<double> d) {
|
|
|
|
return SparamTodB(abs(d));
|
|
|
|
}
|
|
|
|
static inline double SparamToDegree(std::complex<double> d) {
|
|
|
|
return (arg(d) * 180.0 / M_PI);
|
|
|
|
}
|
|
|
|
static inline double SparamToVSWR(double d) {
|
|
|
|
if(abs(d) < 1.0) {
|
|
|
|
return (1+abs(d)) / (1-abs(d));
|
|
|
|
} else {
|
|
|
|
return std::numeric_limits<double>::quiet_NaN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static inline double SparamToVSWR(std::complex<double> d) {
|
|
|
|
return SparamToVSWR(abs(d));
|
|
|
|
}
|
|
|
|
static inline std::complex<double> SparamToImpedance(std::complex<double> d) {
|
|
|
|
return 50.0 * (1.0 + d) / (1.0 - d);
|
|
|
|
}
|
|
|
|
// all these conversions assume series connection of real and imag part
|
|
|
|
static inline double SparamToResistance(std::complex<double> d) {
|
|
|
|
return SparamToImpedance(d).real();
|
|
|
|
}
|
|
|
|
static inline double SparamToCapacitance(std::complex<double> d, double freq) {
|
|
|
|
return -1.0 / (SparamToImpedance(d).imag() * 2.0 * M_PI * freq);
|
|
|
|
}
|
|
|
|
static inline double SparamToInductance(std::complex<double> d, double freq) {
|
|
|
|
return SparamToImpedance(d).imag() / (2.0 * M_PI * freq);
|
|
|
|
}
|
|
|
|
static inline double SparamToQualityFactor(std::complex<double> d) {
|
|
|
|
return abs(d.imag()) / d.real();
|
|
|
|
}
|
2021-10-26 02:56:27 +08:00
|
|
|
// attempts to return a font color with good contrast against the given background color
|
|
|
|
static inline QColor getFontColorFromBackground(QColor q) {
|
|
|
|
auto brightness = q.redF() * 0.299 + q.greenF() * 0.587 + q.blueF() * 0.114;
|
|
|
|
return brightness > 0.6 ? Qt::black : Qt::white;
|
|
|
|
}
|
2021-12-11 06:36:28 +08:00
|
|
|
|
|
|
|
void unwrapPhase(std::vector<double> &phase);
|
|
|
|
|
|
|
|
// input values are Y coordinates, assumes evenly spaced linear X values from 0 to input.size() - 1
|
|
|
|
void linearRegression(const std::vector<double> &input, double &B_0, double &B_1);
|
2020-11-22 07:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // UTILH_H
|