Skip to content

Instantly share code, notes, and snippets.

@andrewdunndev
Created August 30, 2010 14:05
Show Gist options
  • Select an option

  • Save andrewdunndev/557446 to your computer and use it in GitHub Desktop.

Select an option

Save andrewdunndev/557446 to your computer and use it in GitHub Desktop.
private static Map<String, Value> intermittentError(Sensor sensor) {
Map<String, Value> map = new HashMap<String, Value>();
Map<String, Value> abruptError = abruptError(sensor);
if(abruptError.size() == 0 || sensor.id.equals("ST516"))
return map;
int timeOfFault = ((IntegerValue)(abruptError.get("faultIndex"))).get() + INTERMITTENT_OFFSET;
// calculate min and max values after an abrupt error, instantiate them way out of bounds so that they get pulled back in by our checks
double maximum = 0;
double minimum = 999999;
for(int index = timeOfFault; index < sensor.data.size(); index++) {
double indexValue = ((RealValue)sensor.data.elementAt(index)).get();
if(indexValue > maximum)
maximum=indexValue;
if(indexValue < minimum)
minimum=indexValue;
}
// Need quite a bit many more variables to track this next bit
int countUpper = 0;
int countLower = 0;
int switches = 1;
double meanUpper = 0;
double meanLower = 0;
double last = 0;
double mean = sensor.meanThrough(0, timeOfFault-5);
// calculate upper and lower means
for(int index = timeOfFault; index < sensor.data.size(); index++) {
double indexValue = ( (RealValue) sensor.data.elementAt(index)).get();
if(maximum - indexValue > indexValue - minimum) {
meanLower += indexValue;
countLower++;
}
else {
meanUpper += indexValue;
countUpper++;
}
}
meanUpper = meanUpper / (double) countUpper;
meanLower = meanLower / (double) countLower;
double standardDeviation = sensor.sdThrough(0, timeOfFault-6);
if((meanUpper - meanLower) / standardDeviation > STANDARD_RANGE) {
if (countUpper / (double) countLower < INTERMITTENT_UPANDLOW && countLower / (double) countUpper < INTERMITTENT_UPANDLOW) {
// look for "switches"
for(int index = timeOfFault; index < sensor.data.size(); index++) {
double indexValue = ( (RealValue) sensor.data.elementAt(index)).get();
if(maximum - indexValue > indexValue - minimum) {
if(last == meanUpper)
switches++;
last = meanLower;
}
else {
if(last == meanLower)
switches++;
last = meanUpper;
}
}
}
if(switches % 2 == 1)
switches++;
switches /= 2;
int sensorFrequency = (int)(1000 / ( (sensor.timestamps.elementAt(sensor.timestamps.size()-1)-sensor.timestamps.elementAt(0)) / sensor.timestamps.size() ));
double meanTimeNominal = 0;
double meanTimeError = 0;
double difference;
if(Math.abs(meanUpper - mean) > Math.abs(meanLower - mean)) {
meanTimeNominal = countLower / (double)(switches * sensorFrequency);
meanTimeError = (INTERMITTENT_OFFSET + countUpper) / (double)(switches * sensorFrequency);
difference = meanUpper - meanLower;
}
else {
meanTimeNominal = countUpper / (double)(switches*sensorFrequency);
meanTimeError = (INTERMITTENT_OFFSET+countLower)/(double)(switches*sensorFrequency);
difference = meanLower-meanUpper;
}
map.put("faultIndex", Value.v(timeOfFault - INTERMITTENT_OFFSET));
map.put("MeanOffset", Value.v(difference));
map.put("MeanFaultDuration", Value.v(meanTimeError));
map.put("MeanNominalDuration", Value.v(meanTimeNominal));
map.put("faultType", Value.v("IntermittentOffset"));
return map;
}
// all that for nothing
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment