S2kit  1.1
Toolkit for working with functions defined on the sphere
test_DLT_naive.c
Go to the documentation of this file.
1 
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 
25 #include "s2kit/pml.h"
26 #include "s2kit/naive.h"
27 #include "s2kit/weights.h"
28 
29 #include "util/csecond.h"
30 
31 int main(int argc, char** argv) {
32  if (argc < 4) {
33  fprintf(stdout, "Usage: test_DLT_naive m bw loops\n");
34  return 0;
35  }
36 
37  int m = atoi(argv[1]);
38  int bw = atoi(argv[2]);
39  int loops = atoi(argv[3]);
40 
41  double* samples = (double*)malloc(sizeof(double) * 2 * bw);
42  double* coeffs = (double*)malloc(sizeof(double) * (bw - m));
43  double* new_coeffs = (double*)malloc(sizeof(double) * (bw - m));
44 
45  double* relerror = (double*)malloc(sizeof(double) * loops);
46  double* curmax = (double*)malloc(sizeof(double) * loops);
47 
48  double* plm = (double*)malloc(sizeof(double) * 2 * bw * (bw - m));
49  double* workspace = (double*)malloc(sizeof(double) * 18 * bw);
50  GeneratePmlTable(bw, m, plm, workspace);
51 
52  double* weights = (double*)malloc(sizeof(double) * 4 * bw);
53  GenerateWeightsForDLT(bw, weights);
54 
55  long int seed;
56  time(&seed);
57  srand48(seed); // init random generator
58 
59  double sum_error = 0.0;
60  double sum_relerror = 0.0;
61 
62  double total_time_f = 0.0;
63  double total_time_i = 0.0;
64 
65  for (int k = 0; k < loops; ++k) {
66  // generate random coefficients
67  for (int i = 0; i < (bw - m); ++i)
68  coeffs[i] = 2.0 * (drand48() - 0.5);
69 
70  double time_start = csecond();
71  // inverse naive transform
72  InvDLTNaive(coeffs, bw, m, samples, plm);
73  total_time_i += (csecond() - time_start);
74 
75  time_start = csecond();
76  // forward naive transform
77  DLTNaive(samples, bw, m, weights, new_coeffs, plm, workspace);
78  total_time_f += (csecond() - time_start);
79 
80  // tally up the error between the original coefficients and the new ones
81  relerror[k] = 0.0;
82  curmax[k] = 0.0;
83 
84  for (int i = 0; i < bw - m; ++i) {
85  double tmp_error = fabs(coeffs[i] - new_coeffs[i]);
86  double tmp_relerror = tmp_error / (fabs(coeffs[i]) + pow(10.0, -50.0));
87  curmax[k] = fmax(curmax[k], tmp_error);
88  relerror[k] = fmax(relerror[k], tmp_relerror);
89  }
90 
91  sum_error += curmax[k];
92  sum_relerror += relerror[k];
93  }
94 
95  double avg_error = sum_error / loops;
96  double avg_relerror = sum_relerror / loops;
97  double stddev_error = 0.0;
98  double stddev_relerror = 0.0;
99  for (int i = 0; i < loops; ++i) {
100  stddev_error += pow(avg_error - curmax[i], 2.0);
101  stddev_relerror += pow(avg_relerror - relerror[i], 2.0);
102  }
103 
104  if (loops != 1) {
105  stddev_error = sqrt(stddev_error / (loops - 1));
106  stddev_relerror = sqrt(stddev_relerror / (loops - 1));
107  }
108 
109  fprintf(stdout, "bw = %d\tm = %d\n", bw, m);
110  fprintf(stdout, "loops = %d\n", loops);
111 
112  fprintf(stdout, "Average r-o error:\t\t %.4e\t", sum_error / loops);
113  fprintf(stdout, "std dev: %.4e\n", stddev_error);
114  fprintf(stdout, "Average (r-o)/o error:\t\t %.4e\t", sum_relerror / loops);
115  fprintf(stdout, "std dev: %.4e\n\n", stddev_relerror);
116 
117  fprintf(stdout, "average forward time = %.4e\n", total_time_f / loops);
118  fprintf(stdout, "average inverse time = %.4e\n", total_time_i / loops);
119 
120  free(curmax);
121  free(relerror);
122  free(workspace);
123  free(weights);
124  free(new_coeffs);
125  free(coeffs);
126  free(samples);
127 
128  return 0;
129 }
weights.h
naive.h
main
int main(int argc, char **argv)
Definition: test_DLT_naive.c:31
InvDLTNaive
void InvDLTNaive(double *, const int, const int, double *, double *)
The inverse discrete Legendre transform.
Definition: naive.c:78
GenerateWeightsForDLT
void GenerateWeightsForDLT(const int, double *)
Generates weights for both even and odd order Legendre transforms for a given bandwidth.
Definition: weights.c:32
DLTNaive
void DLTNaive(double *, const int, const int, double *, double *, double *, double *)
The naive forward discrete Legendre transform.
Definition: naive.c:34
GeneratePmlTable
void GeneratePmlTable(const int, const int, double *, double *)
Generates all of the Pmls for a specified value of m.
Definition: pml.c:41
csecond.h
csecond
double csecond()
Definition: csecond.c:32
pml.h