/***************************************************************************** Major portions of this software are copyrighted by the Medical College of Wisconsin, 1994-2000, and are released under the Gnu General Public License, Version 2. See the file README.Copyright for details. ******************************************************************************/ /* This file is #included by the filtering program 3dFourier and the plugin plug_fourier */ /* By T. Ross and K. Heimerl 8-99 */ static void *My_Malloc( size_t size) { void *ptr=NULL; ptr = (void *)malloc(size); if (ptr == NULL) { #ifdef IN_FOURIER_PLUGIN fprintf(stderr, "Fatal error in Fourier Filter Driver, malloc returned NULL"); exit(1); #else Error_Exit("Fatal: malloc returned NULL"); #endif } return ptr; } /************************************************************************************** This function zero pads a signal to a power of 2 * factor of 3 or 5 then FFT's the data. The zero-padded data are FFT'd, and the resultant spectrum is multiplied by an ideal low or high pass window. The signal is then inverse FFt'd and the zero padded portion of the signal is eliminated. **************************************************************************************/ static char *filter(float *ORIG_SIG, float low_fc, float high_fc, int N, float period, int ignore, int retrend, int transform) { /************************************************************************************** DECLARE and INITIALIZE VARIABLES **************************************************************************************/ float sum, meanorig, mean_new, Fs, slope, inter; int dummy, i,j,power,padded_N, M; int Ncutlo1,Ncutlo2,Ncuthi1,Ncuthi2; FILE *outfile; complex *NEW_SIG, *newfft, *win_fft, *win_low, *win_high; /*typedef'd by B Cox */ complex *restored_sig, *final_sig, sumcx, meanfinal; static char msg[256] ; /* RWCox */ /************************************************************************************* DETREND ORIGINAL DATA **************************************************************************************/ if (ignore>=N) { if (!transform) return "You cannot ignore all of the data"; else high_fc = 1000; /* fine, make it an all-stop */ } else { for (i=0; i<(N-ignore); i++) ORIG_SIG[i] = ORIG_SIG[i+ignore]; N -= ignore; } /* Detrend the data, setting the end points to zero */ slope = ((float)ORIG_SIG[N-1] - (float)ORIG_SIG[0] ) / ((float) N - 1.0); inter = (float)ORIG_SIG[0]; for (i=0; i= Fs) || (low_fc == 0.0)) { if ((low_fc >= Fs) && (!transform)) { sprintf(msg,"Lowpass filter is all-pass since cutoff=%f > Fsample=%f (Mfft=%d)\n", low_fc , Fs , M ) ; return msg ; } for(i=0;i 0) && (low_fc < (Fs/(float)M) ) ) { if (!transform) { sprintf(msg,"Lowpass filter is no-pass since cutoff=%f < Fsample=%f / Mfft=%d\n", low_fc , Fs , M ) ; return msg ; } for(i=0;i= Fs) { if (!transform) { sprintf(msg,"Highpass filter is all-stop since cutoff=%f > Fsample=%f (Mfft=%d)\n", high_fc , Fs , M ) ; return msg ; } for(i=0;i=ignore; i--) ORIG_SIG[i]=ORIG_SIG[i-ignore]; for (i=0; i