2.14. Exercises

Exercise 2.4.

Now you have seen the program, you are advised to compile and run it. The precise formulation of the expression to compile it will depend on your operating system, where you have put the file, and which C compiler you are using. On a PC running Windows, using the Gnu C compiler in the DJGPP download, however, proceed as follows. (It is probably best, if you have not already done so, to make a folder on your C: drive, called e.g. SLP, and copy coswave.c to that folder.) First, open an MS-DOS window. (From the Start menu, select "Programs", then "MS-DOS Prompt".) An MS-DOS Prompt window will open with the prompt C:\WINDOWS>, so change to the right directory by typing e.g. cd ..\slp [Enter]

([Enter] means "press the Enter key", of course. On your computer, this might be labelled Enter, or Return, or be marked with a bent arrow. You know the key I mean.)

Next, compile the program to object code, by typing gcc -c -Wall coswave.c  [Enter] The compiler will respond with the message:

coswave.c:13: warning: return type of 'main' is not 'int'

But that is nothing to worry about: there is nothing wrong with the program (nor the compiler: it is only doing its job). The output of this step is a file called coswave.o, which you cannot understand. Next, compile this object file to an executable program. You can give it whatever name you want: I suggest bleep.exe:

gcc -o bleep.exe coswave.o Enter

Now, you can run bleep.exe, in two different ways. First, still working in the MS-DOS Prompt window, you can just type bleep [Enter] : nothing much appears to happen, though if you type dir cos*, you should see that as well as coswave.c and coswave.o, there is now a file called cosine.dat, the size of which should be 16,000 bytes (i.e. 8,000 shorts, remember?). Alternatively, since bleep.exe is an executable program, you can run it under Windows. For instance, from the desktop, by clicking on the "My Computer" icon, then on the C: drive icon and then on the icon for the SLP folder, you should be able to find an icon for the Application "bleep". Clicking on that icon (or double-clicking, depending on your desktop preferences) will run the program. Doing so will cause a new MS-DOS window to appear, then almost immediately afterwards it will disappear again. The file cosine.dat should now have been created.

Either way, cosine.dat is still not quite usable, because it is a "raw" binary data file: it is not in any recognised audio file format. Fortunately, utilities are available in many operating systems for converting raw audio files to a variety of standard audio file formats. Using "Cool Edit 96" on my PC, for instance, I can follow the following steps to play the cosine.dat audio file:

1) Start Cool Edit 96: from the Start menu, select "Programs", then "Syntrillium", then "Cool Edit 96". Your configuration could be different.)
2) Pull down "File/Open..." and move to the right directory
3) At the bottom of the "Open a Waveform" window, in the "Files of type:" field, pull down type "All files (*.*)"
4)  Double click on the file cosine.dat.
5) Since cosine.dat is not in a recognised audio file format, a window pops up with the title "Interpret Sample Format As". Select the sample rate 8000, channels: mono, and resolution: 16-bit.
6) In the "Raw Data (no header)" window that now pops up, select "Data formatted as PCM, Intel (LSB,MSB)".
7) All being well, the cosine wave should now be displayed. Clicking on the "play" button causes it to be played over the sound card as a short audible bleep.

Exercise 2.5. Recording and displaying speech waveforms

As well as looking at and listening to cosine.dat, examine the speech wave joe.dat, a recording of a sentence containing many of the phonemes of English. What sampling rate was used in that recording? (Note that the resolution is 16 bits and the bytes are in the "Intel" byte ordering convention.)

Exercise 2.6.
In the lecture I stated that "215 is about 32,000". What is the exact value?

Exercise 2.7.
Compile and run the program.

Exercise 2.8.
Alter line 34 as follows:

{x[i] = (short int) 32000 * cos(i*arg);
printf("%d %d\n",i,x[i]);}

What is the effect of this change when coswave.c is compiled again and bleep is run? Note that if more than one expression is to be computed on each pass through the loop, as in this case, they must be grouped together using curly brackets.

printf is a formatted print command. Its first argument, "%d %d\n", is a string specifying the format of the output. The remaining arguments, i and x[i], are variables whose values are to be used in the output of the print command. The first %d indicates that the first variable, i, should be printed as an integer. The second  %d indicates that the second variable, x[i] , is also to be printed as an integer. In the expression "%d %d\n" , note that there is a single-character space between the two instances of %d, indicating that the two values of the two variables should be separated by a single space. The expression \n stands for "new line" (meaning "carriage return, line feed").

Exercise 2.9.
See what happens if the space and new line symbol are not included in the format specification of the previous exercise.

Exercise 2.10.
Modify the program's sampling rate, tone frequency and duration of the signal generated.

Exercise 2.11.
Exercise 2.8 writes out the sample number and sample value. By redirecting that output to a file, those numbers can be stored and inspected. This can easily be done in the MS-DOS Prompt window by typing, e.g.

bleep >cosine.txt

(You can call the text file anything you like.) Now you can examine the text file with an editor like PFE, or WordPad, or even import it into Excel.

What are the differences between the output and the signal file? Hint: the character stiring "12" is not the same as the binary number 1100, even though they mean the same thing.

2.15. Reading in preparation for the next lecture

Klatt, D. H. (1980) Software for a cascade/parallel formant synthesizer. Journal of the Acoustical Society of America 67 (3), 971-995.