public class LinearCalibrationFilter extends AbstractCalibrationFilter
Operational mode
In operational mode the filter corrects incoming samples for offset and scale
errors. The correction algorithm can uses calibration parameters for this.
These calibration parameters can be computed in calibration mode. They can
also be loaded from the filesystem using the load() method.
How it works
The correction algorithm uses two calibration parameters, offset and scale.
The offset parameter corrects for offset errors. An offset error results in a
sample being a fixed amount off the truth: 2 becomes 3, 6 becomes 7. It is
corrected by subtracting a constant value (1 in this example) from the
sample, the offset error.
The scale parameter corrects for scale errors. As a result of a scale error a
sample is always a fixed percentage off the truth: 2 becomes 3, 6 becomes 9.
It is corrected by dividing the sample with a constant value, the scale
error, 1.5 in this case.
The combined correction formula is correctedValue = ( rawValue - offsetError
) / scaleError. This calibration technique works on all sensors who's output
is linear to the input.
If no correction parameters are calculated or loaded the filter uses 0 for
offset correction and 1 for scale correction.
How to use the filter in operational mode
In operational mode is the default mode. It is always active while the filter
is not calibrating.
Calibration mode
In calibration mode the two calibration parameters are calculated. This is
done by comparing samples to a user specified reference value and/or range.
Once calibration parameters are calculated they can be used immediately or
stored to the filesystem for later use using the store() method.
How it works
The CalibratorFiltersupports both offset and scale calibration. However both
are optional and can be enabled or disabled. During calibration the filter
collects minimum and maximum sample values. From this it calculates offset
(as the average of the minimum and maximum value corrected for the reference
value) and scale (as the difference between maximum and minimum value scaled
for range). To minimize the effect of sensors noise or sensors movements during
calculation sample values are low-passed before they are being used for
calibration.
How to use the filter in calibration mode
Calibration is started using the startCalibration method and ended with the
endCalibration method. During calibration the program that uses the filter
must fetch samples to collect data for the calibration algorithm. At the end
the calibration process the calculated calibration settings can be stored
using the store(filename) method. Calibration can be paused if needed.
How to tune the calibration process
There are three important parameters to the calibration process that can be
modified by the user program.
AbstractCalibrationFilter.CalibrationFileException
Modifier and Type | Field and Description |
---|---|
private int |
calibrationType |
private float[] |
lowerBound |
private float[] |
offset |
static int |
OFFSET_AND_SCALE_CALIBRATION |
static int |
OFFSET_CALIBRATION |
private float[] |
scale |
private float[] |
upperBound |
calibrating, lowPassFilter, max, min, numberOfSamplesInCalibration, sum
sampleSize, source
Constructor and Description |
---|
LinearCalibrationFilter(SampleProvider source) |
LinearCalibrationFilter(SampleProvider source,
java.lang.String filename)
Construcor
|
Modifier and Type | Method and Description |
---|---|
void |
fetchSample(float[] dst,
int off)
Fetches a sample from the sensors and updates calibration parameters when
the calibration process is running.
|
int |
getCalibrationType() |
int |
getCallibrationType() |
float[] |
getOffsetCorrection()
Returns an array with the offset correction parameters that are currently
in use
|
float[] |
getScaleCorrection()
Returns an array with the scale correction paramaters that are currently in
use
|
void |
open(java.lang.String name) |
private void |
reset() |
void |
save(java.lang.String filename)
Stores the calibration parameters, offset and/or scale depending on current
settings, to a filterProperties file.
|
void |
setCalibrationType(int calibrationType) |
void |
setOffsetCalibration(float offset) |
void |
setOffsetCalibration(float[] offset) |
void |
setScaleCalibration(float ulBound) |
void |
setScaleCalibration(float[] lBound,
float[] uBound) |
void |
setScaleCalibration(float lBound,
float uBound) |
void |
startCalibration()
Starts a calibration process.
|
void |
stopCalibration()
Halts the process of updating calibration parameters.
|
java.lang.String |
toString() |
getProperty, getPropertyArray, load, resumeCalibration, setProperty, setPropertyArray, setTimeConstant, store, suspendCalibration
sampleSize
public static final int OFFSET_CALIBRATION
public static final int OFFSET_AND_SCALE_CALIBRATION
private float[] lowerBound
private float[] upperBound
private float[] offset
private float[] scale
private int calibrationType
public LinearCalibrationFilter(SampleProvider source, java.lang.String filename)
source
- SampleProviderfilename
- Filename to load calibration settings frompublic LinearCalibrationFilter(SampleProvider source)
public java.lang.String toString()
toString
in class java.lang.Object
public void setScaleCalibration(float ulBound)
public void setScaleCalibration(float lBound, float uBound)
public void setScaleCalibration(float[] lBound, float[] uBound)
public void setOffsetCalibration(float offset)
public void setOffsetCalibration(float[] offset)
public void setCalibrationType(int calibrationType)
public int getCallibrationType()
public float[] getOffsetCorrection()
public float[] getScaleCorrection()
public void startCalibration()
startCalibration
in interface Calibrate
startCalibration
in class AbstractCalibrationFilter
public void stopCalibration()
AbstractCalibrationFilter
stopCalibration
in interface Calibrate
stopCalibration
in class AbstractCalibrationFilter
public void save(java.lang.String filename)
filename
- A name to use for storing calibration parameterspublic void open(java.lang.String name)
private void reset()
public void fetchSample(float[] dst, int off)
fetchSample
in interface SampleProvider
fetchSample
in class AbstractCalibrationFilter
dst
- The array to store the sample in.off
- The elements of the sample are stored in the array starting at the offset position.public int getCalibrationType()