/*--------------------------------------------------------------*/ int main (int argc, char *argv[]) /*--------------------------------------------------------------*/ { FILE *ff; char filename[300]; int day_num; // char filename1[300]; if(argc != 2) { printf("Usage: amp_length_normalize list_file_with_dayinfo\n"); exit(-1); } if((ff = fopen(argv[1], "r"))==NULL) { printf("Cannot open list.lst file. A file containing the names of"); printf(" all cut files for getting spectral SNR is required to run spectral_snr*.\n"); exit(1); } for(;;) { if(fscanf(ff,"%s %d",&(filename[0]),&day_num)==EOF) break; // sprintf(filename1,"/utera/tianye/data_ASN_TA/%s",filename); get_snr(filename,day_num); } fclose(ff); return 1; }
/*--------------------------------------------------------------*/ int main (int argc, char *argv[]) { static int n, npoints, nfin, nfout1, nfout2, ierr, nprpv; static double t0, dt, delta, vmin, vmax, tmin, tmax; static double snr, tresh, ffact, perc, taperl,fmatch,piover4; static float sei[16384], sei_p[32768], sei_n[16384]; static double arr1[100][8],arr2[100][7]; static double c_per[100],g_vel[100],amp_p[100],amp_n[100]; static double tamp, ampo[32][32768], pred[2][300]; static int nrow, ncol, npred; static double prpvper[300],prpvvel[300]; // phase vel prediction files double snr_p[64], snr_n[64]; double f1,f2,f3,f4,dom_am; char *p,name[160],name1[160],buf[200],str[160],phvelname[160],root[160]; char amp_name[100]; FILE *in, *fd, *inv, *fas; int i, j, flag, k, len, n_am, i_am; int nn,sac = 1; // =1 - SAC, =0 - ftat files // input command line arguments treatment if(argc != 2) { printf("Usage: aftan_amp [parameter file]\n"); exit(-1); } // open and read contents of parameter file if((in = fopen(argv[1],"r")) == NULL) { printf("Can not find file %s.\n",argv[1]); exit(1); } while((n = fscanf(in,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %s %d", &piover4,&vmin,&vmax,&tmin,&tmax,&tresh,&ffact,&taperl,&snr,&fmatch, name,&flag)) != EOF) { // start main loop strcpy(root,name); p = strrchr(root,'.'); *(p+1) = '\0'; //strcpy(phvelname,root); //strcat(phvelname,"SAC_PHP"); if(n == 0 || n != 12) break; printf("vmin= %lf, vmax= %lf, tmin= %lf, tmax= %lf\n", vmin,vmax,tmin,tmax); // remove quotes from file names j = 0; for(i = 0; i < strlen(name); i++) { if(name[i] == '\'' || name[i] == '\"') continue; name[j] = name[i]; j++; } name[j] = '\0'; printf("Tresh= %lf, Filter factor= %lf, SNR= %lf, Match = %lf\nData file name=%s\n", tresh,ffact,snr,fmatch,name); // if presents, read phase velocity prediction file // --- nprpv = -1; /* * read phase velocity information */ // strtok(root,"_"); // strcpy(sta1,strtok(NULL,"_")); // strcpy(sta2,strtok(NULL,".")); // sscanf(root, "%[A-Z,a-z,0-9]_%[A-Z,a-z,0-9].", sta1, sta2); sprintf(phvelname, "/home/tianye/code/Programs/head/scalifornia_avg_phvel.dat"); // sprintf(phvelname, "%s/%s/%s_%s.dat",argv[2],sta1,sta1,sta2); // fprintf(stderr, "predicted phase velocity %s \n",phvelname); // if((inv = fopen(phvelname,"r")) == NULL) { // printf("Can not find file %s. Use the inversed path instead\n",phvelname); // sprintf(phvelname, "%s/%s/%s_%s.dat",argv[2],sta2,sta2,sta1); if((inv = fopen(phvelname,"r")) == NULL) { printf("Can not find file %s.\n",phvelname); nprpv = 0; //goto next; continue; } // } fgets(buf,200,inv); while(fgets(buf,200,inv) != NULL) { if(nprpv == -1) { nprpv++; continue; } if((n = sscanf(buf,"%lf %lf",&prpvper[nprpv],&prpvvel[nprpv])) < 2) break; nprpv++; } fclose(inv); printf("Phase velocity prediction file name= %s\n",phvelname); // next: /* * read SAC or ascii data */ readdata(sac,name,&n,&dt,&delta,&t0,sei_p); if(flag==1) { len=(n-1)/2; for(k=0;k<=len;k++) sei_n[k]=sei_p[len-k]; for(k=0;k<=len;k++) sei_p[k]=sei_p[len+k]; for(k=0;k<=len;k++) sei[k]=(sei_p[k]+sei_n[k])/2.; n=len+1; t0 += len*dt; } else for(k=0;k<n;k++) sei[k]=sei_p[k]; /* * Read group velocity prediction file */ // strcpy(name1,root); // strcat(name1,"SAC_GRP"); // sscanf(root, "%[A-Z,a-z,0-9]_%[A-Z,a-z,0-9].", sta1, sta2); //sprintf(name1, "/home/linf/California/PRED_DISP/COR_%s_%s.SAC_PRED", sta1, sta2); //printf("Group velocity prediction curve: %s\n",name1); //if((fd = fopen(name1,"r")) == NULL) { // printf("Can not find file %s.\n",name1); // exit(1); //} //i = 0; //fgets(str,100,fd); //while((nn = fscanf(fd,"%lf %lf",&pred[0][i],&pred[1][i])) == 2) i++; //npred = i; //fclose(fd); /* ==================================================================== * Parameters for aftanipg function: * Input parameters: * piover4 - phase shift = pi/4*piover4, for cross-correlation * piover4 should be -1.0 !!!! (double) * n - number of input samples, (int) * sei - input array length of n, (float) * t0 - time shift of SAC file in seconds, (double) * dt - sampling rate in seconds, (double) * delta - distance, km (double) * vmin - minimal group velocity, km/s (double) * vmax - maximal value of the group velocity, km/s (double) * tmin - minimal period, s (double) * tmax - maximal period, s (double) * tresh - treshold, usually = 10, (double) * ffact - factor to automatic filter parameter, (double) * perc - minimal length of of output segment vs freq. range, % (double) * npoints - max number points in jump, (int) * taperl - factor for the left end seismogram tapering, * taper = taperl*tmax, (double) * nfin - starting number of frequencies, nfin <= 32, (int) * snr - phase match filter parameter, spectra ratio to * determine cutting point (double) * fmatch - factor to length of phase matching window * npred - length of prediction table * pred - prediction table: pred[0][] - periods in sec, * pred[1][] - pedicted velocity, km/s * flag - Input file type: 0 for single-sided, 1 for double-sided * ========================================================== * Output parameters are placed in 2-D arrays arr1 and arr2, * arr1 contains preliminary results and arr2 - final. * ========================================================== * nfout1 - output number of frequencies for arr1, (int) * arr1 - the first nfout1 raws contain preliminary data, * (double arr1[n][5], n >= nfout1) * arr1[:,0] - central periods, s (double) * arr1[:,1] - apparent periods, s (double) * arr1[:,2] - group velocities, km/s (double) * arr1[:,3] - phase velocities, km/s (double) * arr1[:,4] - amplitudes, Db (double) * arr1[:,5] - discrimination function, (double) * arr1[:,6] - signal/noise ratio, Db (double) * arr1[:,7] - maximum half width, s (double) * nfout2 - output number of frequencies for arr2, (int) * If nfout2 == 0, no final result. * arr2 - the first nfout2 raws contains final data, * (double arr2[n][5], n >= nfout2) * arr2[:,0] - central periods, s (double) * arr2[:,1] - apparent periods, s (double) * arr2[:,2] - group velocities, km/s (double) * arr2[:,3] - amplitudes, Db (double) * arr2[:,4] - signal/noise ratio, Db (double) * arr2[:,5] - maximum half width, s (double) * tamp - time to the beginning of ampo table, s (double) * nrow - number of rows in array ampo, (int) * ncol - number of columns in array ampo, (int) * ampo - Ftan amplitude array, Db, (double [32][32768]) * ierr - completion status, =0 - O.K., (int) * =1 - some problems occures * =2 - no final results */ // t0 = 0.0; nfin = 32; npoints = 10; // only 3 points in jump perc = 50.0; // 50 % for output segment // taperl = 2.0; // factor to the left end tapering printf("pi/4 = %5.1lf, t0 = %9.3lf\n",piover4,t0); printf("#filters= %d, Perc= %6.2f %s, npoints= %d, Taper factor= %6.2f\n", nfin,perc,"%",npoints,taperl); /* Call aftanipg function, FTAN + prediction */ // printf("FTAN + prediction curve\n"); //ffact =2.0; //aftanipg_(&piover4,&n,sei,&t0,&dt,&delta,&vmin,&vmax,&tmin,&tmax,&tresh, // &ffact,&perc,&npoints,&taperl,&nfin,&snr,&fmatch,&npred,pred, // &nprpv,prpvper,prpvvel, // &nfout1,arr1,&nfout2,arr2,&tamp,&nrow,&ncol,ampo,&ierr); //printres(dt,nfout1,arr1,nfout2,arr2,tamp,nrow,ncol,ampo,ierr,name,"_P"); //if(nfout2 == 0) continue; // break aftan sequence //printf("Tamp = %9.3lf, nrow = %d, ncol = %d\n",tamp,nrow,ncol); /* Pre-whiten and record the amp factor */ f1=1./tmax/1.25; f2=1./tmax; f3=1./tmin; f4=1./tmin/1.25; filter4_(&f1,&f2,&f3,&f4,&dt,&n,sei,&n_am,&dom_am); // printf("%lf %lf %lf\n",amp_rec[180],amp_rec[1000],amp_rec[1600]); // printf("%d %lf\n",n_am/2+1,dom_am); /* FTAN with phase match filter. First Iteration. */ printf("FTAN - the first iteration\n"); ffact =1.0; aftanpg_(&piover4,&n,sei,&t0,&dt,&delta,&vmin,&vmax,&tmin,&tmax,&tresh, &ffact,&perc,&npoints,&taperl,&nfin,&snr,&nprpv,prpvper,prpvvel, &nfout1,arr1,&nfout2,arr2,&tamp,&nrow,&ncol,ampo,&ierr); // printres(dt,nfout1,arr1,nfout2,arr2,tamp,nrow,ncol,ampo,ierr,name,"_1"); if(nfout2 == 0) continue; // break aftan sequence printf("Tamp = %9.3lf, nrow = %d, ncol = %d\n",tamp,nrow,ncol); /* Make prediction based on the first iteration */ npred = nfout2; tmin = arr2[0][1]; tmax = arr2[nfout2-1][1]; for(i = 0; i < nfout2; i++) { pred[0][i] = arr2[i][1]; // apparent period // central periods pred[1][i] = arr2[i][2]; // group velocities } /* FTAN with phase with phase match filter. Second Iteration. */ printf("FTAN - the second iteration (phase match filter)\n"); ffact = 2.0; aftanipg_(&piover4,&n,sei,&t0,&dt,&delta,&vmin,&vmax,&tmin,&tmax,&tresh, &ffact,&perc,&npoints,&taperl,&nfin,&snr,&fmatch,&npred,pred, &nprpv,prpvper,prpvvel, &nfout1,arr1,&nfout2,arr2,&tamp,&nrow,&ncol,ampo,&ierr); printf("Tamp = %9.3lf, nrow = %d, ncol = %d\n",tamp,nrow,ncol); printres(dt,nfout1,arr1,nfout2,arr2,tamp,nrow,ncol,ampo,ierr,name,"_2"); for(i = 0; i < nfout2; i++) { c_per[i]=arr2[i][0]; g_vel[i]=arr2[i][2]; } get_snr(sei_p,n,dt,delta,t0,c_per,g_vel,nfout2,amp_p,snr_p); if(flag==1) get_snr(sei_n,n,dt,delta,t0,c_per,g_vel,nfout2,amp_n,snr_n); sprintf(amp_name,"%s_amp_snr",name); if((fas=fopen(amp_name,"w"))==NULL) { printf("Cannot open file %s to write!\n"); exit (1); } if(flag==1) for(i = 0; i < nfout2; i++) fprintf(fas,"%8.4f %.5g %8.4f %.5g %8.4f\n",arr2[i][1],amp_p[i],snr_p[i],amp_n[i],snr_n[i]); else for(i = 0; i < nfout2; i++) fprintf(fas,"%8.4f %.5g %8.4f\n",arr2[i][1],amp_p[i],snr_p[i]); fclose(fas); } fclose(in); return 0; }
int bm3d (char* const infile, // name of input file char* const kind, // kind of shrinkage (ht, wnr, avg) int const block_size, // size of internal processed blocks int const block_step, // step size between blocks int const sigma, // standard deviation of noise int const max_blocks, // maximum number of block in one 3D array int const h_search, // horizontal width of search window int const v_search, // vertical width of search window double const th_2d, // threshold for the 2D transformation double const tau_match, // match value for block-matching double const th_3d, // threshold for the 3D transformtaion int const block_marking) { // indicates the action of block marking png_img img; // noisy input image png_img org; // temporary image for marking the blocks FILE* log = 0; // log-file for all kinds of messages char logfile[30]; // name of the log-file including path char path[30]; // universally used path-name char prefix[20]; // universally used prefix-name char pure_name[30]; int h_search_true; // true value of horizontal search window size after validation int v_search_true; // true value of vertical search window size after validation list_t y_list = 0; // list of groups of the y-channel list_t u_list = 0; // list of groups of the u-channel list_t v_list = 0; // list of groups of the v-channel clock_t start, end; // time variables for counting durations double time; // ---------------------------------------------------------------------- // OPEN LOG-FILE FOR WRITING // ---------------------------------------------------------------------- // obtain filename without path and extension if (exclude_extension(infile, pure_name) != 0) { return 1; } sprintf (logfile, "log/log_%s_%s[%d].txt", pure_name, kind, sigma); log = fopen (logfile, "a"); if (log == NULL) { generate_error ("Unable to open log-file for writing ..."); return 1; } // ---------------------------------------------------------------------- // INPUT READING AND VALIDATION // ---------------------------------------------------------------------- // read input image if (png_read(&img, infile) != 0) { return 1; } // read temporary image if (png_read(&org, infile) != 0) { return 1; } // control color type if (img.color != PNG_COLOR_TYPE_RGB) { generate_error ("Wrong color type..."); return 1; } // control number of channels if (img.channels != 3) { generate_error ("Wrong number of channels..."); return 1; } // ---------------------------------------------------------------------- // PARAMETER VALIDATION // ---------------------------------------------------------------------- // verify kind of shrinkage if (strcmp(kind, "none") && strcmp(kind, "avg") && strcmp(kind, "ht") && strcmp(kind, "wnr")) { generate_error ("Unknown kind of shrinkage..."); return 1; } // verify block size if ((block_size!=7) && (block_size!=9) && (block_size!=11) && (block_size!=13)) { generate_error ("Wrong value for block size...\nValid values: 7, 9, 11, 13"); return 1; } // verify block step if ((block_step<5) || (block_step>15)) { generate_error ("Block step is out of valid Range...\nValid range: 5..15"); return 1; } // control search window dimensions h_search_true = ((h_search > img.width) || (h_search <= 0)) ? img.width : h_search; v_search_true = ((v_search > img.height) || (v_search <= 0)) ? img.height : v_search; // ---------------------------------------------------------------------- // PRINTING OF STATUS INFORMATION // ---------------------------------------------------------------------- fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "[INFO] ... image dimensions: %dx%d\n", img.width, img.height); fprintf (log, "[INFO] ... kind of shrinkage: %s\n", kind); fprintf (log, "[INFO] ... block size: %d\n", block_size); fprintf (log, "[INFO] ... block step: %d\n", block_step); fprintf (log, "[INFO] ... sigma: %d\n", sigma); fprintf (log, "[INFO] ... maximum number of blocks: %d\n", max_blocks); fprintf (log, "[INFO] ... horizontal search window size: %d\n", h_search_true); fprintf (log, "[INFO] ... vertical search window size: %d\n", v_search_true); fprintf (log, "[INFO] ... threshold 2D: %f\n", th_2d); fprintf (log, "[INFO] ... tau-match 2D: %f\n", tau_match); fprintf (log, "[INFO] ... threshold 3D: %f\n", th_3d); fprintf (log, "[INFO] ... block marking: %s\n\n", block_marking ? "yes" : "no"); // ---------------------------------------------------------------------- // COLORSPACE CONVERSION & WRITEBACK // ---------------------------------------------------------------------- printf ("[INFO] ... launch of color conversion...\n"); rgb2yuv (&img); // write output image if (png_write(&img, "img/yuv/", "noisy_yuv", 0) != 0) { return 1; } printf ("[INFO] ... end of color conversion...\n\n"); fprintf (log, "[INFO] ... converted colorspace of input image to YUV...\n\n"); // ---------------------------------------------------------------------- // IMAGE-TO-ARRAY CONVERSION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of printing image as three separate value arrays...\n"); printf ("[INFO] ... ... luminance channel...\n"); img2array (&img, 0, "img/", "y_channel_before"); printf ("[INFO] ... ... chrominance channel 1...\n"); img2array (&img, 1, "img/", "u_channel_before"); printf ("[INFO] ... ... chrominance channel 2...\n"); img2array (&img, 2, "img/", "v_channel_before"); printf ("[INFO] ... end of printing arrays...\n\n"); fprintf (log, "[INFO] ... printed every intput channel as array of values...\n\n"); // ---------------------------------------------------------------------- // BLOCK-MATCHING // ---------------------------------------------------------------------- printf ("[INFO] ... launch of block-matching...\n"); printf ("[INFO] ... ... luminance channel...\n"); start = clock(); if (block_matching(&img, &org, block_size, block_step, sigma, h_search_true, v_search_true, th_2d, tau_match, 0, block_marking, &y_list) != 0) { return 1; } printf ("[INFO] ... end of block-matching...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... block-matching accomplished...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n", time); fprintf (log, "[INFO] ... ... number of groups in list: %d\n\n", list_length(&y_list)); // print recognized groups to file sprintf (path, "grp/org/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } // trim groups to maximal number of blocks printf ("[INFO] ... trimming groups to maximum size...\n\n"); if (trim_list(&y_list, max_blocks) != 0) { return 1; } fprintf (log, "[INFO] ... trimmed groups to maximum size of blocks...\n\n"); // obtain the pixel values from the u- and v-channel of the image printf ("[INFO] ... extracting blocks from chrominance channels...\n"); printf ("[INFO] ... ... chrominance channel 1...\n"); if (get_chrom(&img, &y_list, &u_list, 1)) { return 1; } printf ("[INFO] ... ... chrominance channel 2...\n\n"); if (get_chrom(&img, &y_list, &v_list, 2)) { return 1; } fprintf (log, "[INFO] ... extracted values from chrominance channels...\n\n"); // print trimmed groups to file sprintf (path, "grp/trm/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } sprintf (path, "grp/trm/%s/u/", kind); if (print_list(u_list, path, "group") != 0) { return 1; } sprintf (path, "grp/trm/%s/v/", kind); if (print_list(v_list, path, "group") != 0) { return 1; } // ---------------------------------------------------------------------- // IMAGE-DENOISING // ---------------------------------------------------------------------- printf ("[INFO] ... launch of shrinkage...\n"); start = clock(); printf ("[INFO] ... ... luminance channel...\n"); if (shrinkage(kind, &y_list, sigma, th_3d, 0) != 0) { return 1; } // printf ("[INFO] ... ... chrominance channel 1...\n"); // if (shrinkage(kind, &u_list, sigma, th_3d, 1) != 0) { // return 1; // } // printf ("[INFO] ... ... chrominance channel 2...\n"); // if (shrinkage(kind, &v_list, sigma, th_3d, 1) != 0) { // return 1; // } printf ("[INFO] ... end of shrinkage...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... accomplished shrinkage...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n\n", time); sprintf (path, "grp/est/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } sprintf (path, "grp/est/%s/u/", kind); if (print_list(u_list, path, "group") != 0) { return 1; } sprintf (path, "grp/est/%s/v/", kind); if (print_list(v_list, path, "group") != 0) { return 1; } // ---------------------------------------------------------------------- // AGGREGATION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of aggregation...\n"); start = clock(); printf ("[INFO] ... ... luminance channel...\n"); if (aggregate(kind, &img, &y_list, 0) != 0) { return 1; } // printf ("[INFO] ... chrominance channel 1...\n"); // if (aggregate(kind, &img, &u_list, 1) != 0) { // return 1; // } // printf ("[INFO] ... chrominance channel 2...\n"); // if (aggregate(kind, &img, &v_list, 2) != 0) { // return 1; // } printf ("[INFO] ... end of aggregation...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... accomplished aggregation...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n\n", time); // ---------------------------------------------------------------------- // IMAGE-TO-ARRAY CONVERSION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of printing image as three separate value arrays...\n"); printf ("[INFO] ... ... luminance channel...\n"); img2array (&img, 0, "img/", "y_channel_after"); printf ("[INFO] ... ... chrominance channel 1...\n"); img2array (&img, 1, "img/", "u_channel_after"); printf ("[INFO] ... ... chrominance channel 2...\n"); img2array (&img, 2, "img/", "v_channel_after"); printf ("[INFO] ... end of printing arrays...\n\n"); // ---------------------------------------------------------------------- // COLORSPACE CONVERSION & WRITEBACK // ---------------------------------------------------------------------- printf ("[INFO] ... launch of color conversion...\n"); yuv2rgb (&img); sprintf (prefix, "denoised_rgb_%s_%s", pure_name, kind); // write output image if (png_write(&img, "img/rgb/", prefix, sigma) != 0) { return 1; } printf ("[INFO] ... end of color conversion...\n\n"); fprintf (log, "[INFO] ... converted colorspace of output image to RGB...\n\n"); fprintf (log, "[INFO] ... PSNR after denoising: %fdB\n", get_snr(&org, &img)); fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "-------------------------------------------------------------------------\n\n\n"); // ---------------------------------------------------------------------- // FREEING DYNAMICALY ALLOCATED MEMORY // ---------------------------------------------------------------------- free_list (&y_list); free_list (&u_list); free_list (&v_list); png_free_mem (&img); png_free_mem (&org); fclose (log); return 0; }