// file: lecture_17/gen_signal/example.cc // // local include files // #include "example.h" // function: main // // This is a simple program that generates a sinewave. // int main(int argc, const char** argv) { // open a file for writing // FILE* fp = fopen(argv[1], "w"); if (fp == (FILE*)NULL) { fprintf(stdout, "**> (%s): error opening file %s\n", argv[0], argv[1]); } // decode the command line arguments // float fsamp = atof(argv[2]); float freq = atof(argv[3]); float ampl = atof(argv[4]); float dur = atof(argv[5]); // echo the command line arguments // fprintf(stdout, "program name: %s\n", argv[0]); fprintf(stdout, "fsamp = %f Hz\n", fsamp); fprintf(stdout, "freq = %f Hz\n", freq); fprintf(stdout, "ampl = %f units\n", ampl); fprintf(stdout, "dur = %f secs\n", dur); // create space for the signal // long num_samples = round(dur * fsamp); short int sig[num_samples]; // loop over all samples // for (long i = 0; i < num_samples; i++) { // compute the argument to sin // float sum = (float)i / fsamp * freq * M_2PI; // float val = ampl * sin(sum); float val = i; // clip the signal: // note the range of a short int is [-32768, 32767] // if (val > MAX_VAL) { val = MAX_VAL; } else if (val < -MAX_VAL) { val = -MAX_VAL; } // assign the value // sig[i] = (short int)val; } // write the signal to a file // long num_written = fwrite(sig, sizeof(short int), num_samples, fp); if (num_written != num_samples) { fprintf(stdout, "**> (%s): incorrect number of samples written (%d %d)\n", num_written, num_samples); } // close the file // fclose(fp); // exit gracefully // return(0); }