📄 addsd.c
字号:
/* This material contains proprietary software of Entropic Speech, Inc. Any reproduction, distribution, or publication without the prior written permission of Entropic Speech, Inc. is strictly prohibited. Any public distribution of copies of this work authorized in writing by Entropic Speech, Inc. must bear the notice "Copyright (c) 1987 Entropic Speech, Inc.; All rights reserved" Module Name: addsd Written By: Ajaipal S. Virdy Checked By: Alan Parker Purpose: This program adds two SPS sampled data files to create a new sd file. In a typical application, the two SPS sd files represent signal and noise (this program allows the second file to be scaled), and the new sd file is the "signal + noise". Secrets: None */#ifdef SCCSstatic char *sccs_id = "@(#)addsd.c 1.5 6/16/87 ESI";#define VERSION "1.5"#define DATE "6/16/87"#endif#ifndef VERSION#define VERSION "debug"#endif#ifndef DATE#define DATE "none"#endif#include <stdio.h>#include <esps/esps.h>#include <esps/sd.h>#define SYNTAX USAGE ("addsd [-x debug-level] [-f range] [-s scale] [-i] [-t] file1 file2 file3")#define SIZE 1024 /* size of data buffers */extern optind;extern char *optarg, *get_cmd_line();main (argc, argv) /* program to add two sampled data files */int argc;char **argv;{ int c; int getopt (); double atof(); int atoi(); FILE *f1p, *f2p, *f3p; /* file pointers */ struct header *f1h, *f2h, *f3h; /* pointers to structure */ char *f1str, *f2str, *f3str; /* string pointers */ int debug_level = 0; /* debugger flag */ int fflag = 0; /* range flag */ int sflag = 0; /* scale flag */ int iflag = 0; /* inhibit warning message flag */ int tflag = 0; /* truncate flag */ char *range; /* string to hold range */ double scale; /* scale file2 before adding to file1 */ double maxvalue; /* maximum value of file3 */ char combuf[100]; /* command buffer for writing to header */ int s_rec; /* starting record position */ int e_rec; /* ending record position */ int n_rec; /* number of records in file1 to use */ int tot_rec; /* total number of records */ int f2_rec; /* total number of records in file2 */ int reuse_data; /* flag for reusing data in file2 */ int data_read; /* flag for determining if data was read */ int n = SIZE; /* number of records to read and write */ double dbuf1[SIZE]; /* buffer for storing sd records for file1 */ double dbuf2[SIZE]; /* buffer for storing sd records for file2 */ int i = 0; /* indexing variable *//* get options from command line and set various flags */ while ((c = getopt (argc, argv, "x:f:s:it")) != EOF) { switch (c) { case 'x': debug_level = atoi (optarg); break; case 'f': fflag++; range = optarg; break; case 's': sflag++; scale = atof (optarg); if (debug_level > 0) fprintf (stderr, "addsd: scale is %lf\n", scale); break; case 'i': iflag++; break; case 't': tflag++; break; default: SYNTAX; } }/* user must supply three file names: file1 file2 file3 */ if (argc - optind < 3) SYNTAX;/* check if output should be sent to stdout (i.e. f3str = "-") */ f3str = argv[argc - 1]; if (strcmp (f3str, "-") == 0) { f3str = "<stdout>"; f3p = stdout; } else TRYOPEN (argv[0], f3str, "w", f3p);/* check if input should be taken from stdin (i.e. f1str = "-") */ f1str = argv[optind++]; if (strcmp (f1str, "-") == 0) { f1str = "<stdin>"; f1p = stdin; } else TRYOPEN (argv[0], f1str, "r", f1p);/* also open the second file (it usually represents noise) */ f2str = argv[optind]; TRYOPEN (argv[0], f2str, "r", f2p); if (debug_level > 0) (void) fprintf (stderr, "addsd: file1 = %s, file2 = %s, file3 = %s.\n", f1str, f2str, f3str);/* make sure the two input files are SPS files */ if (!(f1h = read_header (f1p))) NOTSPS ("addsd:", f1str); if (!(f2h = read_header (f2p))) NOTSPS ("addsd:", f2str);/* make sure the two input files are Sampled Data Files (.sd extension) */ if (f1h -> common.type != FT_SD) { (void) fprintf (stderr, "addsd: %s is not a sampled data file.\n", f1str); exit (1); } if (f2h -> common.type != FT_SD) { (void) fprintf (stderr, "addsd: %s is not a sampled data file.\n", f2str); exit (1); }/* compare sampling frequencies of the two input files, exit if they differ */ if (f1h->hd.sd->sf != f2h->hd.sd->sf) { (void) fprintf(stderr, "addsd: both files must have same sf, file %s sf is %g.\n", f1str,f1h->hd.sd->sf); (void) fclose(f3p); exit(1); }/* make sure the given range (if there is one) exists in file1 */ s_rec = 1; e_rec = f1h -> common.ndrec; if (debug_level > 0) (void) fprintf (stderr, "addsd: %d data records in %s\n", f1h -> common.ndrec, f1str); (void) range_switch (range, &s_rec, &e_rec, 1); if (debug_level > 1) (void) fprintf (stderr, "addsd: range of %s starts at %d and ends at %d.\n", f1str, s_rec, e_rec); if ((s_rec > f1h->common.ndrec) || (e_rec > f1h->common.ndrec)) { (void) fprintf (stderr, "addsd: incorrect subrange given, only %d records in %s.\n", f1h -> common.ndrec, f1str); exit(1); } if ( s_rec > e_rec ) { (void) fprintf (stderr, "addsd: start record greater than end record.\n"); exit(1); }/* determine how many records will be written into file3 */ n_rec = e_rec - s_rec + 1; reuse_data = NO; if ( f2h->common.ndrec < n_rec ) { if (debug_level > 0) (void) fprintf (stderr, "addsd: %s greater than %s.\n", f1str, f2str); if ( !iflag ) (void) fprintf (stderr, "addsd: %s does not contain enough records--", f2str); if ( tflag ) { /* truncate the processing */ tot_rec = f2h->common.ndrec; if ( !iflag ) (void) fprintf (stderr, "truncating data in %s.\n", f1str); } else { /* reuse data */ tot_rec = n_rec; reuse_data = YES; if ( !iflag ) (void) fprintf (stderr, "reusing data in %s.\n", f2str); } } if ( f2h->common.ndrec == n_rec ) { tot_rec = n_rec; if (debug_level > 0) (void) fprintf (stderr, "addsd: %s equal to %s.\n", f1str, f2str); } if ( f2h->common.ndrec > n_rec ) { /* truncate size of output file to file1 */ tot_rec = n_rec; (void) fprintf (stderr, "addsd: %s larger than %s, truncating data to size of %s.\n", f2str, f1str, f1str); }/* put all pertinent information from file1 to file3 */ f3h = copy_header (f1h); (void) add_source_file (f3h, f1str, f1h); (void) add_source_file (f3h, f2str, f2h);/* add new information to the header of file3 */ (void) strcpy (f3h -> common.prog, "addsd"); (void) strcpy (f3h -> common.vers, VERSION); (void) strcpy (f3h -> common.progdate, DATE); (void) add_comment (f3h, get_cmd_line(argc,argv)); f3h -> common.tag = NO; f3h -> common.ndrec = tot_rec; f3h -> hd.sd -> sf = f1h -> hd.sd -> sf; f3h -> hd.sd -> src_sf = f1h -> hd.sd -> src_sf; f3h -> hd.sd -> equip = NONE; if (sflag) f3h -> hd.sd -> scale = scale; (void) set_sd_type (f3h, get_sd_type (f1h)); if (debug_level > 0) (void) fprintf (stderr, "addsd: sampling frequency is %f.\n", f3h->hd.sd->sf); (void) skiprec (f1p, s_rec - 1, size_rec (f1h)); if (debug_level > 0) { (void) fprintf (stderr, "addsd: skipped %d records.\n",s_rec - 1); } (void) sprintf (combuf, " Added samples %d to %d of %s to samples 1 to %d of %s by addsd.\n", s_rec, s_rec + tot_rec - 1, f1str, tot_rec, f2str); (void) add_comment (f3h, combuf); if ( !sflag ) scale = 1; if ( (f1h->hd.sd->max_value != 0) && (f2h->hd.sd->max_value != 0) ) f3h->hd.sd->max_value = f1h->hd.sd->max_value + scale * f2h->hd.sd->max_value; else f3h->hd.sd->max_value = 0; if (debug_level > 0) { (void) fprintf (stderr,"addsd: maxvalue is %f.\n", f3h->hd.sd->max_value); (void) fprintf (stderr, "addsd: writing header to %s.\n", f3str); } (void) write_header (f3h, f3p);/* now begin adding data from file1 and (scaled) file2 to file3 */ f2_rec = f2h -> common.ndrec; while (tot_rec > 0) { n = SIZE; if (tot_rec < n) n = tot_rec; if (reuse_data) { if (f2_rec < n) { n = f2_rec; (void) get_sd_recd (dbuf2, n, f2h, f2p); /* rewind file */ (void) fclose (f2p); TRYOPEN (argv[0], f2str, "r", f2p); f2h = read_header (f2p); data_read = YES; } } if (get_sd_recd (dbuf1, n, f1h, f1p) == EOF) { (void) fprintf (stderr, "addsd: ran out of data in file %s.\n",f1str); break; } if ( !data_read ) if (get_sd_recd (dbuf2, n, f2h, f2p) == EOF) { (void) fprintf (stderr, "addsd: ran out of data in file %s.\n",f2str); break; }/* scale the data in file2 if a scale factor was given */ if ( sflag ) for (i = 0; i < n; i++) dbuf2[i] *= scale;/* now add the data from file1 and file2, sample-by-sample */ for (i = 0; i < n; i++) dbuf1[i] += dbuf2[i]; (void) put_sd_recd (dbuf1, n, f3h, f3p); tot_rec -= n; f2_rec -= n; if ( f2_rec == 0 ) f2_rec = f2h->common.ndrec; data_read = NO; } (void) fclose (f1p); (void) fclose (f2p); (void) fclose (f3p);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -