2001-03-04 12:54:12 +00:00
|
|
|
/*
|
2001-07-13 19:08:15 +00:00
|
|
|
* Hamlib Interface - calibration routines
|
2009-06-23 20:30:22 +00:00
|
|
|
* Copyright (c) 2000-2009 by Stephane Fillod
|
|
|
|
* Copyright (c) 2000-2003 by Frank Singleton
|
2001-03-04 12:54:12 +00:00
|
|
|
*
|
|
|
|
*
|
2011-08-22 01:07:57 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-03-04 12:54:12 +00:00
|
|
|
*
|
2011-08-22 01:07:57 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-07-13 19:08:15 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-08-22 01:07:57 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-03-04 12:54:12 +00:00
|
|
|
*
|
2011-08-22 01:07:57 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2001-07-13 19:08:15 +00:00
|
|
|
* License along with this library; if not, write to the Free Software
|
2011-08-22 01:07:57 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-03-04 12:54:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rig_internal
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file cal.c
|
|
|
|
* \brief Calibration routines
|
|
|
|
*/
|
|
|
|
|
2001-07-13 19:08:15 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2017-10-05 02:32:08 +00:00
|
|
|
# include "config.h"
|
2001-07-13 19:08:15 +00:00
|
|
|
#endif
|
|
|
|
|
2001-06-20 06:08:22 +00:00
|
|
|
#include <hamlib/rig.h>
|
2001-03-04 12:54:12 +00:00
|
|
|
#include "cal.h"
|
|
|
|
|
|
|
|
/* add rig_set_cal(cal_table), rig_get_calstat(rawmin,rawmax,cal_table), */
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
2019-05-30 06:37:49 +00:00
|
|
|
* \brief Convert raw data to a calibrated integer value, according to table
|
2006-10-15 00:27:52 +00:00
|
|
|
* \param rawval input value
|
|
|
|
* \param cal calibration table
|
2019-05-30 06:37:49 +00:00
|
|
|
* \return calibrated integer value
|
2006-10-15 00:27:52 +00:00
|
|
|
|
2001-03-04 12:54:12 +00:00
|
|
|
* cal_table_t is a data type suited to hold linear calibration
|
|
|
|
* cal_table_t.size tell the number of plot cal_table_t.table contains
|
2011-08-22 01:07:57 +00:00
|
|
|
* If a value is below or equal to cal_table_t.table[0].raw,
|
2001-03-04 12:54:12 +00:00
|
|
|
* rig_raw2val() will return cal_table_t.table[0].val
|
2011-08-22 01:07:57 +00:00
|
|
|
* If a value is greater or equal to cal_table_t.table[cal_table_t.size-1].raw,
|
2001-03-04 12:54:12 +00:00
|
|
|
* rig_raw2val() will return cal_table_t.table[cal_table_t.size-1].val
|
|
|
|
*/
|
2004-10-02 10:32:09 +00:00
|
|
|
float HAMLIB_API rig_raw2val(int rawval, const cal_table_t *cal)
|
2001-03-04 12:54:12 +00:00
|
|
|
{
|
|
|
|
#ifdef WANT_CHEAP_WNO_FP
|
2017-08-05 14:09:12 +00:00
|
|
|
int interpolation;
|
2007-11-05 03:48:14 +00:00
|
|
|
#else
|
2017-08-05 14:09:12 +00:00
|
|
|
float interpolation;
|
2001-03-04 12:54:12 +00:00
|
|
|
#endif
|
2017-08-05 14:09:12 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* ASSERT(cal != NULL) */
|
|
|
|
/* ASSERT(cal->size <= MAX_CAL_LENGTH) */
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (cal->size == 0)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
return rawval;
|
|
|
|
}
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
for (i = 0; i < cal->size; i++)
|
2017-10-05 02:32:08 +00:00
|
|
|
{
|
|
|
|
if (rawval < cal->table[i].raw)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-10-05 02:32:08 +00:00
|
|
|
}
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
return cal->table[0].val;
|
|
|
|
}
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (i >= cal->size)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
return cal->table[i - 1].val;
|
|
|
|
}
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
/* catch divide by 0 error */
|
2017-10-05 02:32:08 +00:00
|
|
|
if (cal->table[i].raw == cal->table[i - 1].raw)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
return cal->table[i].val;
|
|
|
|
}
|
2001-03-04 12:54:12 +00:00
|
|
|
|
|
|
|
#ifdef WANT_CHEAP_WNO_FP
|
2017-08-05 14:09:12 +00:00
|
|
|
/* cheap, less accurate, but no fp needed */
|
|
|
|
interpolation = ((cal->table[i].raw - rawval)
|
|
|
|
* (cal->table[i].val - cal->table[i - 1].val))
|
2017-10-05 02:32:08 +00:00
|
|
|
/ (cal->table[i].raw - cal->table[i - 1].raw);
|
2001-03-04 12:54:12 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
return cal->table[i].val - interpolation;
|
2001-03-04 12:54:12 +00:00
|
|
|
#else
|
2017-08-05 14:09:12 +00:00
|
|
|
interpolation = ((cal->table[i].raw - rawval)
|
|
|
|
* (float)(cal->table[i].val - cal->table[i - 1].val))
|
2017-10-05 02:32:08 +00:00
|
|
|
/ (float)(cal->table[i].raw - cal->table[i - 1].raw);
|
2001-03-04 12:54:12 +00:00
|
|
|
#endif
|
2017-08-05 14:09:12 +00:00
|
|
|
return cal->table[i].val - interpolation;
|
2001-03-04 12:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 06:37:49 +00:00
|
|
|
/**
|
|
|
|
* \brief Convert raw data to a calibrated floating-point value, according to table
|
|
|
|
* \param rawval input value
|
|
|
|
* \param cal calibration table
|
|
|
|
* \return calibrated floating-point value
|
|
|
|
|
|
|
|
* cal_table_float_t is a data type suited to hold linear calibration
|
|
|
|
* cal_table_float_t.size tell the number of plot cal_table_t.table contains
|
|
|
|
* If a value is below or equal to cal_table_float_t.table[0].raw,
|
|
|
|
* rig_raw2val_float() will return cal_table_float_t.table[0].val
|
|
|
|
* If a value is greater or equal to cal_table_float_t.table[cal_table_float_t.size-1].raw,
|
|
|
|
* rig_raw2val_float() will return cal_table_float_t.table[cal_table_float_t.size-1].val
|
|
|
|
*/
|
|
|
|
float HAMLIB_API rig_raw2val_float(int rawval, const cal_table_float_t *cal)
|
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
float interpolation;
|
|
|
|
int i;
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
/* ASSERT(cal != NULL) */
|
|
|
|
/* ASSERT(cal->size <= MAX_CAL_LENGTH) */
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
if (cal->size == 0)
|
|
|
|
{
|
|
|
|
return rawval;
|
|
|
|
}
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
for (i = 0; i < cal->size; i++)
|
2019-05-30 06:37:49 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
if (rawval < cal->table[i].raw)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-05-30 06:37:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
return cal->table[0].val;
|
|
|
|
}
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
if (i >= cal->size)
|
|
|
|
{
|
|
|
|
return cal->table[i - 1].val;
|
|
|
|
}
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
/* catch divide by 0 error */
|
|
|
|
if (cal->table[i].raw == cal->table[i - 1].raw)
|
|
|
|
{
|
|
|
|
return cal->table[i].val;
|
|
|
|
}
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
interpolation = ((cal->table[i].raw - rawval)
|
|
|
|
* (float)(cal->table[i].val - cal->table[i - 1].val))
|
|
|
|
/ (float)(cal->table[i].raw - cal->table[i - 1].raw);
|
2019-05-30 06:37:49 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
return cal->table[i].val - interpolation;
|
2019-05-30 06:37:49 +00:00
|
|
|
}
|
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/** @} */
|