ld_noise.c
Go to the documentation of this file.
1 #include <gsl/gsl_rng.h>
2 #include <gsl/gsl_randist.h>
3 
4 #include <math.h>
5 #include <libgen.h>
6 
7 #include <options/options.h>
8 #include "../csm/csm_all.h"
9 
11  int seed;
13  double sigma;
14  const char* file_input;
15  const char* file_output;
17 };
18 
19 int main(int argc, const char * argv[]) {
20  sm_set_program_name(argv[0]);
21 
22  options_banner("ld_noise: Adds noise to readings in a scan");
23 
24  struct ld_noise_params p;
25 
26  struct option* ops = options_allocate(20);
27  options_double(ops, "discretization", &p.discretization, 0.0,
28  "Size of discretization (disabled if 0)");
29  options_double(ops, "sigma", &p.sigma, 0.0,
30  "Std deviation of gaussian noise (disabled if 0)");
31  options_int(ops, "lambertian", &p.lambertian, 0,
32  "Use lambertian model cov = sigma^2 / cos(beta^2) where beta is the incidence. Need have alpha or true_alpha.");
33  options_int(ops, "seed", &p.seed, 0,
34  "Seed for random number generator (if 0, use GSL_RNG_SEED env. variable).");
35  options_string(ops, "in", &p.file_input, "stdin", "Input file ");
36  options_string(ops, "out", &p.file_output, "stdout", "Output file ");
37 
38 
39  if(!options_parse_args(ops, argc, argv)) {
40  fprintf(stderr, "A simple program for adding noise to sensor scans.\n\nUsage:\n");
41  options_print_help(ops, stderr);
42  return -1;
43  }
44 
45  FILE * in = open_file_for_reading(p.file_input);
46  if(!in) return -3;
47 
48  FILE * out = open_file_for_writing(p.file_output);
49  if(!out) return -2;
50 
51 
52  gsl_rng_env_setup();
53  gsl_rng * rng = gsl_rng_alloc (gsl_rng_ranlxs0);
54  if(p.seed != 0)
55  gsl_rng_set(rng, (unsigned int) p.seed);
56 
57  LDP ld; int count = 0;
58  while( (ld = ld_from_json_stream(in))) {
59  if(!ld_valid_fields(ld)) {
60  sm_error("Invalid laser data (#%d in file)\n", count);
61  continue;
62  }
63 
64  int i;
65  for(i=0;i<ld->nrays;i++) {
66  if(!ld->valid[i]) continue;
67 
68  double * reading = ld->readings + i;
69  if(p.sigma > 0) {
70  double add_sigma = p.sigma;
71 
72  if(p.lambertian) {
73 
74  int have_alpha = 0;
75  double alpha = 0;
76  if(!is_nan(ld->true_alpha[i])) {
77  alpha = ld->true_alpha[i];
78  have_alpha = 1;
79  } else if(ld->alpha_valid[i]) {
80  alpha = ld->alpha[i];;
81  have_alpha = 1;
82  } else have_alpha = 0;
83 
84  if(have_alpha) {
85  /* Recall that alpha points outside the surface */
86  double beta = (alpha+M_PI) - ld->theta[i];
87  add_sigma = p.sigma / cos(beta);
88  } else {
89  sm_error("Because lambertian is active, I need either true_alpha[] or alpha[]");
90  ld_write_as_json(ld, stderr);
91  return -1;
92  }
93 
94  }
95 
96  *reading += gsl_ran_gaussian(rng, add_sigma);
97 
98  if(is_nan(ld->readings_sigma[i])) {
99  ld->readings_sigma[i] = add_sigma;
100  } else {
101  ld->readings_sigma[i] = sqrt(square(add_sigma) + square(ld->readings_sigma[i]));
102  }
103  }
104  if(p.discretization > 0)
105  *reading -= fmod(*reading , p.discretization);
106  }
107 
108  ld_write_as_json(ld, out);
109  ld_free(ld);
110  }
111 
112  return 0;
113 }
int *restrict valid
Definition: laser_data.h:23
void options_banner(const char *message)
Definition: options.c:33
void sm_set_program_name(const char *name)
Definition: logging.c:21
void ld_write_as_json(LDP ld, FILE *stream)
double sigma
Definition: ld_noise.c:13
double *restrict readings_sigma
Definition: laser_data.h:32
double discretization
Definition: ld_noise.c:12
double *restrict theta
Definition: laser_data.h:21
void options_double(struct option *, const char *name, double *p, double def_value, const char *desc)
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
struct option * ops
Definition: rb_sm.c:31
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
const char * file_input
Definition: ld_noise.c:14
#define M_PI
Definition: math_utils.h:7
double *restrict true_alpha
Definition: laser_data.h:34
const char * file_output
Definition: ld_noise.c:15
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
int main(int argc, const char *argv[])
Definition: ld_noise.c:19
double *restrict readings
Definition: laser_data.h:24
struct option * options_allocate(int n)
LDP ld_from_json_stream(FILE *file)
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
int *restrict alpha_valid
Definition: laser_data.h:30
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
int is_nan(double v)
Definition: math_utils.c:60
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
gsl_rng * rng
Definition: ld_resample.c:18
double square(double x)
Definition: math_utils.c:124
double *restrict alpha
Definition: laser_data.h:28
void sm_error(const char *msg,...)
Definition: logging.c:49
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66


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