]> hackdaworld.org Git - my-code/api.git/commitdiff
finished 1d-dft
authorhackbard <hackbard>
Thu, 23 Sep 2004 14:26:21 +0000 (14:26 +0000)
committerhackbard <hackbard>
Thu, 23 Sep 2004 14:26:21 +0000 (14:26 +0000)
fourier/fourier.c
fourier/fourier.h

index fca6d6a2fdda3072aad543e6c85165102925c0b1..255012bfaa232ce68c4738d03ca6afa908164cc9 100644 (file)
@@ -8,7 +8,7 @@
 
 int fourier_init(t_fourier *fourier,int outfd) {
 
fprintf(outfd,"[fourier] initializing fourier api ...\n");
dprintf(outfd,"[fourier] initializing fourier api ...\n");
 
  fourier->outfd=outfd;
  fourier->type=0;
@@ -25,7 +25,7 @@ int fourier_alloc_data(t_fourier *fourier) {
     fourier->data[i]=(t_complex *)malloc(fourier->data_len[i]*sizeof(t_complex));
     fourier->ftdata[i]=(t_complex *)malloc(fourier->data_len[i]*sizeof(t_complex));
     if((fourier->data[i]==NULL)||(fourier->ftdata[i]==NULL)) {
-      fprintf(fourier->outfd,"[fourier] malloc failed\n");
+      dprintf(fourier->outfd,"[fourier] malloc failed\n");
       return F_ALLOC_FAIL;
     }
   }
@@ -37,11 +37,11 @@ int fourier_shutdown(t_fourier *fourier) {
 
   int i;
 
-  fprintf(fourier->outfd,"[fourier] shutdown\n");
+  dprintf(fourier->outfd,"[fourier] shutdown\n");
 
   for(i=0;i<fourier->dim;i++) {
-    free(fourier->data[dim]);
-    free(fourier->ftdata[dim]);
+    free(fourier->data[i]);
+    free(fourier->ftdata[i]);
   }
 
   return F_SUCCESS;
@@ -53,15 +53,15 @@ int fourier_dft_1d(t_fourier *fourier) {
   double arg;
 
   if(fourier->type&FWD) {
-    for(dim=1;dim<=fourier->dim;dim++) {
+    for(dim=0;dim<fourier->dim;dim++) {
       for(k=0;k<fourier->data_len[dim];k++) {
         fourier->ftdata[dim][k].r=0;
         fourier->ftdata[dim][k].i=0;
         for(i=0;i<fourier->data_len[dim];i++) {
          /* f(k) = 1/N sum(n=0-N) f(n) exp(-i*k*2*PI*n/N) */
           arg=-1.0*k*M_PI*i/fourier->data_len[dim];
-          fourier->ftdata[dim][k].r+=(cos(arg)*fourier->data[dim][i]-sin(arg)*fourier->data[dim][i].i);
-          fourier->ftdata[dim][k].i+=(sin(arg)*fourier->data[dim][i]+cos(arg)*fourier->data[dim][i].i);
+          fourier->ftdata[dim][k].r+=(cos(arg)*fourier->data[dim][i].r-sin(arg)*fourier->data[dim][i].i);
+          fourier->ftdata[dim][k].i+=(sin(arg)*fourier->data[dim][i].r+cos(arg)*fourier->data[dim][i].i);
         }
         fourier->ftdata[dim][k].r/=fourier->data_len[dim];
         fourier->ftdata[dim][k].i/=fourier->data_len[dim];
@@ -75,8 +75,8 @@ int fourier_dft_1d(t_fourier *fourier) {
         fourier->data[dim][k].i=0;
         for(i=0;i<fourier->data_len[dim];i++) {
           arg=1.0*k*M_PI*i/fourier->data_len[dim];
-          fourier->data[dim][k].r+=(cos(arg)*fourier->ftdata[dim][i]-sin(arg)*fourier->ftdata[dim][i].i);
-          fourier->data[dim][k].i+=(sin(arg)*fourier->ftdata[dim][i]+cos(arg)*fourier->ftdata[dim][i].i);
+          fourier->data[dim][k].r+=(cos(arg)*fourier->ftdata[dim][i].r-sin(arg)*fourier->ftdata[dim][i].i);
+          fourier->data[dim][k].i+=(sin(arg)*fourier->ftdata[dim][i].r+cos(arg)*fourier->ftdata[dim][i].i);
         }
       }
     }
@@ -85,9 +85,17 @@ int fourier_dft_1d(t_fourier *fourier) {
   return F_SUCCESS;
 }
 
+int fourier_dft_2d(t_fourier *fourier) {
+  return 0;
+}
+
+int fourier_dft_3d(t_fourier *fourier) {
+  return 0;
+}
+
 int fourier_calc(t_fourier *fourier) {
 
-  fprintf(fourier->outfd,"[fourier] %d dimensional %c-%cft calculation ...\n",
+  dprintf(fourier->outfd,"[fourier] %d dimensional %c-%cft calculation ...\n",
           fourier->dim,
           ((fourier->type&FWD)?'f':'b'),
           ((fourier->type&DFT)?'d':'f'));
@@ -104,13 +112,13 @@ int fourier_calc(t_fourier *fourier) {
         fourier_dft_3d(fourier);
         break;
       default:
-        fprintf(fourier->outfd,"[fourier] dimension failure\n");
+        dprintf(fourier->outfd,"[fourier] dimension failure\n");
         return F_DIM_FAILURE;
     }
     return F_SUCCESS;
   }
   else {
-    fprintf(fourier->outfd,"[fourier] fft not supported by now\n");
+    dprintf(fourier->outfd,"[fourier] fft not supported by now\n");
     return F_NOT_SUPPORTED;
   }
 } 
index f3cab6b27977d56ccba1d2e50b0968afc9f60ffa..39aee41ce5ba294239b47a8bf60c43dca19734cf 100644 (file)
@@ -4,7 +4,10 @@
 #define FOURIER_H
 
 /* includes */
+#define _GNU_SOURCE
 #include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
 
 /* defines */
 #define F_SUCCESS 1
@@ -36,6 +39,8 @@ typedef struct s_fourier {
 /* function prototypes */
 int fourier_init(t_fourier *fourier,int outfd);
 int fourier_dft_1d(t_fourier *fourier);
+int fourier_dft_2d(t_fourier *fourier);
+int fourier_dft_3d(t_fourier *fourier);
 int fourier_calc(t_fourier *fourier);
 
 #endif