Ejemplo n.º 1
0
/*
 * The spectrum of a simple AM wave
 */
void am_wave(void) {
    plot_t p;

    p.outfile = "output3.png";
    p.title = "AM wave";
    p.xlabel = "t [s]";
    p.ylabel = "u(t) [m]";
    p.xmin = 0;
    p.xmax = 0;
    init_gnuplot(&p);

    gsl_complex_packed_array data = calloc(2*N, sizeof(double));

    BEGIN_SCATTER(p, "u(t)");

    double t = 0;

    for (int i = 0; i < 2*N; i += 2) {
        data[i] = u(t);
        PLOT(p, t, data[i]);
        t += dt;
    }

    END_PLOT(p);

    p.outfile = "output4.png";
    p.title = "FFT of AM wave";
    p.xlabel = "f [Hz]";
    p.ylabel = "S(f) = |U(f)|^2 [m^2]";
    p.xmin = 0;
    p.xmax = 40;
    init_gnuplot(&p);

    gsl_fft_complex_radix2_forward(data, 1, N);

    BEGIN_PLOT(p, "S(f)");

    double f = 0;

    for (int i = 0; i < N; i += 2) {
        PLOT(p, f, (data[i]*data[i] + data[i+1]*data[i+1])/(N*N));
        f += 1.0;
    }

    END_PLOT(p);

    free(data);
}
Ejemplo n.º 2
0
int
main (int argc, char *argv[])
{

  int i;
  long seed = RAND_SEED;

  for (i = 1; i < argc; i++) {
    switch (*(argv[i] + 1)) {
    case 'r':
      seed = atoi (argv[++i]);
      break;
    default:
      fprintf (stderr, "Usage : %s\n", argv[0]);
      fprintf (stderr, "\t-r : random-seed(%ld)\n", seed);
      exit (0);
      break;
    }
  }

  srand48 (seed);
  init_1d_dk2011_bnf();

  // set_1d_ordered_connection();
  // test_symmetric_connections(1);
  init_gnuplot ();

  for (i = 0; i < N_LEARNING; i++) {
    if (i % 10 == 0) {
      sprintf (buf, "t=%d", i);
      //plot3d_gnuplot (buf);
      plot3d_gnuplot_file(buf,i);
    }
    one_cycle();
  }
  
}
Ejemplo n.º 3
0
/*
 * The Fourier transform of a Gaussian
 */
void gaussian(void) {
    plot_t p;

    p.outfile = "output1.png";
    p.title = "Gaussian";
    p.xlabel = "t [s]";
    p.ylabel = "x(t) [m]";
    p.xmin = -N/2*dt;
    p.xmax = N/2*dt;
    init_gnuplot(&p);

    gsl_complex_packed_array data = calloc(2*N, sizeof(double));

    BEGIN_SCATTER(p, "x(t)");

    double t = -N/2 * dt;

    for (int i = 0; i < 2*N; i += 2) {
        data[i] = x(t);
        PLOT(p, t, data[i]);
        t += dt;
    }

    END_PLOT(p);

    gsl_fft_complex_radix2_forward(data, 1, N);

    p.outfile = "output2.png";
    p.title = "FFT of a Gauissian";
    p.xlabel = "f [Hz]";
    p.ylabel = "X(f) [m]";
    p.xmin = -512;
    p.xmax = 512;
    init_gnuplot(&p);

    fprintf(p.fp, "plot '-' title 'Real',"
            "'-' title 'Imag',"
            "'-' with lines title 'Analytical'\n");

    // Real part
    double f = -1.0/(2.0*dt);

    for (int i = N; i < 2*N; i += 2) {
        PLOT(p, f, creal(shift(f))*(data[i]/(double) N));
        f += 1.0;
    }

    for (int i = 0; i < N; i += 2) {
        PLOT(p, f, creal(shift(f))*(data[i]/(double) N));
        f += 1.0;
    }

    // Imag. part
    NEW_PLOT(p);

    f = -1.0/(2.0*dt);

    for (int i = N+1; i <= 2*N; i += 2) {
        PLOT(p, f, cimag(shift(f))*data[i]/(double) N);
        f += 1.0;
    }

    for (int i = 1; i <= N; i += 2) {
        PLOT(p, f, cimag(shift(f))*data[i]/(double) N);
        f += 1.0;
    }

    // Analytical
    NEW_PLOT(p);

    f = -1.0/(2.0*dt);

    for (int i = 1; i <= 2*N; i += 2) {
        PLOT(p, f, X(f));
        f += 1.0;
    }

    END_PLOT(p);

    free(data);
}
Ejemplo n.º 4
0
/*
 * Extracting information from a noisy signal
 */
void extract_noisy_signal(int count, double *values) {
    plot_t p;

    p.outfile = "output5.png";
    p.title = "Noisy signal";
    p.xlabel = "t [s]";
    p.ylabel = "u(t) [m]";
    p.xmin = 0;
    p.xmax = 0;
    init_gnuplot(&p);

    gsl_complex_packed_array data = calloc(2*count, sizeof(double));

    // Noisy signal
    BEGIN_SCATTER(p, "u(t)");

    double t = 0;

    for (int i = 0; i < count; i++) {
        PLOT(p, t, values[i]);
        data[2*i] = values[i];
        t += 1.0/count;
    }

    END_PLOT(p);

    p.outfile = "output6.png";
    p.title = "FFT of noisy signal";
    p.xlabel = "f [Hz]";
    p.ylabel = "U(f) [m]";
    p.xmin = -1500;
    p.xmax = 1500;
    init_gnuplot(&p);

    gsl_fft_complex_radix2_forward(data, 1, count);

    // FFT of noisy signal
    BEGIN_SCATTER(p, "U(f)");

    double f = -count/2;

    for (int i = count; i < 2*count; i += 2) {
        double y = data[i];
        data[i] = H(f) * data[i];
        data[i+1] = H(f) * data[i+1];
        PLOT(p, f, y);
        f += 1.0;
    }

    for (int i = 0; i < count; i += 2) {
        double y = data[i];
        data[i] = H(f) * data[i];
        data[i+1] = H(f) * data[i+1];
        PLOT(p, f, y);
        f += 1.0;
    }

    END_PLOT(p);

    gsl_fft_complex_radix2_inverse(data, 1, count);

    p.outfile = "output7.png";
    p.title = "Inverse FFT of data";
    p.xlabel = "t [s]";
    p.ylabel = "u(t) [m]";
    p.xmin = 0;
    p.xmax = 1;
    init_gnuplot(&p);

    // Filtered signal
    BEGIN_PLOT(p, "u(t)");

    t = 0;

    double umax = 0;
    int periods = 0;
    char letter = 0;
    int bits = 0;
    const double plen = 0.0078;

    for (int i = 0; i < 2*count; i += 2) {
        if (t >= plen * (double)periods && t < plen * ((double)periods + 1.0)) {
            if (data[i] > umax) umax = data[i];
        } else {
            if (umax < 1) letter *= 2;
            else letter = letter*2 + 1;
            bits++;
            if (bits % 8 == 0) {
                printf("%c", letter);
                bits = 0;
                letter = 0;
            }
            umax = 0;
            periods++;
        }

        PLOT(p, t, (data[i]));
        t += 1.0/count;
    }
    if (umax < 1) letter *= 2;
    else letter = letter*2 + 1;
    printf("%c\n", letter);

    END_PLOT(p);
}