Skip to content

Instantly share code, notes, and snippets.

@iondune
Created November 20, 2013 02:56
Show Gist options
  • Save iondune/7556917 to your computer and use it in GitHub Desktop.
Save iondune/7556917 to your computer and use it in GitHub Desktop.
QND tool for comparing two histograms of equal and known bin count.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
if (argc != 4)
{
fprintf(stderr, "usage: histdiff <size> <file1> <file2>\n");
return -1;
}
int size = atoi(argv[1]);
FILE * f1, * f2;
f1 = fopen(argv[2], "r");
if (! f1)
{
perror(argv[2]);
return -1;
}
f2 = fopen(argv[3], "r");
if (! f2)
{
perror(argv[3]);
return -1;
}
int differences = 0;
int totalA = 0;
int totalB = 0;
for (int i = 0; i < size; ++ i)
{
int binA, binB;
int countA, countB;
fscanf(f1, "%d, %d\n", & binA, & countA);
fscanf(f2, "%d, %d\n", & binB, & countB);
differences += abs(countA - countB);
totalA += countA;
totalB += countB;
}
if (totalA != totalB)
printf("Histogram totals differ! %d %d\n", totalA, totalB);
printf("%d of %d values in wrong bin.\n", differences, totalA);
printf("%.6f%% error\n", differences / (double) totalA * 100.0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment