static int guess_loops(double overhead, Benchmark* bench, SkCanvas* canvas) { WallTimer timer; // Measure timer overhead and bench time together. do { timer.start(); bench->draw(1, canvas); safe_flush(canvas); timer.end(); } while (timer.fWall < overhead); // Shouldn't normally happen. // Later we'll just start and stop the timer once, but loop N times. // We'll pick N to make timer overhead negligible: // // Timer Overhead // ------------------------------- < FLAGS_overheadGoal // Timer Overhead + N * Bench Time // // where timer.fWall ≈ Timer Overhead + Bench Time. // // Doing some math, we get: // // (Timer Overhead / FLAGS_overheadGoal) - Timer Overhead // ----------------------------------------------------- < N // (timer.fWall - Timer Overhead) // // Luckily, this also works well in practice. :) const double numer = overhead / FLAGS_overheadGoal - overhead; const double denom = timer.fWall - overhead; return (int)ceil(numer / denom); }
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; }
int tool_main(int argc, char** argv) { SetupCrashHandler(); SkAutoGraphics ag; SkCommandLineFlags::Parse(argc, argv); const double overhead = estimate_timer_overhead(); if (FLAGS_verbose) { // No header. } else if (FLAGS_quiet) { SkDebugf("min\tbench\tconfig\n"); } else { SkDebugf("loops\tmin\tmean\tmax\tstddev\tbench\tconfig\n"); } for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) { SkAutoTDelete<Benchmark> bench(r->factory()(NULL)); if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { continue; } SkTDArray<SkSurface*> surfaces; SkTDArray<const char*> configs; create_surfaces(bench.get(), &surfaces, &configs); bench->preDraw(); for (int j = 0; j < surfaces.count(); j++) { SkCanvas* canvas = surfaces[j] ? surfaces[j]->getCanvas() : NULL; const char* config = configs[j]; bench->draw(1, canvas); // Just paranoid warmup. safe_flush(canvas); const int loops = guess_loops(overhead, bench.get(), canvas); SkAutoTMalloc<double> samples(FLAGS_samples); WallTimer timer; for (int i = 0; i < FLAGS_samples; i++) { timer.start(); bench->draw(loops, canvas); safe_flush(canvas); timer.end(); samples[i] = timer.fWall / loops; } Stats stats(samples.get(), FLAGS_samples); if (FLAGS_verbose) { for (int i = 0; i < FLAGS_samples; i++) { SkDebugf("%s ", humanize(samples[i]).c_str()); } SkDebugf("%s\n", bench->getName()); } else if (FLAGS_quiet) { if (configs.count() == 1) { config = ""; // Only print the config if we run the same bench on more than one. } SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->getName(), config); } else { const double stddev_percent = 100 * sqrt(stats.var) / stats.mean; SkDebugf("%d\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n" , loops , humanize(stats.min).c_str() , humanize(stats.mean).c_str() , humanize(stats.max).c_str() , stddev_percent , bench->getName() , config ); } } surfaces.deleteAll(); } return 0; }