+ size_t Correlation = 0;
+ int maxSum = 0;
+ int lastMax = 0;
+ if (verbose) PrintAndLog("performing %d correlations", GraphTraceLen - window);
+ for (int i = 0; i < GraphTraceLen - window; ++i) {
+ int sum = 0;
+ for (int j = 0; j < window; ++j) {
+ sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256;
+ }
+ CorrelBuffer[i] = sum;
+ if (sum >= maxSum-100 && sum <= maxSum+100){
+ //another max
+ Correlation = i-lastMax;
+ lastMax = i;
+ if (sum > maxSum) maxSum = sum;
+ } else if (sum > maxSum){
+ maxSum=sum;
+ lastMax = i;
+ }
+ }
+ if (Correlation==0){
+ //try again with wider margin
+ for (int i = 0; i < GraphTraceLen - window; i++){
+ if (CorrelBuffer[i] >= maxSum-(maxSum*0.05) && CorrelBuffer[i] <= maxSum+(maxSum*0.05)){
+ //another max
+ Correlation = i-lastMax;
+ lastMax = i;
+ //if (CorrelBuffer[i] > maxSum) maxSum = sum;
+ }
+ }
+ }
+ if (verbose && Correlation > 0) PrintAndLog("Possible Correlation: %d samples",Correlation);
+
+ if (SaveGrph){
+ GraphTraceLen = GraphTraceLen - window;
+ memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int));
+ RepaintGraphWindow();
+ }
+ return Correlation;
+}