Example #1
0
int main() {
  int i, h_index[2];
  double value;

  /* Create two histograms */
  h_index[0]=hist_create("Gaus",20,-3.0,3.0);
  h_index[1]=hist_create("Flat",20,0.0,1.0);

  for (i=0;i<10000;i++) {
    /* Generate a random number following a Gaussian distribution */
    value = random_dist_gaus(1.0);

    /* Add this number to the histogram */
    hist_book(h_index[0],value,1.0);

    /* Generate a random number following a flat distribution */
    value = random_dist_flat();

    /* Add this number to the histogram */
    hist_book(h_index[1],value,1.0);
  }

  /* Plot both histograms using gnuplot */
  hist_plot(h_index[0]);
  hist_plot(h_index[1]);

  return 0;
}
int main()
{
	double m, stddev;
	int hist[n_bins], samples = 10;

	while (samples <= 10000) {
		m = avg(samples, &stddev, hist);
		printf("size %5d: %g %g\n", samples, m, stddev);
		samples *= 10;
	}

	printf("\nHistograph:\n");
	hist_plot(hist);

	printf("\nMoving average:\n  N     Mean    Sigma\n");
	moving_rec rec = { 0, 0, 0, {0} };
	double data[100];
	for (int i = 0; i < 10000; i++) {
		for (int j = 0; j < 100; j++) data[j] = rand01();

		moving_avg(&rec, data, 100);

		if ((i % 1000) == 999) {
			printf("%4lluk %f %f\n",
				rec.size/1000,
				rec.sum / rec.size,
				sqrt(rec.x2 * rec.size - rec.sum * rec.sum)/rec.size
			);
		}
	}
}