Fixed problem when integrating & averaging arrays containing NaN's

This commit is contained in:
Richard Graham 2012-09-12 07:28:57 +00:00
parent f5241b85cd
commit efe18d33ea

View File

@ -32,7 +32,9 @@ public class ArrayUtils {
public static double mean(double[] vals){ public static double mean(double[] vals){
double subtotal = 0; double subtotal = 0;
for (int i = 0; i < vals.length; i++ ){ for (int i = 0; i < vals.length; i++ ){
subtotal += vals[i]; if (!Double.isNaN(vals[i])){
subtotal += vals[i];
}
} }
subtotal = subtotal / vals.length; subtotal = subtotal / vals.length;
return subtotal; return subtotal;
@ -68,8 +70,10 @@ public class ArrayUtils {
double sumsq = 0.0; double sumsq = 0.0;
double temp = 0; double temp = 0;
for (int i = 0; i < vals.length; i++){ for (int i = 0; i < vals.length; i++){
temp = (mu - vals[i]); if (!Double.isNaN(vals[i])){
sumsq += temp*temp; temp = (mu - vals[i]);
sumsq += temp*temp;
}
} }
return sumsq / (vals.length); return sumsq / (vals.length);
} }
@ -92,7 +96,7 @@ public class ArrayUtils {
/** /**
* Returns the integral of a given array calculated by the trapezoidal rule * Returns the integral of a given array calculated by the trapezoidal rule
* dt is the time step between each array value * dt is the time step between each array value. Any NaN values are treated as zero
*/ */
public static double trapz(double[] y, double dt){ public static double trapz(double[] y, double dt){
double stop = (y.length -1) * dt; double stop = (y.length -1) * dt;
@ -103,7 +107,10 @@ public class ArrayUtils {
double sum = 0.0; double sum = 0.0;
for (int i = 1; i < x.length; i++) { for (int i = 1; i < x.length; i++) {
sum += (x[i] - x[i-1]) * (y[i] + y[i-1]); double temp = (x[i] - x[i-1]) * (y[i] + y[i-1]);
if (!Double.isNaN(temp)){
sum += temp;
}
} }
return sum * 0.5; return sum * 0.5;
} }