Example #1
0
int
main (void)
{
  int i; double data[2*128];

  for (i = 0; i < 128; i++)
    {
       REAL(data,i) = 0.0; IMAG(data,i) = 0.0;
    }

  REAL(data,0) = 1.0;

  for (i = 1; i <= 10; i++)
    {
       REAL(data,i) = REAL(data,128-i) = 1.0;
    }

  for (i = 0; i < 128; i++)
    {
      printf ("%d %e %e\n", i, 
              REAL(data,i), IMAG(data,i));
    }
  printf ("\n");

  gsl_fft_complex_radix2_forward (data, 1, 128);

  for (i = 0; i < 128; i++)
    {
      printf ("%d %e %e\n", i, 
              REAL(data,i)/sqrt(128), 
              IMAG(data,i)/sqrt(128));
    }

  return 0;
}
Example #2
0
int main(void)
{
	int i; double data[2*N];
	double freq;
	FILE * indat = fopen("dat","r");
	if (readfile(indat, data) != 0) {printf("Bad input file!\n"); return -1;}
//	printf("file read...\n");
#if 0
	for(i=0; i<N; i++){
		printf("%d %e %e\n",i,REAL(data,i),IMAG(data,i));
	}
#endif

	gsl_fft_complex_radix2_forward(data,1,N);
	for(i=0;i<N; i++){
		if(i < N/2){
			freq = ((double) i)/N ;//* 2.0 * M_PI;
		}else{
			freq = ((double)(i-N))/N;// * 2.0 * M_PI;
		}

		printf("%g %e %e\n",freq,
				REAL(data,i)/sqrt(N*N),
				IMAG(data,i)/sqrt(N*N));
	}
	return 0;
}
Example #3
0
File: fft.c Project: rcortini/mylib
/* calculates the fast Fourier transform of a complex packed array, and stores
 * it into fft_results. The length of the array is 2*N, and the storage
 * convention is that the real and imaginary parts of the complex number are
 * stored in consecutive locations */
int complex_fft (size_t N, double *data, double *fft_results) {
  size_t i;
  int retcode;

  /* initialize the data for fft */
  for (i=0; i<2*N; i++) fft_results [i] = data [i];

  /* use the corresponding routine if N is power of 2 */
  if (is_power_of_n (N,2)) {
    /* perform the fft */
    retcode = gsl_fft_complex_radix2_forward (fft_results, 1, N);
  }
  else {
    /* alloc memory for wavetable and workspace */
    gsl_fft_complex_wavetable * wavetable = gsl_fft_complex_wavetable_alloc (N);
    gsl_fft_complex_workspace * workspace = gsl_fft_complex_workspace_alloc (N);

    /* perform the fft */
    retcode = gsl_fft_complex_forward (fft_results, 1, N, wavetable, workspace);

    /* free memory */
    gsl_fft_complex_wavetable_free (wavetable);
    gsl_fft_complex_workspace_free (workspace);
  }
  return retcode;
}
Example #4
0
int
main (void)
{
//  printf("EPS: %e\n", EPS);

  size_t size = pow(2, SIZE);
  size_t i; double data[2*size];

  for (i = 0; i <  size; i++)
  {
       REAL(data,i) = 0.0; IMAG(data,i) = 0.0;
  }

  REAL(data,0) = 1.0;

  for (i = 1; i <= 10; i++)
  {
       REAL(data,i) = REAL(data,size - i) = 1.0;
  }

/*  for (i = 0; i < size; i++)
    {
      printf ("%d %e %e\n", i, 
              REAL(data,i), IMAG(data,i));
    }
  printf ("\n");
*/
# pragma adapt begin
  gsl_fft_complex_radix2_forward (data, 1, size);

  double norm = 0;
  for (i = 0; i < size; i++)
  {
//      printf ("%d %e %e\n", i, 
//              REAL(data,i)/sqrt(128), 
//              IMAG(data,i)/sqrt(128));
    norm += square(REAL(data, i)/sqrt(size)) + square(IMAG(data,i)/sqrt(size));
  }

  norm = sqrt(norm); 
# pragma adapt output norm EPS
# pragma adapt end

//  printf("norm: %.16f\n", norm);

  double diff = (ANS-norm);
  double error = ABS(diff);
  if ((double)error < (double)EPS) {
    printf("fft - SUCCESSFUL!\n");
  }
  else {
    printf("fft - FAILED!\n");
  }
  return 0;
}
Example #5
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);
}
int
main (void)
{
  int i; 

  for (i = 0; i < PULSE_SAMPLE_LEN; i++)
    {
       REAL(data,i) = 0.0; IMAG(data,i) = 0.0;
    }

  REAL(data,0) = 1.0;

  for (i = 1; i <= PULSE_ACTUAL_LEN; i++)
    {
       REAL(data,i) = REAL(data,PULSE_SAMPLE_LEN-i) = 1.0;
    }

  #if PRINT_RES
  for (i = 0; i < PULSE_SAMPLE_LEN; i++)
    {
      printf ("%d %e %e\n", i,
              REAL(data,i), IMAG(data,i));
    }
  printf ("\n\n");
  #endif  

  gsl_fft_complex_radix2_forward (data, 1, PULSE_SAMPLE_LEN);

  #if PRINT_RES
  for (i = 0; i < PULSE_SAMPLE_LEN; i++)
    {
      printf ("%d %e %e\n", i,
              REAL(data,i)/sqrt(PULSE_SAMPLE_LEN),
              IMAG(data,i)/sqrt(PULSE_SAMPLE_LEN));
    }
  #endif

  return 0;
}
Example #7
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);
}
Example #8
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);
}