Skip to content

Instantly share code, notes, and snippets.

@enrichman
Created September 10, 2016 09:37
Show Gist options
  • Save enrichman/a0bd7ba32df47d6ebc920191058c69e2 to your computer and use it in GitHub Desktop.
Save enrichman/a0bd7ba32df47d6ebc920191058c69e2 to your computer and use it in GitHub Desktop.
import weka.core.*;
public class CrossCorrelationDistance extends NormalizableDistance {
@Override
public double distance(Instance first, Instance second) {
double mx = 0;
double my = 0;
for(int i=0; i<first.numValues(); i++) {
mx += first.value(i);
my += second.value(i);
}
mx /= first.numValues();
my /= second.numValues();
double num = 0;
double sumX = 0;
double sumY = 0;
for(int i=0; i<first.numValues(); i++) {
double x = first.value(i);
double y = second.value(i);
num += (x-mx) * (y-my);
sumX += Math.pow(x-mx, 2);
sumY += Math.pow(y-my, 2);
}
double xcorr = num / (Math.sqrt(sumX) * Math.sqrt(sumY));
if(Double.isNaN(xcorr))
xcorr = 0;
return xcorr;
}
@Override
public String globalInfo() {
return null;
}
@Override
protected double updateDistance(double currDist, double diff) {
return 0;
}
public String getRevision() {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment