// file: lecture_18/example.cc // // generate a sinewave to a file // // include files // //#include "example.h" // main program // int main(int argc, char** argv) { int joe; { int mary; } float* s = {1, 2, 3}; long i = s[i] + log(x) + j / k; char c = 'a'; long ii = (long)c; float* x; long joe = (long)(long*)x; /* // check the number of command line arguments // if (argc < NARGS) { fprintf(stdout, "**> insufficient number of arguments (%d)\n", argc); return(1); } // process the command line arguments // float amplitude = atof(argv[1]); float frequency = atof(argv[2]); float phase = atof(argv[3]); float sample_frequency = atof(argv[4]); float duration = atof(argv[5]); char* ofname = argv[6]; // echo the input parameters // fprintf(stdout, "sample_frequency = %f Hz\n", sample_frequency); // not a good way: loop over all samples // float time_increment = 1.0 / sample_frequency; for (float ctime = 0; ctime < duration; ctime += time_increment) { float sum = amplitude * sin(2.0 * M_PI * frequency * ctime + phase / 360.0); } // convert time to samples // long N = (long)round(duration * sample_frequency); fprintf(stdout, "the number of samples is %d\n", N); // loop over the number of samples and generate a sinewave // float sample_period = 1.0 / sample_frequency; float sine_const = 2.0 * M_PI * frequency * sample_period; for (long i = 0; i < N; i++) { // calculate a value // float sum = amplitude * sin(sine_const * (float)i); } /* // open the file again // FILE* fp = fopen(argv[1], "r"); if (fp == (FILE*)NULL) { fprintf(stdout, "**> error opening file\n"); return(-1); } // process the command line arguments // float sample_frequency = atof(argv[2]); fprintf(stdout, "sample_frequency = %f Hz\n", sample_frequency); // get the size by seeking to the end // fseek(fp, (long)0, SEEK_END); long end_pos = ftell(fp); long N = end_pos / sizeof(short int); rewind(fp); fprintf(stdout, "%s: number of samples = %ld\n", argv[1], N); // declare a buffer size // long M = 10; short int mybuf[N]; fprintf(stdout, "<---->\n"); fprintf(stdout, "... reading back in groups of %d\n", M); // long nbytes_elem = fread(mybuf, sizeof(short int), N * 10, fp); //fprintf(stdout, "number of elements read = %d\n", nbytes_elem); long i = 0; long nelem_read = 0; while ((nelem_read = fread(&mybuf[i], sizeof(short int), M, fp)) > 0) { if (nelem_read != M) { fprintf(stdout, "**> warning: end of file\n"); } fprintf(stdout, "(%d) (%f secs): %f %f %f\n", i, (float)i / sample_frequency, (float)mybuf[i], (float)mybuf[i+1], (float)mybuf[i+2]); i += M; } // close the file // fclose(fp); //-------------------------------------------------------------------- // get the command line arguments // FILE* fp = fopen(argv[1], "w"); if (fp == (FILE*)NULL) { fprintf(stdout, "error opening file\n"); return(-1); } float amplitude = atof(argv[2]); fprintf(stdout, "amplitude = %f units\n", amplitude); float frequency = atof(argv[3]); fprintf(stdout, "frequency = %f Hz\n", frequency); float total_time = atof(argv[4]); fprintf(stdout, "total time = %f secs\n", total_time); float sample_frequency = atof(argv[5]); fprintf(stdout, "sample_frequency = %f Hz\n", sample_frequency); // not a good way: loop over all samples // float time_increment = 1.0 / sample_frequency; for (float ctime = 0; ctime < total_time; ctime += time_increment) { fprintf(stdout, "ctime = %f\n", ctime); } // convert time to samples // long N = (long)round(total_time * sample_frequency); fprintf(stdout, "the number of samples is %d\n", N); // loop over the number of samples and generate a sinewave // float sample_period = 1.0 / sample_frequency; float sine_const = 2.0 * M_PI * frequency * sample_period; for (long i = 0; i < N; i++) { // calculate a value // float sum = amplitude * sin(sine_const * (float)i); // write this to a file // short int ival = (short int)round(SCALE * sum); //short int ival = round(sum); fprintf(stdout, "(%d) (%f secs): %f (%d)\n", i, (float)i / sample_frequency, sum, ival); if (fwrite(&ival, sizeof(short int), 1, fp) != 1) { fprintf(stdout, "*> error writing data (%d %f %d)\n", i, sum, ival); } } // close the file // fclose(fp); // open the file again // FILE* fp2 = fopen(argv[1], "r"); if (fp2 == (FILE*)NULL) { fprintf(stdout, "error opening file\n"); return(-1); } fprintf(stdout, "<---->\n"); fprintf(stdout, "... reading back ...\n"); long i = 0; short int jval; do while { }; while (fread(&jval, sizeof(short int), 1, fp2) == 1) { fprintf(stdout, "(%d) (%f secs): %f (%d)\n", i, (float)i / sample_frequency, (float)jval, jval); i++; } // close the file // fclose(fp2); // check the number of arguments // if (argc < 7) { fprintf(stdout, "%s: insuffficient number of arguments (%d)\n", argv[0], argc); exit(-1); } */ // grab the last argument // char grade = argv[1][0]; fprintf(stdout, "the input was: %c [%d]\n", grade, grade); // convert the input to uppercase // char tmp_grade = toupper(grade); // test for its value using if/else if // fprintf(stdout, "using if/else if:\n"); if ((tmp_grade == 'A') || (tmp_grade == 'Z')) { fprintf(stdout, "%c: Excellent!\n", tmp_grade); } else if (tmp_grade == 'B') { fprintf(stdout, "%c: Well done\n", tmp_grade); } /* else if (tmp_grade == 'C') { fprintf(stdout, "%c: Satisfactory\n", tmp_grade); } else if (tmp_grade == 'D') { fprintf(stdout, "%c: You passed\n", tmp_grade); } else if (tmp_grade == 'F') { fprintf(stdout, "%c: Better try again\n", tmp_grade); } */ else { fprintf(stdout, "%c: Invalid tmp_grade\n", tmp_grade); } // switch statement // fprintf(stdout, "using switch:\n"); switch(tmp_grade) { case 'A': case 'Z': fprintf(stdout, "%c: Excellent!\n", tmp_grade); break; case 'B': case 'C': fprintf(stdout, "%c: Well done\n", tmp_grade); break; case 'D': fprintf(stdout, "%c: You passed\n", tmp_grade); break; case 'F': fprintf(stdout, "%c: Better try again\n", tmp_grade); break; default: fprintf(stdout, "%c: Invalid grade\n", tmp_grade); } // try a number // int tmp_num = atoi(argv[2]); switch(tmp_num) { case 3: fprintf(stdout, "the perfect number...\n"); break; case 27: fprintf(stdout, "my favorite number...\n"); break; default: fprintf(stdout, "what is wrong with you!...\n"); } // exit gracefully // return 0; }