int main (int argc, char **argv) { char **argv_new=NULL; int i, argc_test; plan_tests(5); { char *argv_test[] = {"prog_name", (char *) NULL}; argc_test=1; char *argv_known[] = {"prog_name", (char *) NULL}; argv_new=np_extra_opts(&argc_test, argv_test, "check_disk"); ok(array_diff(argc_test, argv_new, 1, argv_known), "No opts, returns correct argv/argc"); my_free(&argc_test, argv_new, argv_test); } { char *argv_test[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL}; argc_test=5; char *argv_known[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL}; argv_new=np_extra_opts(&argc_test, argv_test, "check_disk"); ok(array_diff(argc_test, argv_new, 5, argv_known), "No extra opts, verbatim copy of argv"); my_free(&argc_test, argv_new, argv_test); } { char *argv_test[] = {"prog_name", "--extra-opts=@./config-opts.ini", (char *) NULL}; argc_test=2; char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", (char *) NULL}; argv_new=np_extra_opts(&argc_test, argv_test, "check_disk"); ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts using default section"); my_free(&argc_test, argv_new, argv_test); } { char *argv_test[] = {"prog_name", "--extra-opts=sect1@./config-opts.ini", "--extra-opts", "sect2@./config-opts.ini", (char *) NULL}; argc_test=4; char *argv_known[] = {"prog_name", "--one=two", "--something else=oops", "--this=that", (char *) NULL}; argv_new=np_extra_opts(&argc_test, argv_test, "check_disk"); ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts specified twice"); my_free(&argc_test, argv_new, argv_test); } { char *argv_test[] = {"prog_name", "--arg1=val1", "--extra-opts=@./config-opts.ini", "--extra-opts", "sect1@./config-opts.ini", "--arg2", (char *) NULL}; argc_test=6; char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", "--one=two", "--arg1=val1", "--arg2", (char *) NULL}; argv_new=np_extra_opts(&argc_test, argv_test, "check_disk"); ok(array_diff(argc_test, argv_new, 7, argv_known), "twice extra opts using two sections"); my_free(&argc_test, argv_new, argv_test); } return exit_status(); }
/* * Does a single fwd+bwd fft of size n and checks it against python. * To test fftw replace fft_mkl with fft_fftw and set flag to MKL_ALIGN. */ void test_mkl(int n, enum mkl_align_flag flag){ double *space = (double *)_mm_malloc((4*n+2)*sizeof(double), 64); double *v; switch(flag){ case MKL_ALIGN: v = space; break; case MKL_NOALIGN: v = space+1; break; } double *w = v + 2*n; for(int i=0; i < n; i++){ w[2*i] = v[2*i] = rand()*1.0/RAND_MAX - 0.5; w[2*i+1] = v[2*i+1] = rand()*1.0/RAND_MAX - 0.5; } verify_dir("DBG/"); array_out(v, 2, n, "DBG/v.dat"); fft_mkl fft(n); fft.fwd(v); array_out(v, 2, n, "DBG/vf.dat"); system("test_fft.py DBG/v.dat DBG/vf.dat"); fft.bwd(v); array_diff(v, w, 2*n); double rerror = array_max(v, 2*n)/array_max(w, 2*n); std::cout<<"\n\tfwd+bwd error in complex mkl 1D fft"<<std::endl; std::cout<<"\tn = "<<n<<std::endl; std::cout<<"\trel error = "<<rerror<<std::endl; _mm_free(space); }
//compute the best coefficient of sine function struct sin_coeff find_best_coeff(short channel1[], unsigned channel_diff, unsigned sub_samples, unsigned sample_rate) { int i = 0, j = 0, k = 0; int minErrdiff1, minj1, mink1; struct sin_coeff r; unsigned total_samples = sub_samples * 4; short * error = (short *) malloc (total_samples); //store the data of error(subchannel - sine_data) short * sine = (short *) malloc (total_samples); /*int minA, maxA, minO, maxO; printf("please enter the range of coefficient for sine wave:<usage: minimum-amptitude maximum-amptitude minimum-Omega maximum Omega.>\n"); scanf("%d %d %d %d", &minA, &maxA, &minO, &maxO); printf("your setting is:minA%d,maxA%d,mimO%d,max%d.\n",minA,maxA,minO,maxO); fflush(stdin);*/ for (k = -40; k < 40; k++){ for(j = (-40); j < 40 ; j++){ for(i = 0; i < sub_samples; i++) { sine[i] = 0.05 * j * channel_diff * sin( 2 * PI * sample_rate * i * k); error[i] = channel1[i] - sine[i]; }// j domin if (j == (-20) && k == 1){minErrdiff1 = array_diff( error, sub_samples);} else if ( minErrdiff1 > array_diff( error, sub_samples)){ minErrdiff1 = array_diff( error, sub_samples); minj1 = j; mink1 = k; } }//k domin } printf("Errintdiff is %d, minA is %d, minOmega is %d\n", minErrdiff1, minj1, mink1); r.A = minj1; r.omega = mink1; return r; }
void test_nr(int n){ double *space = new double[4*n]; double *v = space; double *w = space + 2*n; for(int i=0; i < n; i++){ w[2*i] = v[2*i] = rand()*1.0/RAND_MAX - 0.5; w[2*i+1] = v[2*i+1] = rand()*1.0/RAND_MAX - 0.5; } fft_mkl fft(n); nrbwd(v, n); fft.bwd(w); array_diff(v, w, 2*n); std::cout<<"\n\tNR error relative to MKL"<<std::endl; std::cout<<"\tn = "<<n<<std::endl; std::cout<<"\trel error = "<<array_max(v, 2*n)/array_max(w,2*n) <<std::endl; delete[] space; }
/** * @brief Read in a list of array and check for each of the arrays whether they are in LMC form or not * @param argc * @param argv[] * @return */ int main (int argc, char *argv[]) { char *fname; int correct = 0, wrong = 0; /* parse command line options */ AnyOption opt; opt.setFlag ("help", 'h'); /* a flag (takes no argument), supporting long and short form */ opt.setOption ("loglevel", 'l'); opt.setOption ("oaconfig", 'c'); opt.setOption ("check"); opt.setOption ("strength", 's'); opt.setOption ("prune", 'p'); opt.setOption ("output", 'o'); opt.setOption ("mode", 'm'); opt.addUsage ("oacheck: Check arrays for LMC form or reduce to LMC form"); opt.addUsage ("Usage: oacheck [OPTIONS] [ARRAYFILE]"); opt.addUsage (""); opt.addUsage (" -h --help Prints this help"); opt.addUsage (" -l [LEVEL] --loglevel [LEVEL] Set loglevel to number"); opt.addUsage (" -s [STRENGTH] --strength [STRENGTH] Set strength to use in checking"); std::string ss = " --check [MODE] Select mode: "; ss += printfstring ("%d (default: check ), %d (reduce to LMC form), %d (apply random transformation and reduce)", MODE_CHECK, MODE_REDUCE, MODE_REDUCERANDOM); // for (int i = 3; i < ncheckopts; ++i) ss += printfstring (", %d (%s)", static_cast< checkmode_t > (i), modeString ((checkmode_t)i).c_str ()); opt.addUsage (ss.c_str ()); opt.addUsage (" -o [OUTPUTFILE] Output file for LMC reduction"); opt.processCommandArgs (argc, argv); algorithm_t algmethod = (algorithm_t)opt.getIntValue ("mode", MODE_AUTOSELECT); int loglevel = NORMAL; if (opt.getValue ("loglevel") != NULL || opt.getValue ('l') != NULL) loglevel = atoi (opt.getValue ('l')); // set custom loglevel setloglevel (loglevel); if (checkloglevel (QUIET)) { print_copyright (); } if (opt.getFlag ("help") || opt.getFlag ('h') || opt.getArgc () == 0) { opt.printUsage (); return 0; } checkmode_t mode = MODE_CHECK; if (opt.getValue ("check") != NULL) mode = (checkmode_t)atoi (opt.getValue ("check")); // set custom loglevel if (mode >= ncheckopts) mode = MODE_CHECK; logstream (QUIET) << "#time start: " << currenttime () << std::endl; double t = get_time_ms (); t = 1e3 * (t - floor (t)); srand (t); int prune = opt.getIntValue ('p', 0); fname = opt.getArgv (0); setloglevel (loglevel); int strength = opt.getIntValue ('s', 2); if (strength < 1 || strength > 10) { printf ("Strength specfied (t=%d) is invalid\n", strength); exit (1); } else { log_print (NORMAL, "Using strength %d to increase speed of checking arrays\n", strength); } arraylist_t outputlist; char *outputfile = 0; bool writeoutput = false; if (opt.getValue ("output") != NULL) { writeoutput = true; outputfile = opt.getValue ('o'); } arrayfile_t *afile = new arrayfile_t (fname); if (!afile->isopen ()) { printf ("Problem opening %s\n", fname); exit (1); } logstream (NORMAL) << "Check mode: " << mode << " (" << modeString (mode) << ")" << endl; /* start checking */ double Tstart = get_time_ms (), dt; int index; array_link al (afile->nrows, afile->ncols, -1); for (int i = 0; i < afile->narrays; i++) { index = afile->read_array (al); array_t *array = al.array; arraydata_t arrayclass = arraylink2arraydata (al, 0, strength); arrayclass.lmc_overflow_check (); OAextend oaextend; lmc_t result = LMC_NONSENSE; LMCreduction_t *reduction = new LMCreduction_t (&arrayclass); LMCreduction_t *randtest = new LMCreduction_t (&arrayclass); array_link test_arraylink = al.clone(); array_t *testarray = test_arraylink.array; randtest->transformation->randomize (); reduction->setArray ( al); /* variables needed within the switch statement */ switch (mode) { case MODE_CHECK: { /* LMC test with reduction code */ reduction->mode = OA_TEST; result = LMCcheck(al, arrayclass, oaextend, *reduction); break; } case MODE_CHECKJ4: { /* LMC test with special code */ reduction->mode = OA_TEST; reduction->init_state = COPY; oaextend.setAlgorithm (MODE_J4, &arrayclass); result = LMCcheck (al, arrayclass, oaextend, *reduction); break; } case MODE_REDUCEJ4: { /* LMC test with special code */ reduction->mode = OA_REDUCE; reduction->setArray (al); result = LMCcheckj4 (al, arrayclass, *reduction, oaextend); break; } case MODE_CHECK_SYMMETRY: { myprintf("MODE_CHECK_SYMMETRY not supported any more"); exit(1); } case MODE_CHECKJ5X: { oaextend.setAlgorithm (MODE_J5ORDERX, &arrayclass); /* LMC test with special code */ reduction->mode = OA_TEST; reduction->init_state = COPY; result = LMCcheck (al, arrayclass, oaextend, *reduction); break; } case MODE_CHECKJ5XFAST: { oaextend.setAlgorithm (MODE_J5ORDERX, &arrayclass); /* LMC test with special code */ reduction->mode = OA_TEST; reduction->init_state = COPY; if (1) { array_link al (array, arrayclass.N, arrayclass.ncols, al.INDEX_NONE); reduction->updateSDpointer (al); } result = LMCcheck (al, arrayclass, oaextend, *reduction); break; } case MODE_REDUCEJ5X: { OAextend oaextend; oaextend.setAlgorithm (MODE_J5ORDERX, &arrayclass); /* LMC test with special code */ printf ("oacheck: WARNING: MODE_CHECKJ5X: untested code, not complete\n"); reduction->mode = OA_REDUCE; result = LMCcheck (al, arrayclass, oaextend, *reduction); break; } case MODE_REDUCE: /* LMC reduction */ { reduction->mode = OA_REDUCE; copy_array(array, reduction->array, arrayclass.N, arrayclass.ncols); dyndata_t dynd = dyndata_t(arrayclass.N); result = LMCreduction(array, array, &arrayclass, &dynd, reduction, oaextend); break; } case MODE_REDUCERANDOM: { /* random LMC reduction */ oaextend.setAlgorithm(MODE_ORIGINAL, &arrayclass); reduction->mode = OA_REDUCE; randtest->transformation->apply(array, testarray); copy_array(testarray, reduction->array, arrayclass.N, arrayclass.ncols); reduction->init_state = INIT; array_link alp = arrayclass.create_root(arrayclass.ncols, 1000); reduction->setArray(alp); dyndata_t dynd = dyndata_t(arrayclass.N); result = LMCreduction(testarray, testarray, &arrayclass, &dynd, reduction, oaextend); if (log_print(NORMAL, "")) { reduction->transformation->show(); } } break; case MODE_REDUCETRAIN: /* LMC reduction with train*/ reduction->mode = OA_REDUCE; result = LMCreduction_train (al, &arrayclass, reduction, oaextend); break; case MODE_REDUCETRAINRANDOM: /* random LMC reduction with train*/ reduction->mode = OA_REDUCE; randtest->transformation->apply (array, testarray); result = LMCreduction_train (test_arraylink, &arrayclass, reduction, oaextend); break; case MODE_HADAMARD: myprintf("MODE_HADAMARD not supported any more\n"); exit(1); default: result = LMC_NONSENSE; std::cout << "function " << __FUNCTION__ << "line " << __LINE__ << "Unknown mode" << std::endl; exit (1); break; } bool aequal; switch (mode) { case MODE_CHECK: if (result == LMC_LESS) { ++wrong; log_print (NORMAL, "Found array nr %i/%i NOT in lmc form:\n", i, afile->narrays); } else { ++correct; log_print (NORMAL, "Found array nr %i/%i in lmc form.\n", i, afile->narrays); } break; case MODE_CHECKJ4: case MODE_CHECKJ5X: case MODE_CHECKJ5XFAST: case MODE_CHECK_SYMMETRY: if (result == LMC_LESS) { ++wrong; log_print (NORMAL, "Found array nr %i/%i NOT in minimal form:\n", i, afile->narrays); } else { ++correct; log_print (NORMAL, "Found array nr %i/%i in minimal form.\n", i, afile->narrays); } break; case MODE_REDUCE: case MODE_REDUCEJ4: case MODE_REDUCEJ5X: case MODE_REDUCETRAIN: aequal = std::equal (array, array + arrayclass.N * arrayclass.ncols, reduction->array); if ((!aequal) || reduction->state == REDUCTION_CHANGED) { ++wrong; log_print (QUIET, "Reduced array %i/%i to lmc form.\n", i, afile->narrays); if (checkloglevel (NORMAL)) { log_print (QUIET, "Original:\n"); print_array (array, afile->nrows, afile->ncols); print_array ("Reduction:\n", reduction->array, afile->nrows, afile->ncols); printf ("---------------\n"); } if (checkloglevel (DEBUG)) { cout << "Transformation: " << endl; reduction->transformation->show (cout); rowindex_t row; colindex_t column; array_diff (array, reduction->array, arrayclass.N, arrayclass.ncols, row, column); cout << "Difference at: "; printf (" row %d, col %d\n", row, column); } } else { ++correct; log_print (QUIET, "Array %i/%i was already in lmc form:\n", i, afile->narrays); } break; case MODE_REDUCETRAINRANDOM: case MODE_REDUCERANDOM: case MODE_REDUCEJ4RANDOM: aequal = std::equal (array, array + arrayclass.N * arrayclass.ncols, reduction->array); if (!aequal) { ++wrong; log_print (SYSTEM, "Array %i/%i: reduced random transformation not equal to original (BUG!)\n", i, afile->narrays); if (checkloglevel (NORMAL)) { print_array ("Original:\n", array, afile->nrows, afile->ncols); print_array ("Randomized:\n", testarray, afile->nrows, afile->ncols); print_array ("Reduction:\n", reduction->array, afile->nrows, afile->ncols); printf ("---------------\n"); } } else { ++correct; log_print (QUIET, "Array %i/%i: reduced random transformation to original\n", i, afile->narrays); } break; case MODE_HADAMARD: break; default: std::cout << "function " << __FUNCTION__ << "line " << __LINE__ << "unknown mode" << std::endl; break; } array_link al = array_link ((carray_t *)reduction->array, afile->nrows, afile->ncols, -1); if (result == LMC_MORE || !prune) { outputlist.push_back (al); } delete randtest; delete reduction; destroy_array (testarray); } afile->closefile (); delete afile; if (writeoutput) { logstream (NORMAL) << "Writing output (" << outputlist.size () << " arrays)" << endl; writearrayfile (outputfile, outputlist); } logstream (QUIET) << "#time end: " << currenttime () << std::endl; return 0; }
int main() { int i = 0; int main_choice; struct sin_coeff sin_coe1; struct sin_coeff sin_coe2; struct sin_coeff sin_coe3; struct sin_coeff sin_coe4; char * infile_name; FILE * fp; char * buffer = malloc(sizeof(char)* 1024 * 2 * 8); printf("***Welcome to eeg data testing system!!!***\n"); printf("1. read a wav file.\n") ; printf("2. check the data of loaded wav file.\n"); printf("3. set a sin wav.\n"); printf("4. write the new file\n"); printf("******************************************\n"); scanf("%d", &main_choice); safe_flush(stdin); printf("your choice: %d!\n", main_choice); printf("\n"); if (main_choice == 1) { infile_name = "4cmotion.wav"; if(( fp = fopen(infile_name, "rb")) == NULL){ fprintf(stderr, "ERROR: cannot open file: %s!\n", infile_name); return 1; } if( fread(buffer, 1, 44, fp) != 44 || memcmp(buffer, "RIFF", 4) || memcmp(buffer + 8, "WAVEfmt ", 8) || memcmp(buffer + 36, "data", 4) ){ fprintf(stderr, "This file is not wav file file! Check the header\n"); fclose(fp); return 1; } // unsigned bps = 16; //read chunk size /* unsigned * pos = (unsigned *) malloc (4); fseek(fp, 4, SEEK_SET); fread(pos, 1, 4, fp); unsigned chunksize = array2data(pos, 4); printf("chunksize is %u\n",chunksize); fflush(stdout); fflush(stdin); safe_flush(stdout); safe_flush(stdin); */ //read channel number unsigned * pos =(unsigned *) malloc(4); fseek(fp, 22, SEEK_SET); fread(pos, 2, 1, fp); unsigned channel = array2data(pos,2); printf("channel number: %u\n", channel); //read sample rate number fseek(fp, 24, SEEK_SET); fread(pos, 4, 1, fp); unsigned sample_rate = array2data( pos, 4); printf("sample rate is %u\n", sample_rate); //read total samples fseek(fp, 40, SEEK_SET); fread(pos, 1, 4, fp); unsigned total_samples = array2data(pos, 4) / 2; printf("total samples is %u\n", total_samples); free(pos); //sleep(1); //data store section short * data_store = (short *)malloc(total_samples * 4); fseek(fp, 44, SEEK_SET); if ( fread(data_store, 2, total_samples, fp) == 0) { printf("I can't read any data from wav file!\n"); } fclose(fp); fflush(fp); /* for( i = 0; i < total_samples; i++) { printf("The data_store[%u] is %hd\n", i, data_store[i]); }*/ unsigned sub_samples = total_samples / 4; short * channel1_data = (short *)malloc(total_samples); short * channel2_data = (short *)malloc(total_samples); short * channel3_data = (short *)malloc(total_samples); short * channel4_data = (short *)malloc(total_samples); short * channel_bakup = (short *)malloc(total_samples*2); for( i = 0; i < total_samples; i++) { if(i < sub_samples) { channel1_data[i] = data_store[i*4]; channel2_data[i] = data_store[i*4+1]; channel3_data[i] = data_store[i*4+2]; channel4_data[i] = data_store[i*4+3]; } channel_bakup[i] = data_store[i]; } //write and generate full(contains 4 channels) wav file char * outfile_full = "4cfull.wav"; write_wav(outfile_full, sub_samples, channel_bakup, sample_rate, channel); //find max and min value in subchannels unsigned channel1_diff = array_diff( channel1_data, sub_samples); unsigned channel2_diff = array_diff( channel2_data, sub_samples); unsigned channel3_diff = array_diff( channel3_data, sub_samples); unsigned channel4_diff = array_diff( channel4_data, sub_samples) ; printf("The channel1_diff is %u, channel2 %u, channel3 %u, channel4 %u\n", channel1_diff,channel2_diff, channel3_diff, channel4_diff); //sleep(3); //generate sine wave //short * sine_data = (short *) malloc (total_samples); sin_coe1 = find_best_coeff(channel1_data, channel1_diff, sub_samples, sample_rate); sin_coe2 = find_best_coeff(channel2_data, channel2_diff, sub_samples, sample_rate); sin_coe3 = find_best_coeff(channel3_data, channel3_diff, sub_samples, sample_rate); sin_coe4 = find_best_coeff(channel4_data, channel4_diff, sub_samples, sample_rate); printf("LOOK at main values:\n"); printf("minA1 is %d, minO1 is %d\n", sin_coe1.A, sin_coe1.omega); printf("minA2 is %d, minO2 is %d\n", sin_coe2.A, sin_coe2.omega); printf("minA3 is %d, minO3 is %d\n", sin_coe3.A, sin_coe3.omega); printf("minA4 is %d, minO4 is %d\n", sin_coe4.A, sin_coe4.omega); //short * sine_wave = (short *) malloc (total_samples); short * final_data = (short *) malloc (total_samples); int mark = 0; for ( i = 0; i < sub_samples; i++) { mark = i; final_data[mark] = channel1_data[i] - (0.05 * sin_coe1.A * channel1_diff) * sin( 2 * PI * sample_rate * i * sin_coe1.omega); final_data[mark+1] = channel2_data[i] - (0.05 * sin_coe2.A * channel2_diff) * sin( 2 * PI * sample_rate * i * sin_coe2.omega); final_data[mark+2] = channel3_data[i] - (0.05 * sin_coe3.A * channel3_diff) * sin( 2 * PI * sample_rate * i * sin_coe3.omega); final_data[mark+3] = channel4_data[i] - (0.05 * sin_coe4.A * channel4_diff) * sin( 2 * PI * sample_rate * i * sin_coe4.omega); } printf("mark is %d",mark); char * newfile = "4cnew.wav"; write_wav(newfile, sub_samples, final_data, sample_rate, channel); /* for(i = 0; i < sub_samples; i++) { error_data[i] = channel1_data[i] - sine_data[i]; printf("error data[%u] is %hu\n", i, error_data[i]); } */ //int error_diff = array_diff( error_data, sub_samples); //printf("error diff is %u, channel1_diff is %u\n", error_diff, channel1_diff); } return 1; }