Skip to content

Instantly share code, notes, and snippets.

@libertylocked
Created January 25, 2015 21:32
Show Gist options
  • Save libertylocked/0e40a11de3250e7afc79 to your computer and use it in GitHub Desktop.
Save libertylocked/0e40a11de3250e7afc79 to your computer and use it in GitHub Desktop.
COE 449 project 1
/*
project-1.c
As you develop and test this file:
use this command to compile: (you can name the executable whatever you like)
gcc -W -Wall -Wextra -O2 project-1.c -o project-1.exe
use this command to execute: (you will of course test on both input files)
./project-1.exe sine-1.bin
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[])
{
FILE * inFile= NULL;
// DECLARE ALL ADDITIONAL NEEDED VARIABLES
/* recommend declaring two short int i.e prev & current so that you can compare
them to look for zero crossings or changes in direction and sign.
You will also need some int counters etc.
*/
if (argc < 2)
{
fprintf(stderr,"\nMust specify a binary file on the command line. Please try again.\n");
exit(EXIT_FAILURE);
}
if ((inFile = fopen(argv[1],"rb")) == NULL)
{
fprintf(stderr,"Can't open %s for input\n", argv[1] );
exit(EXIT_FAILURE);
}
// YOUR CODE HERE - READ EACH VALUE FROM THE BINARY FILE ONE AT A TIME AND LOOK FOR ZERO CROSSINGS TO DETECT WAVES
/* recommended strategy:
read in the first value of the file (prev) before the loop starts.
Then in the while loop read next short in.
In the loop you are to be looking for critical events: zero crossings or sign changes.
Before you start writing that code start out just echoing each value and the sample # associated with it.
Once that is right start printing an alert whenever zero is touched/crossed OR or the direction changes.
Zero crossings and direction changes are the critical events you must correctly detect.
Once that is right then try to detect the start of the first wave.
Once that is right add code to detect the end of the first wave.
Then start coding to detet every wave - counting samples per wave and
keeping track of the highest and lowest value in that wave.
*/
short int lastShort, nextShort;
int samplesTotal = 1;
int waveCount = 0;
int samplesInWave = 1;
short int min = 0;
short int max = 0;
// Read in the first value of the file
fread(&lastShort, sizeof(lastShort), 1, inFile);
//printf("#%d: %hi\n", samplesTotal, lastShort);
// In the while loop read next short in
while (fread(&nextShort, sizeof(nextShort), 1, inFile) == 1)
{
// Look for zero crossings or sign changes
if ((lastShort * nextShort < 0 || nextShort == 0) && nextShort > lastShort)
{
// Check if it's at the end of a wave
if (waveCount > 0)
{
if (nextShort == 0)
{
// nextShort is the end of last wave because it's 0
// Include nextShort into sample count
printf("%d\t%hi\t%d\t%hi\t%hi\n", samplesTotal + 1, nextShort, samplesInWave + 1, max, min);
}
else
{
// lastShort is the end of last wave
printf("%d\t%hi\t%d\t%hi\t%hi\n", samplesTotal, lastShort, samplesInWave, max, min);
}
}
// Start of a wave
waveCount++;
//printf("start of wave #%d at sample #%d\n", waveCount, samplesTotal);
samplesInWave = 0;
max = nextShort;
min = nextShort;
}
// Search for peak and nadir
if (nextShort > max)
{
max = nextShort;
}
if (nextShort < min)
{
min = nextShort;
}
// Increment sample counters
samplesTotal++;
samplesInWave++;
//printf("#%d: %hi\n", samplesTotal, nextShort);
// Set lastShort to nextShort
lastShort = nextShort;
}
fclose(inFile); /* after the read loop is done. close file */
return EXIT_SUCCESS; // this is just a zero value. In C a zero return often indicates no error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment