#ifndef UTILH_H #define UTILH_H #include #include #include #include #include namespace Util { template T Scale(T value, T from_low, T from_high, T to_low, T to_high) { value -= from_low; value *= (to_high - to_low) / (from_high - from_low); value += to_low; return value; } static inline double SparamTodB(double d) { return 20*log10(d); } static inline double SparamTodB(std::complex d) { return SparamTodB(abs(d)); } static inline double SparamToDegree(std::complex 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::quiet_NaN(); } } static inline double SparamToVSWR(std::complex d) { return SparamToVSWR(abs(d)); } static inline std::complex SparamToImpedance(std::complex 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 d) { return SparamToImpedance(d).real(); } static inline double SparamToCapacitance(std::complex d, double freq) { return -1.0 / (SparamToImpedance(d).imag() * 2.0 * M_PI * freq); } static inline double SparamToInductance(std::complex d, double freq) { return SparamToImpedance(d).imag() / (2.0 * M_PI * freq); } static inline double SparamToQualityFactor(std::complex d) { return abs(d.imag()) / d.real(); } // 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; } void unwrapPhase(std::vector &phase); // input values are Y coordinates, assumes evenly spaced linear X values from 0 to input.size() - 1 void linearRegression(const std::vector &input, double &B_0, double &B_1); } #endif // UTILH_H