ld_smooth.c
Go to the documentation of this file.
1 #include <options/options.h>
2 
3 #include "../csm/csm_all.h"
4 
5 struct {
7  double scale_deg;
8 
11 } p;
12 
13 void ld_smooth(LDP ld, int neighbours, double scale_rad);
14 void convolve(const int*valid,const double*original, int n, double*dest, double*filter, int filter_len);
15 
16 int main(int argc, const char * argv[]) {
17  sm_set_program_name(argv[0]);
18 
19  struct option* ops = options_allocate(3);
20  options_double(ops, "scale_deg", &p.scale_deg, 0.0, "Scale factor (degrees) ");
21  options_int(ops, "neighbours", &p.neighbours, 1, "How many neighbours to consider (regardless of scale).");
22 
23  if(!options_parse_args(ops, argc, argv)) {
24  fprintf(stderr, "A simple program for smoothing a sensor scan.\n\nUsage:\n");
25  options_print_help(ops, stderr);
26  return -1;
27  }
28 
29  int errors = 0;
30  int count = -1;
31  LDP ld;
32  while( (ld = ld_read_smart(stdin)) ) {
33  count++;
34  if(!ld_valid_fields(ld)) {
35  sm_error("Invalid laser data (#%d in file)\n", count);
36  errors++;
37  continue;
38  }
39 
40  ld_smooth(ld, p.neighbours, deg2rad(p.scale_deg) );
41 
42  ld_write_as_json(ld, stdout);
43 
44  ld_free(ld);
45  }
46 
47  return errors;
48 }
49 
50 void convolve(const int*valid,const double*original, int n, double*dest, double*filter, int filter_len)
51 {
52  int i; /* index on the points */
53  int j; /* index on the filter */
54 
55  for(i=0;i<n;i++) {
56  if(!valid[i]) {
57  dest[i] = GSL_NAN;
58  continue;
59  }
60 
61  dest[i] = 0;
62  for(j=-(filter_len-1);j<=(filter_len-1);j++) {
63  int i2 = i + j;
64  if(i2<0) i2=0; if(i2>=n) i2=n-1;
65  if(!valid[i2]) i2 = i;
66  dest[i] += original[i2] * filter[abs(j)];
67  }
68  }
69 }
70 
71 void ld_smooth(LDP ld, int neighbours, double scale_rad) {
72  int len = neighbours + 1;
73  double filter[len];
74  int j;
75  static int once = 1;
76  for(j=0;j<len;j++) {
77  double dist_rad = j * ((ld->max_theta-ld->min_theta)/ld->nrays);
78  double mahal = square(dist_rad / scale_rad);
79  filter[j] = exp(-mahal);
80 
81  if(once)
82  sm_info("filter[%d] = %f mahal = %f dist_rad = %f scale_rad = %f \n", j, filter[j], mahal, dist_rad, scale_rad);
83 
84  }
85 
86  once = 0;
87 
88  /* find total filter */
89  double filter_tot = filter[0];
90  for(j=1;j<len;j++) filter_tot+=filter[j];
91  /* and normalize */
92  for(j=0;j<len;j++) filter[j]/=filter_tot;
93 
94  double new_readings[ld->nrays];
95  convolve(ld->valid, ld->readings, ld->nrays, new_readings, filter, len);
96  copy_d(new_readings, ld->nrays, ld->readings);
97 
98  for(j=0;j<len;j++) {
99 
100  }
101 }
102 
103 
int *restrict valid
Definition: laser_data.h:23
#define deg2rad(x)
Definition: gpc_test.c:22
void sm_set_program_name(const char *name)
Definition: logging.c:21
double scale_deg
Definition: ld_smooth.c:7
double max_theta
Definition: laser_data.h:19
double min_theta
Definition: laser_data.h:18
void ld_write_as_json(LDP ld, FILE *stream)
void copy_d(const double *from, int n, double *to)
Definition: math_utils.c:83
void convolve(const int *valid, const double *original, int n, double *dest, double *filter, int filter_len)
Definition: ld_smooth.c:50
int neighbours
Definition: ld_smooth.c:10
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
LDP ld_read_smart(FILE *)
void ld_smooth(LDP ld, int neighbours, double scale_rad)
Definition: ld_smooth.c:71
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
double *restrict readings
Definition: laser_data.h:24
struct option * options_allocate(int n)
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
struct @2 p
int main(int argc, const char *argv[])
Definition: ld_smooth.c:16
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void sm_info(const char *msg,...)
Definition: logging.c:71
double square(double x)
Definition: math_utils.c:124
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