ECE-1021

HOMEWORK #8

(Last Mod: 27 November 2010 21:38:42 )

ECE-1021 Home


PROGRAM A - Complex Math

Programming Problem #10.3 (p545).

As the problem states, you are to use the structure that is defined in Section 10.2. But it is important to recognize that Section 10.2 implements these various operations as macros and you are being asked to implement them as functions. Functions are not macros - you can determine what each operation is by studying the macros in Section 10.2 and get a good idea of how to implement them as functions, but don't assume you can copy them unaltered and expect them to work.

It is also important to recognize that the function descriptions in Problem 10.3 imply certain things. First and foremost, they imply that the structures will be passed to these functions by value and that the functions, where appropriate, return them by value. This is important because some of these functions, such as add_c(), have to return "new" complex values. We have not yet discussed dynamic memory allocation which would be required for the proper behavior of such functions were we to use pointers to these structures.

You are to implement these functions using the concept of "utility" and "primitive" functions. In this context, a primitive is a basic function that is allowed to directly access, either for read or write, the contents of a structure. These are sometimes referred to as "GetSet" functions because they usually appear in pairs - one of which gets the contents of a specific member element of the structure and another which sets that same element. The idea is that the primitives form an input/output interface to the structure.

For the structure as given in Section 10.2, you should have four GetSet functions with the following prototypes:

Use the typedef statement (Section 10.3) to create a type called COMPLEX.

double GetCOMPLEXreal(COMPLEX a);

double GetCOMPLEXimag(COMPLEX a);

double SetCOMPLEXreal(COMPLEX *a, double real);  //returns real

double SetCOMPLEXimag(COMPLEX *a, double imag);  //returns imag

You should then use these functions to create the following low level utility functions:

Re(a): returns the real part of the complex number a

Im(a): returns the imaginary part of the complex number a

cartesian(real, imaginary): returns a complex number given that number's real and imaginary components.

Mag(a): returns the magnitude of the complex number a

Arg(a): returns the arg (the angle in radians) of the complex number a

polar(magnitude, arg): returns a complex number given that number's magnitude and arg.

The functions listed in the problem should then be implemented using only the low level utility functions above.

PROGRAM B - WAV reader/writer

Write a program that accepts a filename (with extension) as a command line argument or prompts the User for the filename if one is not supplied on the command line.

If the extension is .txt, then the file is an ASCII text file where the first line is a header and all subsequent lines are sound sample data. The header contains the following data separated by commas: Number of channels, Significant bits per sample, and Sample rate (samples per second). All remaining lines contain one sample per channel per row (e.g., if there are four channels, then there are four values per row). Your program is to use this information to create a WAV file. You may assume there is no compression and no extra format bytes. You may not assume that the data is consistent with the format in which WAV data is to be stored. The values in the file are to be read in as doubles and scaled so that the average value of all the data is at the midpoint of the range of data in the WAV file and so that the global deviation anywhere in the file is 90% of the maximum that the range permits. You might consider reading the file twice - once to determine all of the parameters needed to write the file WAV and the second to actually write it.

If the extension is .wav, then the file is a WAV file and you are to dump it's data to a text file. The text file format should be compatible with the description above. Note that writing the data to the file without modification is consistent with this description.

PROGRAM C - Complex Math in Polar - EXTRA CREDIT

Change the structure used in Program A so that it contains the magnitude and the arg instead of the real and imaginary parts. After creating a set of GetSet primitives for it, use them to write versions of the six utility functions that have the same behavior as the six above. The names of the structure, typedef, and functions should be identical to the above. Use #ifdef and related directives (Chapter 5) to let the programmer select which set of definitions is to be compiled via a #define constant called COMPLEX_POLAR. Do not change any other functions. If you have done Program A the way you were supposed to, this program is extremely straightforward.