Example #1
0
// returned value = meters*100 (2 decimal precision). sensorID is [1,8], speed is 100*(kmph) (2 decimal precision)
int getSensorValue(char sensorID, unsigned long speed_mph, unsigned long long time_usec){
    // position
    float pos;
    // noise to be added
    float additive_noise;
    // convert arguments to usable units
    float time_sec = (float)(time_usec/1000000.0);
    // additional time division for ratio to be correct
    time_sec /= 3600.0;
    
    // get noise
    additive_noise = 50*randnormal();
    // get pos
    pos = 555*sin(0.1*time_sec) + additive_noise;
   
    return( (int)(pos) );

}
Example #2
0
/* main: application entry point.
 * @argc: the number of commandline arguments.
 * @argv: the commandline argument string array.
 */
int main (int argc, char **argv) {
  /* declare required variables. */
  struct vector *x, *y, *z, *u;
  struct matrix *V, *R, *C;
  double theta;
  char *smean, *svar, *label;
  unsigned long n;
  int i;

  /* initialize the random number generator. */
  rand_init (time (NULL));

  /* parse the command-line arguments. */
  if (!opts_init (argc, argv)) {
    /* output an error message and exit. */
    ferror ("failed to parse arguments");
    fprintf (stdout, HELP);
    return 1;
  }

  /* see if we should display the help message. */
  if (opts_geti (OPTS_S_HELP)) {
    /* print the help message and quit. */
    fprintf (stdout, HELP);
    return 0;
  }

  /* get the number of points to produce. */
  n = opts_geti (OPTS_S_COUNT);

  /* ensure the point count is valid. */
  if (!n) {
    /* output an error message and exit. */
    ferror ("invalid point count (%lu)", n);
    fprintf (stdout, HELP);
    return 1;
  }

  /* get the points label. */
  label = opts_gets (OPTS_S_LABEL);

  /* ensure the points label is valid. */
  if (!label) {
    /* output an error message and exit. */
    ferror ("invalid label '%s'", label);
    fprintf (stdout, HELP);
    return 1;
  }

  /* allocate the math structures. */
  x = vector_new (2);
  y = vector_new (2);
  z = vector_new (2);
  u = vector_new (2);
  V = matrix_new (2, 2);
  R = matrix_new (2, 2);
  C = matrix_new (2, 2);

  /* ensure we allocated the math structures. */
  if (!x || !y || !z || !u || !V || !R || !C) {
    /* output an error message and exit. */
    ferror ("failed to allocate math structures");
    return 1;
  }

  /* get the mean string. */
  smean = opts_gets (OPTS_S_MEAN);

  /* did we get a mean string? */
  if (smean) {
    /* try to parse the string. */
    if (sscanf (smean, "(%lf,%lf)", &u->d[0], &u->d[1]) != 2) {
      /* output an error message and exit. */
      ferror ("failed to parse mean string '%s'", smean);
      fprintf (stdout, HELP);
      return 1;
    }
  }
  else {
    /* initialize to the origin. */
    u->d[0] = u->d[1] = 0.0;
  }

  /* get the variance string. */
  svar = opts_gets (OPTS_S_VAR);

  /* did we get a variance string? */
  if (svar) {
    /* try to parse the string. */
    if (sscanf (svar, "(%lf,%lf)", &V->d[0][0], &V->d[1][1]) != 2) {
      /* output an error message and exit. */
      ferror ("failed to parse variance string '%s'", svar);
      fprintf (stdout, HELP);
      return 1;
    }
  }
  else {
    /* initialize to unit variances. */
    V->d[0][0] = V->d[1][1] = 1.0;
  }

  /* get the rotation value. */
  theta = opts_getf (OPTS_S_ROT) / 180.0 * M_PI;

  /* build the rotation matrix. */
  R->d[0][0] = cos (theta);
  R->d[0][1] = -sin (theta);
  R->d[1][0] = sin (theta);
  R->d[1][1] = cos (theta);

  /* calculate the covariance matrix. */
  if (!matrix_matrix_mul (1.0, R, V, C)) {
    /* output an error message and exit. */
    ferror ("failed to calculate covariance matrix");
    return 1;
  }

  /* see if we should produce a header. */
  if (opts_geti (OPTS_S_HEADER)) {
    /* yep. print one. */
    fprintf (stdout,
      "Obs ID (Primary)\tObs ID (Obs. Sec. ID:1)\tM1.t[2]\tM1.t[1]\n");
  }

  /* loop a set number of times. */
  for (i = 0; i < n; i++) {
    /* calculate the two values. */
    x->d[0] = randnormal ();
    x->d[1] = randnormal ();

    /* scale the values. */
    if (!matrix_vector_mul (1.0, C, x, y)) {
      /* output an error message and exit. */
      ferror ("failed to scale random variate %d", i + 1);
      return 1;
    }

    /* translate the values. */
    if (!vector_sum (1.0, y, 1.0, u, z)) {
      /* output an error message and exit. */
      ferror ("failed to translate random variate %d", i + 1);
      return 1;
    }

    /* print the values. */
    fprintf (stdout, "%d\t%s\t%lf\t%lf\n", i + 1, label, z->d[0], z->d[1]);
  }

  /* free the math structures. */
  vector_free (x);
  vector_free (y);
  vector_free (z);
  vector_free (u);
  matrix_free (V);
  matrix_free (R);
  matrix_free (C);

  /* return success. */
  return 0;
}