Exemple #1
0
int main(int argc, char *argv[])
{
   int numread;
   double shift;
   float indata[WORKLEN], outdata[WORKLEN + 1], overlap = 0.0;
   char *infilenm = NULL, *outfilenm = NULL;
   FILE *infile = NULL, *outfile = NULL, *textout = NULL;

   /*  Get the filename of the datafile and open it */

   textout = (argc == 3) ? stderr : stdout;
   if (argc == 3 || argc == 4) {
      shift = strtod(argv[1], (char **) NULL);
      if (shift <= -1.0 || shift >= 1.0) {
         fprintf(textout, "\nbins_to_shift must be between -1.0 and 1.0.");
         fprintf(textout, "  Exiting.\n\n");
         exit(-1);
      }
      infilenm = (char *) malloc(strlen(argv[2]) + 1);
      strcpy(infilenm, argv[2]);
      fprintf(textout, "\nShifting data from \"%s\" by %f bin.\n\n",
              infilenm, shift);
      if (NULL == (infile = fopen(infilenm, "rb"))) {
         fprintf(textout, "\nCan't open \"%s\".  Exiting.\n\n", infilenm);
         exit(-1);
      }

      /* Open an output file if needed, else STDOUT */

      if (argc == 4) {
         outfilenm = (char *) malloc(strlen(argv[3]) + 1);
         strcpy(outfilenm, argv[3]);
         fprintf(textout, "Shifted data will be placed in \"%s\".\n\n", outfilenm);
         if (NULL == (outfile = fopen(outfilenm, "wb"))) {
            fprintf(textout, "\nCan't open \"%s\".  Exiting.\n\n", outfilenm);
            exit(-1);
         }
      } else {
         outfile = stdout;
      }

   } else {

      printf("\nUsage:  shiftdata bins_to_shift infile [outfile]\n\n");
      printf("   This routine is used to shift a file of single\n");
      printf("   precision floating point data to the left or right\n");
      printf("   by up to 1 bin.  This allows you to patch two time\n");
      printf("   series together with the correct phase, assuming\n");
      printf("   that the data point durations are the same.\n");
      printf("   The arguments are:\n");
      printf("      bins_to_shift (double):  The amount to shift the\n");
      printf("         data.  Negative shifts left, positive shifts\n");
      printf("         right.  For example, if I have the time series\n");
      printf("         {1.0, 2,0, 1.0} and I give a bins_to_shift of\n");
      printf("         -0.3, my new time series would look like\n");
      printf("         {0.3, 1.3, 1.7, 0.7}.  Notice that my time\n");
      printf("         series increased in length by one and that I\n");
      printf("         added some noise due to the fact that I am\n");
      printf("         performing a type of two-bin average.\n");
      printf("      infile (string):  The data file to shift.\n");
      printf("      outfile (string):  An optional output file.  The\n");
      printf("         default behavior sends the raw output to STDOUT\n\n");
      exit(0);
   }

   /* Read the data in WORKLEN sized chunks */

   do {
      numread = fread(indata, sizeof(float), WORKLEN, infile);
      fshift(indata, outdata, numread, shift, overlap);
      overlap = outdata[numread];
      fwrite(outdata, sizeof(float), (unsigned long) numread, outfile);
   } while (numread == WORKLEN);

   /* Write the last data point if we didn't shift at all */

   if (fabs(shift) > 1.0e-7)
      fwrite(&overlap, sizeof(float), 1, outfile);

   /* Cleanup */

   free(infilenm);
   fclose(infile);
   if (argc == 4) {
      fclose(outfile);
      free(outfilenm);
   }
   exit(0);
}
Exemple #2
0
int main()
{
	int rc;						// Return code from filesystem functions

	File f1;						// Demo file handle

	static char buf[1024];	// General-purpose buffer.

	if (!LX_2_USE) {
		printf("The specified device (LX# %d) does not exist.  Change LX_2_USE.\n",
			(int)LX_2_USE);
		exit(1);
	}
	else
		printf("Using device LX# %d...\n", (int)LX_2_USE);

	/*
	 * Step 1: initialize the filesystem.  A real application must
	 * decide whether to format a new flash device, or just start
	 * up an existing filesystem.  Formatting should not normally
	 * be needed unless, say, a user commands it.  A factory-fresh
	 * flash device will usually be all erased so there is no need
	 * to format.  Likewise, a flash with completely random data
	 * will be recognised as such and automatically formatted.
	 */
	rc = fs_init(0,0);
	if (rc) {
		printf("Could not initialize filesystem, error number %d\n", errno);
		exit(2);
	}

	/*
	 * Step 2: format the filesystem if requested.  Note that formatting
	 * must be done _after_ the call to fs_init().
	 *
	 * This demo initially asks whether the flash should be formatted
	 * via the stdio connection.
	 */

	printf("Do you want to:\n");
	printf("  <enter>       re-use existing filesystem -or-\n");
	printf("  F <enter>     format the filesystem logical extent?\n");
	gets(buf);
	if (toupper(buf[0]) == 'F') {
		rc = lx_format(LX_2_USE, 0);
		if (rc) {
			printf("Format failed, error code %d\n", errno);
			exit(3);
		}
	}


	/*
	 * Step 3: open a file for writing if it already exists, or
	 * create it if it does not.  Note that creation automatically
	 * opens the file for writing if it does not exist.  If it
	 * does exist, then EEXIST will be returned in errno.
	 */
	printf("Capacity of LX# %d is approximately %ld\n",
		(int)LX_2_USE, fs_get_lx_size(LX_2_USE, 0, 0));
	fs_set_lx(LX_2_USE, LX_2_USE);
	rc = fcreate(&f1, MY_FILE_NAME);
	if (rc && errno == EEXIST) {
		printf("File %d exists: shall I delete it? (y/n)\n", (int)MY_FILE_NAME);
		gets(buf);
		if (toupper(buf[0]) == 'Y') {
			fdelete(MY_FILE_NAME);
			rc = fcreate(&f1, MY_FILE_NAME);
		}
		else
			rc = fopen_wr(&f1, MY_FILE_NAME);
	}
	if (rc) {
		printf("Couldn't create/open file %d: errno = %d\n", (int)MY_FILE_NAME, errno);
		exit(3);
	}

do_it_again:
	/*
	 * Seek to the end of the file then print the current length.
	 */
	fseek(&f1, 0, SEEK_END);
	printf("File length is %ld\n", ftell(&f1));

	/*
	 * Add some data to the end.
	 */
	strcpy(buf, "Test pattern ________");
	rc = fwrite(&f1, buf, strlen(buf));
	if (rc < strlen(buf)) {
		printf("Append operation failed, error code %d\n", errno);
		// but we keep going...
	}

	printf("After append operation...\n");
	print_file(&f1);

	/*
	 * Seek backwards and overwrite something.
	 */
	fseek(&f1, -7, SEEK_END);
	sprintf(buf, "%ld", ftell(&f1));
	rc = fwrite(&f1, buf, strlen(buf));
	if (rc < strlen(buf)) {
		printf("Overwrite operation failed, error code %d\n", errno);
		// but we keep going...
	}

	printf("After overwrite operation...\n");
	print_file(&f1);

	/*
	 * Delete some data from the start.
	 */
	rc = fshift(&f1, DELETE_AMOUNT, buf);
	if (rc == 0) {
		printf("Shift operation failed, error code %d\n", errno);
		// but we keep going...
	}

	printf("After shifting out %d chars...\n", rc);
	print_file(&f1);
	buf[rc] = 0;
	printf("...and the deleted data was '%s'\n", buf);

	/*
	 * Another round, anyone?
	 */
	printf("Are you having too much fun? (y/n)\n");
	gets(buf);
	if (toupper(buf[0]) == 'Y')
		goto do_it_again;


	return 0;
}