//File: setieasy_v133b.cpp //Source by: // Robert L. Samuell IV // Robert A. Lodder Ph.D // Dan Kennedy // Kah-Siew Ho // //Software Name: SETIEasy //Version: 1.33b //Last Modified: Friday, March 30, 2001 #include #include #include #include #include #include #include #include #define SWAP(a,b) tempr=(a); (a)=(b); (b)=tempr class Statpack1{ public: //CONSTRUCTOR Statpack1(); //FUNCTIONS FOR FILES char* string_to_Cstring(string s1); string Cstring_to_string(char* s1); char* get_input_filename(int num); char* get_outData_filename(); char* get_grafData_filename(); char* get_factor(); void set_input_filename(char* fname1, char* fname2); void open_input_file(); void reopen_input_file(); unsigned long get_nChunkSize(FILE* &infile); void open_output_file(char *outData, char *graphData); void close_input_file(); void close_output_file() { fout.close(); gout.close(); } void print_bit(); void put_output_file_header(); void put_output_file_footer(time_t *TIME); void put_dechirp_factor_header(char* dechirp_factor, time_t *TIME); void print_fftblock_header(); void print_one_fft_header(); void hello(); //FUNCTIONS FOR ANALYSIS void initialize_rand(); void get_data_length(unsigned long n); void initialize_variables(); void initialize_data_set1(); void initialize_data_set2(); void delete_data_set1(){ delete [] data1; } void delete_data_set2(){ delete [] data2; } void initialize_train_set(); void delete_train_set(); void initialize_test_set(); void delete_test_set(); void set_shadow_dimensions(); void dfft(float data[],unsigned long n,int isign); void sizer_ft(float data[], unsigned long nn, int isign); float fsob(float **b_train, float *c_train, float **b_test, float *c_test); void sortarray(float array[], unsigned long n); void limits(); void find_confid(); void find_coeff(); float sdev(float *array, float mean, unsigned long n); float sumcols(float **a, unsigned long norows, unsigned long colnumber); void make_train_set(); void make_test_set(); void interpret_data(); void init_start_sample(); bool is_sample(); void next_sample(); void put_final_analysis(); void make_dechirped_file(char* dechirpFactor); unsigned long open_dechirped_file1(); unsigned long open_dechirped_file2(); unsigned long make_dechirped_file(unsigned long nChunkSize, FILE* &infile, FILE* &outfile); void close_dechirped_file1(); void close_dechirped_file2(); private: int unusual_signal; bool signal; double Factor; char *D_factor; string DFactor; char *in_file1, *in_file2, *out_file1, *out_file2; string vers; string filename1, filename2; FILE *infile1, *infile2, *outfile1, *outfile2; ofstream fout, gout; unsigned long b; unsigned long rows, cols; unsigned long start_sample; unsigned long end_sample; unsigned long data_len; float rc; float confid; float *data1, *data2; float **btn; float *ctn; float **btest; float *ctest; short int nBitsPerSample1, nBitsPerSample2; }; Statpack1::Statpack1(){ b=100; cols=8192; start_sample=0; rc=0.0; confid=0.0; signal = false; unusual_signal = 0; vers = "v1.33b beta"; } void Statpack1::hello(){ cout << "SETIEASY " << vers << " - Unusual Signal Detector" << endl; cout << "Copyright (c) 2000 University of Kentucky" << endl; } char* Statpack1::string_to_Cstring(string s1){ char* s2 = new char[s1.length()+1]; for(int i=0; i> temp; return string_to_Cstring(temp); } char* Statpack1::get_outData_filename(){ string temp; cout << "What do you want to name the outData file?" << endl; cin >> temp; return string_to_Cstring(temp); } char* Statpack1::get_grafData_filename(){ string temp; cout << "What do you want to name the grafData file?" << endl; cin >> temp; return string_to_Cstring(temp); } char* Statpack1::get_factor(){ string temp; cout << "What do you want to use as the dechirp factor?" << endl; cin >> temp; return string_to_Cstring(temp); } void Statpack1::initialize_variables(){ signal = false; unusual_signal = 0; start_sample=0; } unsigned long Statpack1::get_nChunkSize(FILE* &infile){ /* Scan in the 4 byte integer */ unsigned char ch; unsigned long nChunkSize = 0; for(int i=0; i<4; i++){ fscanf( infile, "%c", &ch); nChunkSize = (unsigned long) (nChunkSize + ch*pow(2, i*8)); } return nChunkSize; } void Statpack1::set_input_filename(char* fname1, char* fname2){ in_file1 = fname1; filename1 = Cstring_to_string(in_file1); in_file2 = fname2; filename2 = Cstring_to_string(in_file2); } void Statpack1::open_input_file(){ unsigned char ch; if( (infile1 = fopen( out_file1, "rb" )) == NULL ) { printf( "The source file %s was not opened\n", out_file1 ); exit(1); } else{ printf( "The source file %s was opened\n", out_file1 ); } if( (infile2 = fopen( out_file2, "rb" )) == NULL ) { printf( "The source file %s was not opened\n", out_file2 ); exit(1); } else{ printf( "The source file %s was opened\n", out_file2 ); } for (int i=0; i<44; i++) fscanf( infile1, "%c", &ch ); for (int j=0; j<44; j++) fscanf( infile2, "%c", &ch ); } void Statpack1::close_input_file(){ int Status; Status = fclose(infile1); if(Status == 0) printf("%s was closed\n", out_file1); else printf("%s did not close\n", out_file1); Status = fclose(infile2); if(Status == 0) printf("%s was closed\n", out_file2); else printf("%s did not close\n", out_file2); } void Statpack1::open_output_file(char *outData, char* graphData){ char *out_data; char *graph_data; string temp1, temp2; string D = "D"; string underscore = "_"; temp1 = D; temp1 += DFactor; temp1 += underscore; temp1 += Cstring_to_string(outData); out_data = string_to_Cstring(temp1); temp2 = D; temp2 += DFactor; temp2 += underscore; temp2 += Cstring_to_string(graphData); graph_data = string_to_Cstring(temp2); fout.open(out_data); if(fout.fail()){ cout << "Open outData file failed!" << endl; exit(1); } gout.open(graph_data); if(gout.fail()){ cout << "Open graphData file failed!" << endl; exit(1); } } void Statpack1::put_output_file_header(){ fout << "SETIEASY " << vers << " analysis of " << filename1 << " and " << filename2 << endl; } void Statpack1::put_output_file_footer(time_t *TIME){ fout << ctime(TIME); } void Statpack1::put_dechirp_factor_header(char* dechirp_factor, time_t *TIME){ fout << ctime(TIME); fout << "@~~~~~ Dechirp Factor: " << dechirp_factor << " ~~~~~@" << endl; } void Statpack1::initialize_rand(){ cout << "Initializing random number generator" << endl; srand((unsigned int) time(NULL)); } void Statpack1::get_data_length(unsigned long n){ data_len = (unsigned long) (pow(2,n)); } void Statpack1::print_bit(){ if (nBitsPerSample1 == 8) { cout << in_file1 << " and " << in_file2 << " are 8-bit wav files." << endl; fout << in_file1 << " and " << in_file2 << " are 8-bit wav files." << endl; } else if (nBitsPerSample1 == 16) { cout << in_file1 << " and " << in_file2 << " are 16-bit wav files." << endl; fout << in_file1 << " and " << in_file2 << " are 16-bit wav files." << endl; } } void Statpack1::initialize_data_set1(){ float sampleVal; unsigned char ch; cout << "Reading data set 1" << endl; data1 = new float[(2*data_len)+1]; if (nBitsPerSample1 == 8){ for(unsigned long index = 0; (feof(outfile1) == 0) && (index < data_len); index++) { /* Read in data array */ fscanf( infile1, "%1c", &ch ); data1[index] = ((float)ch-128)/128; } } if (nBitsPerSample1 == 16){ for(unsigned long index = 0; (feof(outfile1) == 0) && (index < data_len); index++) { /* Read in the low byte */ fscanf( infile1, "%1c", &ch ); sampleVal = (float)ch; /* Read in the high byte. */ fscanf( infile1, "%1c", &ch ); sampleVal = sampleVal + 256 * (float) ch; data1[index] = (sampleVal-32768)/32768; } } } void Statpack1::initialize_data_set2(){ float sampleVal; unsigned char ch; cout << "Reading data set 2" << endl; data2 = new float[(2*data_len)+1]; if (nBitsPerSample2 == 8){ for(unsigned long index = 0; (feof(outfile2) == 0) && (index < data_len); index++) { /* Read in data array */ fscanf( infile2, "%1c", &ch ); data2[index] = ((float)ch-128)/128; } } if (nBitsPerSample2 == 16){ for(unsigned long index = 0; (feof(outfile2) == 0) && (index < data_len); index++) { /* Read in the low byte */ fscanf( infile2, "%1c", &ch ); sampleVal = (float)ch; /* Read in the high byte. */ fscanf( infile2, "%1c", &ch ); sampleVal = sampleVal + 256 * (float) ch; data2[index] = (sampleVal-32768)/32768; } } } void Statpack1::print_fftblock_header(){ int mins; float sec; int sixty = 60; int samps_per_min = 480000; float fsamps_per_min = 480000.0; mins = data_len/samps_per_min; sec = ((data_len/fsamps_per_min)-float(data_len/samps_per_min))*sixty; cout << "Looking for signals " << mins << " mins and " << sec << " secs long." << endl; fout << "Signals for " << mins << " mins and " << sec << " secs long." << endl; } void Statpack1::print_one_fft_header(){ int mins; float sec; int sixty = 60; int samps_per_min = 480000; float fsamps_per_min = 480000.0; mins = start_sample /samps_per_min; sec = ((start_sample/fsamps_per_min)-float(start_sample/samps_per_min))*sixty; cout << "End with " << mins << " mins and " << sec << " secs" << endl; fout << "End with " << mins << " mins and " << sec << " secs" << endl; } void Statpack1::initialize_train_set(){ btn = new float*[b]; for(unsigned long i=0; i>1); if(isign == 1){ z_stack = -0.5; sizer_ft(data,n>>1,1); } else{ z_stack = 0.5; angles = -angles; } wtemp = sin(0.5*angles); w_progress = -2.0*wtemp*wtemp; pop_i = sin(angles); work = 1.0+w_progress; work_init = pop_i; np_complete = n+3; for(i=2;i<=(n>>2);i++){ closer_init=1+(init_temp=np_complete-(init_too=(1+(init_one=i+i-1)))); pop_1=end_flag*(data[init_one]+data[init_temp]); y_stack=end_flag*(data[init_too]-data[closer_init]); dummy_y= -z_stack*(data[init_too]+data[closer_init]); counter_3y=z_stack*(data[init_one]-data[init_temp]); data[init_one]=pop_1+work*dummy_y-work_init*counter_3y; data[init_too]=y_stack+work*counter_3y+work_init*dummy_y; data[init_temp]=pop_1-work*dummy_y+work_init*counter_3y; data[closer_init]= -y_stack+work*counter_3y+work_init*dummy_y; work=(wtemp=work)*w_progress-work_init*pop_i+work; work_init=work_init*w_progress+wtemp*pop_i+work_init; } if(isign == 1){ data[1]=((pop_1=data[1])+data[2]); data[2]=pop_1-data[2]; float dtemp=data[2]; for(start_flag=3;start_flag<=n;start_flag++) data[start_flag-1]=data[start_flag]; data[start_flag-1]=dtemp; } else{ data[1]=end_flag*((pop_1=data[1])+data[2]); data[2]=end_flag*(pop_1-data[2]); sizer_ft(data,n>>1,-1); } } void Statpack1::sizer_ft(float data[],unsigned long nn,int isign){ unsigned long n=0,g_max=0,m=0,j=0,istep=0,i=0; float work=0,work_init=0,wtemp=0,w_progress=0,pop_i=0,angles=0; float tempr=0,tempi=0; n=nn << 1; j=1; for(i=1;ii){ SWAP(data[j],data[i]); SWAP(data[j+1],data[i+1]); } m=n >> 1; while(m>=2 && j>m){ j -= m; m >>= 1; } j += m; } g_max=2; while(n>g_max){ istep = g_max << 1; angles = isign*(6.2831853071796/g_max); wtemp = sin(0.5*angles); w_progress = -2.0*wtemp*wtemp; pop_i = sin(angles); work = 1.0; work_init = 0.0; for(m=1;m>1)+1; indx_node=n; for(;;){ if(D_length>1){ d_nodes=array[--D_length]; } else{ d_nodes=array[indx_node]; array[indx_node]=array[1]; if(--indx_node == 1){ array[1]=d_nodes; break; } } i=D_length; j=D_length+D_length; while(j<=indx_node){ if(j < indx_node && array[j] < array[j+1]) j++; if(d_nodes < array[j]){ array[i]=array[j]; i=j; j <<= 1; }else break; } array[i]=d_nodes; } } void Statpack1::limits(){ float sd=0.0; float r=0.0, mean=0.0; unsigned long ncors=b; unsigned long i,j,m,n,p,s,pick; float *cors; float **check; float *chtest; cors = new float[ncors+1]; check = new float*[b]; for(i=0;i1.0 || Factor<-1.0) { cerr << "Dechirp factor must be in the range of [-1, 1]" << endl; exit(1); } D_factor = dechirpFactor; DFactor = Cstring_to_string(dechirpFactor); nChunkSize1 = open_dechirped_file1(); nChunkSize2 = open_dechirped_file2(); //To make sure both files have same nChunksize and nBitsPerSample if ( nBitsPerSample1 != nBitsPerSample2 ){ cerr << "Error, the two input files are not in the same bit!" << endl; exit(1); } if ( nChunkSize1 != nChunkSize2 ){ cerr << "WARNING: The two input files are not the same length!" << endl; cout << nChunkSize1 << ", " << nChunkSize2 << endl; exit(1); } cout << "Dechirping " << filename1 << endl; end_sample1 = make_dechirped_file(nChunkSize1, infile1, outfile1); close_dechirped_file1(); cout << "Dechirping " << filename2 << endl; end_sample2 = make_dechirped_file(nChunkSize2, infile2, outfile2); close_dechirped_file2(); if ( end_sample1 != end_sample2 ){ cerr << "WARNING: Something gone wrong with the algorithm!" << endl; cout << end_sample1 << ", " << end_sample2 << endl; exit(1); } else end_sample = end_sample1; } unsigned long Statpack1::make_dechirped_file(unsigned long nChunkSize, FILE* &infile, FILE* &outfile){ unsigned long byteCount, oldbyteCount; unsigned char ch, ch1, ch2; double slideFactor; oldbyteCount = 0; if (nBitsPerSample1 == 8){ for(unsigned long i=0; ioldbyteCount) { do { fprintf(outfile,"%c",ch); oldbyteCount = oldbyteCount+1; } while (byteCount > oldbyteCount); } } } else if (nBitsPerSample1 == 16){ for(unsigned long i=0; i<(nChunkSize/2); i++) { slideFactor = (i/(double)nChunkSize)*Factor; byteCount = (unsigned long) (i*(1+slideFactor)); fscanf( infile, "%1c", &ch1 ); fscanf( infile, "%1c", &ch2 ); if (byteCount>oldbyteCount) { do { fprintf(outfile,"%c",ch1); fprintf(outfile,"%c",ch2); oldbyteCount = oldbyteCount+1; } while (byteCount > oldbyteCount); } } } return oldbyteCount; } void Statpack1::close_dechirped_file1(){ int Status; Status = fclose(infile1); if(Status == 0) printf("%s was closed\n", in_file1); else printf("%s did not close\n", in_file1); Status = fclose(outfile1); if(Status == 0) printf("%s was closed\n", out_file1); else printf("%s did not close\n", out_file1); } void Statpack1::close_dechirped_file2(){ int Status; Status = fclose(infile2); if(Status == 0) printf("%s was closed\n", in_file2); else printf("%s did not close\n", in_file2); Status = fclose(outfile2); if(Status == 0) printf("%s was closed\n", out_file2); else printf("%s did not close\n", out_file2); } void analyze(Statpack1&, char*, char*, char*, char*, int, char**); void main(int argc, char* argv[]){ time_t *start_time, *end_time; int x; int y=0; int no_of_factors = 0; int no_of_commands = argc; char *dechirpFactor[300]; char *input_file1, *input_file2; char *outData_file; char *grafData_file; Statpack1 seti; seti.hello(); if (no_of_commands <= 6){ switch(no_of_commands){ case 6: dechirpFactor[0] = argv[5]; no_of_factors = 1; case 5: grafData_file = argv[4]; case 4: outData_file = argv[3]; case 3: input_file2 = argv[2]; case 2: input_file1 = argv[1]; } switch(no_of_commands){ case 1: input_file1 = seti.get_input_filename(1); case 2: input_file2 = seti.get_input_filename(2); case 3: outData_file = seti.get_outData_filename(); case 4: grafData_file = seti.get_grafData_filename(); case 5: dechirpFactor[0] = seti.get_factor(); no_of_factors = 1; case 6: analyze(seti, input_file1, input_file2, outData_file, grafData_file, no_of_factors, dechirpFactor); } } else{ input_file1 = argv[1]; input_file2 = argv[2]; outData_file = argv[3]; grafData_file = argv[4]; for ( x=5; x