Advertisement
cd62131

DFT

Aug 4th, 2014
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define _USE_MATH_DEFINES
  4. #include <math.h>
  5. static const int N = 128;
  6. static FILE *open_for_read(const char *file_name) {
  7.   FILE *in = fopen(file_name, "r");
  8.   if (!in) {
  9.     fprintf(stderr, "open err: %s\n", file_name);
  10.     exit(1);
  11.   }
  12.   return in;
  13. }
  14. static void read_data(FILE *in, double *data) {
  15.   int i;
  16.   for (i = 0; i < N; i++)
  17.     fscanf(in, "%lf", &data[i]);
  18. }
  19. static void dft(double *data, double *real, double *image) {
  20.   int k, n;
  21.   double theta;
  22.   for (k = 0; k < N; k++) {
  23.     real[k] = 0.;
  24.     image[k] = 0.;
  25.     for (n = 0; n < N; n++) {
  26.       theta = 2 * M_PI * k * n / N;
  27.       real[k] += data[n] * cos(theta);
  28.       image[k] += - data[n] * sin(theta);
  29.     }
  30.   }
  31. }
  32. static void power_spectrum(double *real, double *image, double *result) {
  33.   int i;
  34.   for (i = 0; i < N; i++)
  35.     result[i] = sqrt(real[i] * real[i] + image[i] * image[i]);
  36. }
  37. static void filter(double *real, double *image) {
  38.   int i;
  39.   for (i = 0; i < N; i++)
  40.     if (i < 20 || N - 20 < i) {
  41.       real[i] = 0.;
  42.       image[i] = 0.;
  43.     }
  44. }
  45. static void idft(double *real, double *image, double *idft_real, double *idft_image) {
  46.   int k, n;
  47.   double re, im, theta;
  48.   for (k = 0; k < N; k++) {
  49.     re = 0.;
  50.     im = 0.;
  51.     for (n = 0; n < N; n++) {
  52.       theta = 2 * M_PI * k * n / N;
  53.       re += real[n] * cos(theta) - image[n] * sin(theta);
  54.       im += real[n] * sin(theta) + image[n] * cos(theta);
  55.     }
  56.     idft_real[k] = re / N;
  57.     idft_image[k] = im / N;
  58.   }
  59. }
  60. FILE *open_for_write(const char *file_name) {
  61.   FILE *out = fopen(file_name, "w");
  62.   if (!out) {
  63.     fprintf(stderr, "open error: %s\n", file_name);
  64.     exit(1);
  65.   }
  66.   return out;
  67. }
  68. static void print_data(FILE *out, double *data) {
  69.   int i;
  70.   for (i = 0; i < N; i++)
  71.     fprintf(out, "%lf\n", data[i]);
  72. }
  73. static void process(const char *noize, const char *power, const char *power_filter,
  74.     const char *idft_amp, const char *idft_wav) {
  75.   FILE *in, *out;
  76.   double data[N], real[N], image[N], power_spec[N], power_spec_filter[N], idft_real[N], idft_image[N];
  77.   in = open_for_read(noize);
  78.   read_data(in, data);
  79.   fclose(in);
  80.   dft(data, real, image);
  81.   power_spectrum(real, image, power_spec);
  82.   filter(real, image);
  83.   power_spectrum(real, image, power_spec_filter);
  84.   idft(real, image, idft_real, idft_image);
  85.   out = open_for_write(power);
  86.   print_data(out, power_spec);
  87.   fclose(out);
  88.   out = open_for_write(power_filter);
  89.   print_data(out, power_spec_filter);
  90.   fclose(out);
  91.   out = open_for_write(idft_amp);
  92.   print_data(out, idft_real);
  93.   fclose(out);
  94.   out = open_for_write(idft_wav);
  95.   print_data(out, idft_image);
  96.   fclose(out);
  97. }
  98. int main(void) {
  99.   const char *sine_noize = "data_sin_noise.txt";
  100.   const char *sine_power1 = "data_out3.txt";
  101.   const char *sine_power2 = "data_out4.txt";
  102.   const char *sine_idft_amp = "data_out5.txt";
  103.   const char *sine_idft_wav = "data_out6.txt";
  104.   const char *square_noize = "data_square_noise.txt";
  105.   const char *square_power1 = "data_out9.txt";
  106.   const char *square_power2 = "data_out10.txt";
  107.   const char *square_idft_amp = "data_out11.txt";
  108.   const char *square_idft_wav = "data_out12.txt";
  109.   const char *triangle_noise = "data_triangle_noise.txt";
  110.   const char *triangle_power1 = "data_out15.txt";
  111.   const char *triangle_power2 = "data_out16.txt";
  112.   const char *triangle_idft_amp = "data_out17.txt";
  113.   const char *triangle_idft_wav = "data_out18.txt";
  114.   process(sine_noize, sine_power1, sine_power2, sine_idft_amp, sine_idft_wav);
  115.   process(square_noize, square_power1, square_power2, square_idft_amp, square_idft_wav);
  116.   process(triangle_noise, triangle_power1, triangle_power2, triangle_idft_amp, triangle_idft_wav);
  117.   return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement