ld_linearize.c
Go to the documentation of this file.
1 #include <options/options.h>
2 #include "../csm/csm_all.h"
3 
4 void ld_linearize(LDP ld);
5 double weighted_mean(double *x, double *weight, int n);
6 
7 int main(int argc, const char * argv[]) {
8  sm_set_program_name(argv[0]);
9 
10  int errors = 0;
11  int count = -1;
12  LDP ld;
13  while( (ld = ld_read_smart(stdin)) ) {
14  count++;
15  if(!ld_valid_fields(ld)) {
16  sm_error("Invalid laser data (#%d in file)\n", count);
17  errors++;
18  continue;
19  }
20 
21  ld_linearize(ld);
22 
23  ld_write_as_json(ld, stdout);
24 
25  ld_free(ld);
26  }
27 
28  return errors;
29 }
30 
31 double weighted_mean(double *x, double *weight, int n) {
32  double sum_weight = 0;
33  double sum_x = 0;
34  int i;
35  for(i=0;i<n;i++) {
36  sum_x += weight[i] * x[i];
37  sum_weight += weight[i];
38  }
39  return sum_x / sum_weight;
40 }
41 
42 
43 void ld_linearize(LDP ld) {
44  int i;
45  for(i=0;i<ld->nrays;i++) {
46  if(-1 == ld->cluster[i]) continue;
47 
48  int this_cluster = ld->cluster[i];
49  int indexes[ld->nrays];
50  int nindexes = 0;
51 
52  int j;
53  for(j=i;j<ld->nrays;j++)
54  if(ld->cluster[j]==this_cluster)
55  indexes[nindexes++] = j;
56 
57  double alpha[nindexes];
58  double alpha_weight[nindexes];
59  for(j=0;j<nindexes;j++) {
60  alpha[j] = ld->alpha[indexes[j]];
61  alpha_weight[j] = 1 / ld->cov_alpha[indexes[j]];
62  }
63 
64  double est_alpha = weighted_mean(alpha, alpha_weight, nindexes);
65 
66  double rho[nindexes];
67  double rho_weight[nindexes];
68  for(j=0;j<nindexes;j++) {
69  int i = indexes[j];
70  double theta = ld->theta[i];
71  double x = cos(theta) * ld->readings[i];
72  double y = sin(theta) * ld->readings[i];
73  rho[j] = cos(est_alpha) * x + sin(est_alpha) * y;
74  rho_weight[j] = 1 / ( cos(est_alpha) * cos(theta)
75  + sin(est_alpha) * sin(theta) );
76  }
77 
78  double est_rho = weighted_mean(rho, rho_weight, nindexes);
79 
80  for(j=0;j<nindexes;j++) {
81  int i = indexes[j];
82  double theta = ld->theta[i];
83  ld->readings[i] = est_rho / (cos(est_alpha) * cos(theta)
84  + sin(est_alpha) * sin(theta));
85  }
86 
87  i = indexes[nindexes-1];
88  }
89 
90 }
91 
92 
93 
94 
double *restrict cov_alpha
Definition: laser_data.h:29
void ld_linearize(LDP ld)
Definition: ld_linearize.c:43
void sm_set_program_name(const char *name)
Definition: logging.c:21
void ld_write_as_json(LDP ld, FILE *stream)
int *restrict cluster
Definition: laser_data.h:26
double *restrict theta
Definition: laser_data.h:21
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
double weighted_mean(double *x, double *weight, int n)
Definition: ld_linearize.c:31
LDP ld_read_smart(FILE *)
int main(int argc, const char *argv[])
Definition: ld_linearize.c:7
void ld_free(LDP ld)
Definition: laser_data.c:87
double *restrict readings
Definition: laser_data.h:24
double *restrict alpha
Definition: laser_data.h:28
void sm_error(const char *msg,...)
Definition: logging.c:49


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23