00001
00002
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <assert.h>
00006 #include <string.h>
00007 #include <complex.h>
00008 #include <math.h>
00009 #include "ctypes.h"
00010 #include "detrend_C99.h"
00011
00012
00013
00014
00015 #ifdef FLOAT
00016 #define TYPE FLOAT
00017 #include "detrend_code_C99.h"
00018 #undef TYPE
00019 #endif
00020
00021 #ifdef DOUBLE
00022 #define TYPE DOUBLE
00023 #include "detrend_code_C99.h"
00024 #undef TYPE
00025 #endif
00026
00027 #ifdef COMPLEXFLOAT
00028 static int count2=0;
00029 void cdetrend_discontig( int n, _Complex float *data, int *isgood,
00030 int degree, int length, int skip,
00031 int m, int *sect, int detrend_first)
00032 {
00033 int i,first,last;
00034
00035
00036
00037
00038
00039
00040 if (m==0)
00041 cdetrend(n,data,isgood,degree,length,skip,detrend_first);
00042 else
00043 {
00044 for (i=0; i<m; i++)
00045 {
00046 first = sect[2*i];
00047 last = sect[2*i+1];
00048 #ifdef DEBUGOUT
00049 printf("Detrending [%d:%d]\n",first,last);
00050 #endif
00051 cdetrend(last-first+1,&data[first],&isgood[first],degree,length,skip,detrend_first);
00052 }
00053 }
00054 }
00055
00056 void cdetrend( int n, _Complex float *data, int *isgood, int degree,
00057 int length, int skip, int detrend_first)
00058 {
00059 int i;
00060
00061 float *real, *imag;
00062 real = (float *) malloc(n*sizeof(float));
00063 for (i=0;i<n;i++)
00064 real[i] = crealf(data[i]);
00065 sdetrend( n, real, isgood, degree, length, skip, detrend_first);
00066
00067 imag = (float *) malloc(n*sizeof(float));
00068 for (i=0;i<n;i++)
00069 imag[i] = cimagf(data[i]);
00070 sdetrend( n, imag, isgood, degree, length, skip, detrend_first);
00071 for (i=0;i<n;i++)
00072 data[i] = real[i] + _Complex_I*imag[i];
00073
00074 #ifdef DEBUGFILE
00075 {
00076 FILE *fh;
00077 char name[1024];
00078 printf("writing debug2 file #%d\n",count2);
00079 sprintf(name,"detrend_debug2_%d.out",count2++);
00080 fh = fopen(name,"w");
00081 assert(fh);
00082 for (i=0; i<n; i++)
00083 fprintf(fh,"%e %e\n",crealf(data[i]),cimagf(data[i]));
00084 fclose(fh);
00085 }
00086 #endif
00087
00088 free(real);
00089 free(imag);
00090 }
00091 #endif
00092
00093
00094 #ifdef COMPLEXDOUBLE
00095 void zdetrend_discontig( int n, _Complex double *data, int *isgood,
00096 int degree, int length, int skip,
00097 int m, int *sect, int detrend_first)
00098 {
00099 int i,first,last;
00100
00101
00102
00103
00104
00105
00106 if (m==0)
00107 zdetrend(n,data,isgood,degree,length,skip,detrend_first);
00108 else
00109 {
00110 first = 0;
00111 for (i=0; i<m; i++)
00112 {
00113 first = sect[2*i];
00114 last = sect[2*i+1];
00115 zdetrend(last-first+1,&data[first],&isgood[first],degree,length,skip,detrend_first);
00116 }
00117 }
00118 }
00119
00120
00121 void zdetrend( int n, _Complex double *data, int *isgood, int degree,
00122 int length, int skip, int detrend_first)
00123 {
00124 int i;
00125 double *real, *imag;
00126
00127 real = (double *) malloc(n*sizeof(double));
00128 for (i=0;i<n;i++)
00129 real[i] = creal(data[i]);
00130 ddetrend( n, real, isgood, degree, length, skip, detrend_first);
00131
00132 imag = (double *) malloc(n*sizeof(double));
00133 for (i=0;i<n;i++)
00134 imag[i] = cimag(data[i]);
00135 ddetrend( n, imag, isgood, degree, length, skip, detrend_first);
00136 for (i=0;i<n;i++)
00137 data[i] = real[i] + _Complex_I*imag[i];
00138
00139 free(real);
00140 free(imag);
00141 }
00142 #endif