2022-08-30 02:07:07 +08:00
# ifndef CALIBRATION2_H
# define CALIBRATION2_H
2020-08-31 04:03:41 +08:00
2022-08-30 02:07:07 +08:00
# include "savable.h"
# include "calibrationmeasurement.h"
2021-10-21 19:00:34 +08:00
# include "calkit.h"
2022-08-30 02:07:07 +08:00
# include "Traces/trace.h"
2021-10-21 19:00:34 +08:00
2022-08-30 02:07:07 +08:00
class Calibration : public QObject , public Savable
2020-08-31 04:03:41 +08:00
{
2022-08-30 02:07:07 +08:00
Q_OBJECT
2022-09-12 05:22:11 +08:00
friend class LibreCALDialog ;
2020-08-31 04:03:41 +08:00
public :
Calibration ( ) ;
2022-08-30 02:07:07 +08:00
enum class Type {
None ,
SOLT ,
2021-04-16 01:24:11 +08:00
Last ,
2020-08-31 04:03:41 +08:00
} ;
2022-08-30 02:07:07 +08:00
class CalType {
public :
Type type ;
2022-09-12 05:22:11 +08:00
std : : vector < int > usedPorts ; // port count starts at 1
2022-08-30 02:07:07 +08:00
QString getReadableDescription ( ) ;
QString getShortString ( ) ;
static CalType fromShortString ( QString s ) ;
2021-09-03 19:13:33 +08:00
2022-08-30 02:07:07 +08:00
friend bool operator = = ( const CalType & lhs , const CalType & rhs ) ;
2021-09-03 19:13:33 +08:00
} ;
2022-08-30 02:07:07 +08:00
static QString TypeToString ( Type type ) ;
static Type TypeFromString ( QString s ) ;
2021-09-03 19:13:33 +08:00
2022-08-30 02:07:07 +08:00
// Applies calculated calibration coefficients to measurement data
void correctMeasurement ( VirtualDevice : : VNAMeasurement & d ) ;
2020-08-31 04:03:41 +08:00
2022-08-30 02:07:07 +08:00
// Starts the calibration edit dialog, allowing the user to make/delete measurements
void edit ( ) ;
2020-08-31 04:03:41 +08:00
2022-08-30 02:07:07 +08:00
Calkit & getKit ( ) ;
2020-08-31 04:03:41 +08:00
2022-08-30 02:07:07 +08:00
virtual nlohmann : : json toJSON ( ) override ;
virtual void fromJSON ( nlohmann : : json j ) override ;
2020-08-31 04:03:41 +08:00
2022-08-30 02:07:07 +08:00
bool toFile ( QString filename = QString ( ) ) ;
bool fromFile ( QString filename = QString ( ) ) ;
// Returns all possible calibration types/port permutations for the currently connected device.
// If no device is connected, a two-port device is assumed
static std : : vector < CalType > getAvailableCalibrations ( ) ;
// Returns vector of all calibration types (without 'Last')
static std : : vector < Type > getTypes ( ) ;
// Checks whether all measurements for a specific calibration are available.
// If pointer to the frequency/points variables are given, the start/stop frequency and number of points the calibration will have after the calculation is stored there
bool canCompute ( CalType type , double * startFreq = nullptr , double * stopFreq = nullptr , int * points = nullptr ) ;
// Resets the calibration (deletes all measurements and calculated coefficients)
void reset ( ) ;
// Returns the minimum number of ports for a given calibration type.
// E.g. the SOL(T) calibration can work with only one port, a through normalization requires at least two
static int minimumPorts ( Type type ) ;
// Adds a new measurement point (data) to all calibration measurements (m)
void addMeasurements ( std : : set < CalibrationMeasurement : : Base * > m , const VirtualDevice : : VNAMeasurement & data ) ;
// Deletes all datapoints in the calibration measurements (m)
void clearMeasurements ( std : : set < CalibrationMeasurement : : Base * > m ) ;
CalType getCaltype ( ) const ;
2020-08-31 04:03:41 +08:00
enum class InterpolationType {
Unchanged , // Nothing has changed, settings and calibration points match
Exact , // Every frequency point in settings has an exact calibration point (but there are more calibration points outside of the sweep)
Interpolate , // Every point in the sweep can be interpolated between two calibration points
Extrapolate , // At least one point in sweep is outside of the calibration and has to be extrapolated
NoCalibration , // No calibration available
} ;
2022-08-30 02:07:07 +08:00
InterpolationType getInterpolation ( double f_start , double f_stop , int npoints ) ;
2020-08-31 04:03:41 +08:00
std : : vector < Trace * > getErrorTermTraces ( ) ;
2020-11-28 01:18:31 +08:00
std : : vector < Trace * > getMeasurementTraces ( ) ;
2020-08-31 04:03:41 +08:00
2021-12-02 06:21:13 +08:00
QString getCurrentCalibrationFile ( ) ;
double getMinFreq ( ) ;
double getMaxFreq ( ) ;
int getNumPoints ( ) ;
2022-08-30 02:07:07 +08:00
public slots :
// Call once all datapoints of the current span have been added
void measurementsComplete ( ) ;
// Attempts to calculate the calibration coefficients. If not enough measurements are available, false is returned and the currently used coefficients are not changed
bool compute ( CalType type ) ;
// Deactivates the calibration, resets the calibration coefficients. Calibration measurements are NOT deleted.
void deactivate ( ) ;
signals :
// emitted when the measurement of a set of calibration measurements should be started
void startMeasurements ( std : : set < CalibrationMeasurement : : Base * > m ) ;
// emitted whenever a measurement is complete (triggered by calling measurementsComplete())
void measurementsUpdated ( ) ;
// emitted when calibration coefficients were calculated/updated successfully
void activated ( CalType type ) ;
// emitted when the calibrationo coefficients were reset
void deactivated ( ) ;
2020-08-31 04:03:41 +08:00
private :
2022-08-30 02:07:07 +08:00
enum class DefaultMeasurements {
SOL1Port ,
SOLT2Port ,
SOLT3Port ,
SOLT4Port ,
Last
2020-08-31 04:03:41 +08:00
} ;
2022-08-30 02:07:07 +08:00
static QString DefaultMeasurementsToString ( DefaultMeasurements dm ) ;
void createDefaultMeasurements ( DefaultMeasurements dm ) ;
bool hasFrequencyOverlap ( std : : vector < CalibrationMeasurement : : Base * > m , double * startFreq = nullptr , double * stopFreq = nullptr , int * points = nullptr ) ;
CalibrationMeasurement : : Base * findMeasurement ( CalibrationMeasurement : : Base : : Type type , int port1 = 0 , int port2 = 0 ) ;
CalibrationMeasurement : : Base * newMeasurement ( CalibrationMeasurement : : Base : : Type type ) ;
class Point {
2020-08-31 04:03:41 +08:00
public :
2022-08-30 02:07:07 +08:00
double frequency ;
std : : vector < std : : complex < double > > D ; // Directivity
std : : vector < std : : complex < double > > R ; // Source Match
std : : vector < std : : complex < double > > S ; // Reflection tracking
std : : vector < std : : vector < std : : complex < double > > > L ; // Receiver Match
std : : vector < std : : vector < std : : complex < double > > > T ; // Transmission tracking
std : : vector < std : : vector < std : : complex < double > > > I ; // Transmission isolation
Point interpolate ( const Point & to , double alpha ) ;
2020-08-31 04:03:41 +08:00
} ;
std : : vector < Point > points ;
2022-08-30 02:07:07 +08:00
Point computeSOLT ( double f ) ;
std : : vector < CalibrationMeasurement : : Base * > measurements ;
2020-08-31 04:03:41 +08:00
Calkit kit ;
2022-08-30 02:07:07 +08:00
CalType caltype ;
2020-12-07 09:44:25 +08:00
QString descriptiveCalName ( ) ;
2020-12-01 01:16:33 +08:00
QString currentCalFile ;
2020-08-31 04:03:41 +08:00
} ;
2022-08-30 02:07:07 +08:00
# endif // CALIBRATION2_H