# Recompute Cal Factors

The ReCal allows you to compute corrected slope and y-intercept values (M and b) for the "Y = M * X + b" equation of a straight line, where X is the Analog to Digital reading and Y is the real-world engineering units.

What does this all mean? Well, we are all familiar with the Digital Multi-Meters (DMMs) that we use to measure voltages. These use Analog-to-Digital Converters (A/Ds or ADCs) to convert the voltage into a unitless integer value. This value must then be converted into the displayed voltage using the formula displayed above. Many systems, such as the Rabbit Semiconductor BL2100 Smartcat processor utilize ADCs to sense external voltages to read the values of a wide array of external transducers.

You can use the Cals Utility to compute the original calibration values to match the manufacturer's published specification to get a good ballpark reading. Then, using ReCal, you can correct the readings to match observed values. Try to choose values at the low (10%) and high (90%) ends of full scale to compute the calibration values. Stay slightly away from the low and high end values since you can accidentally drift into the rails and not really know what the readings are.

## ReCal.cpp

/************************************************************************************************
*                                                                                               *
*  Copyright (c) 2008 - 2011 David Cass Tyler, PO Box 1026, Willard, NM 87063, (505) 384-5342   *
*                                                                                               *
*  ReCal recomputes and corrects the slope and y-intercept calibration factors based on         *
*  observed inputs.  This allows you to initially load the calibration factors computed from    *
*  the manufacturer-s released specifications, to get them reasonably close, and then run the   *
*  system, observe the correct and the displayed values and use them to recompute the correct   *
*  calibration factors.                                                                         *
*                                                                                               *
*  To compile from the command line:  "CL ReCal.cpp"                                            *
*                                                                                               *
************************************************************************************************/

#include <STDIO.H>
#include <STDLIB.H>

// Y = M * X + b and X = ( Y - b ) / M

// Where:

// M = ( Yhi - Ylo ) / ( Xhi - Xlo )
// b = Ylo - ( M * Xlo )

// If    Y1 = M1 * X1 + b1
// and   Y2 = M2 * X2 + b2
// then  Y2 = M3 * X1 + b3

// Proof:

// Y2 = M2 * ( M1 * X1 + b1 ) + b2
// Y2 = M2 * M1 * X1 + M2 * b1 + b2

// Y2 = ( M2 * M1 ) * X1 + ( M2 * b1 + b2 )

// Thus:

// M3 = M2 * M1
// b3 = M2 * b1 + b2

int main ( int argc, char * argv [], char * /* envp */ [] )

{

// Assure the Parameters Were Passed

if ( argc != 7 )
{

// Print Instructions

fprintf (

stdout,

"ReCal 1.0 (C) 2008 - 2011, David Cass Tyler, P.O. Box 1026, Willard, NM 87063\n"
"\n"
"ReCal recomputes calibration factors based on observed inputs\n"
"\n"
"Usage:  ReCal <M1> <b1> <X1> <Y1> <X2> <Y2>\n"
"\n"
"Where:\n"
"\n"
"M1 is the original slope (M)\n"
"b1 is the original intercept (b)\n"
"X1 is the first reported value\n"
"Y1 is the actual callibrated value corresponding to X1\n"
"X2 is the second reported value\n"
"Y2 is the actual callibrated value corresponding to X2\n"
"\n"
"Example:\n"
"\n"
"ReCal 26.1955 -119.815 0.0 0.0 39.3 38.8\n"
"M3 = 25.8622, b3 = -118.291\n"
"\n"
"Where:\n"
"\n"
"M3 is the corrected slope (M)\n"
"b3 is the corrected intercept (b)\n"
"\n"
"The instrument will display calibrated values using the corrected cal factors\n"

);

return 1;

}

// Original Slope and Intercept

double M1 = atof ( argv [ 1 ] );
double b1 = atof ( argv [ 2 ] );

// Observed Values Using the Original Cal Factors

double Xlo = atof ( argv [ 3 ] ), Ylo = atof ( argv [ 4 ] );
double Xhi = atof ( argv [ 5 ] ), Yhi = atof ( argv [ 6 ] );

// Compute the Corrective Cal Factors

double M2 = ( Yhi - Ylo ) / ( Xhi - Xlo );
double b2 = Ylo - ( M2 * Xlo );

// Compute the Corrected Cal Factors

double M3 = M1 * M2;
double b3 = M2 * b1 + b2;

// Print the Corrected Cal Factors

fprintf ( stdout, "M3 = %g, b3 = %g\n", M3, b3 );

// Return Success

return 0;

}  // int main ( int argc, char * argv[], char * envp[] )