This lab was developed by Dr. Steve Barnicki; it has been modified slightly for this class.
The purpose of this lab is to apply loops to write an application using an existing class. In this case, the class implements a filter used in digital signal processing.
In this lab, you will:
You have been asked to design and implement a computer program to implement a digital filter. You have been given only a class library, and you must complete the application to the following specifications.
Digital filtering, a subset of Digital Signal Processing (DSP) has become a very common signal processing technique. Traditionally, computer engineering and computer science students see a few of these techniques in a "Numerical Methods" class, which is a very small subset of DSP. In this lab you will be using a numerical method you may have seen in calculus class, called trapezoidal integration.
With all of these techniques, the data is a waveform that has been sampled. All this means is that every T seconds, the value (voltage) of the waveform is recorded. This gives a "list" of waveform voltage versus time. In this lab you will be generating the waveform and then processing it through a digital filter.
You are to write the main program that will create the square wave file, integrate the square wave, and write an output file. Both files will be read into Quattro or Excel for plotting. Your program must be divided into at least three (3) functions:
1. User input function
The user input function needs to obtain from the user a few key pieces of information:
- How many data points to generate (n)
- The sampling rate or frequency (how often is the waveform sampled) (fs)
(Note: T = 1/fs, T is the time between samples)- The waveform rate or frequency (the "tone" or pitch of the square wave) (fw)
- The amplitude of the square wave (how "loud" is it) (amplitude)
2. Square wave Generator Function
This function will open an output file, and generate the square wave. To generate the square wave you will use a function called Sqr which is very similar to sin function in the standard math library. The equation to generate a square wave from this function is:
voltage = amplitude * Sqr( 2*fw/fs*m)
where fw is the waveform frequency, fs is the sampling frequency, and m is the point number; the point number (m) ranges from zero to (n-1).
The time value for each point is:
time = m * T
which is equivalent to:
time = m * (1 / fs)
In other words, you will generate a total of n data points, with each point consisting of a time value and a voltage value.
It is suggested that you use a for loop in this function. The output file should be in the form of one data point per line in two columns with the first column as time (in seconds), and the second column as voltage. Separate the columns with a space. Do not have text headings at the top of the columns, and don't worry about precise formatting (e.g., making the columns line up exactly). The file you generate for the square wave data should look something like this:
0.000 2
0.010 2
0.020 2
0.030 2
0.040 -2
0.050 -2
0.060 -2
0.070 -2
0.080 2
.
.
.See the square.txt file in the .zip file for a more detailed example. Note that the actual values in the file depend on the parameters (n, fs, fw, amplitude), so don't be concerned if yours don't match the example exactly.
3 . Integrator Function
In this function you will use the class library to integrate the square wave you just generated, and write the integrated data to a file using the same format as your square wave file. So for this function you read in a data point from the square wave file, pass it to the class library filter function, then write that number returned to the integrator output file. Use a while loop as shown on page 849 of the textbook to implement the reading of the square wave file.
The definitions for the primary member functions of the digital filter class are:
float TrapezoidIntegrate(float input); // Process one data point through the digital filter. // This function is called repeatedly in sequence, once for each data // value in the input data stream. // Each time it is called, it processes the input value and returns // the next output value. // Note: the digital filter object maintains internal data during the // processing. For this reason, the filter object lifetime must begin // before calling this function for the first data value and end after // calling this function for the last data value. // Arguments: // input - The input data sample value to process. // Returns: The next output data value from the filter. void Reset(float T); // Initializes the digital filter and sets the time step (in seconds) // to be used in filter processing. // The time step is the "real-world" time between samples. // Arguments: // T - time step (must be positive and nonzero). // Returns: Nothing. |
In addition, you will need to use the Sqr global function (not a member function):
double Sqr(double arg); // Calculate one point of a square wave signal. // Arguments: // arg - normalized angular argument. // This value is multiplied by pi to get the equivalent // radian angle. // Returns: The calculated data point. |
You can find these interfaces in the file dspclass.h. When you need to integrate you would declare a variable of class Filter and call the Reset member function to set the time between samples. To actually do the integration, call the member function TrapezoidIntegrate once for each data point, passing it the input voltage value. It will return the corresponding integrated (filtered) value.
If you have difficulties with any part of the lab, consult the instructor for assistance. The basic sequence is:
Quattro Pro 7.0
- Select QuickColumns... on the Data menu
- Browse to your text file, and specify the destination cell
- Select Options
- Change Datatype to Delimited
- Select Space in the lower part of the options dialog
- Select OK to close the options dialog
- Select Parse to read in your data
- Save the spreadsheet under an appropriate name
- Now you have two columns of data which you can plot with an XY graph (under the Specialty graph types).
Quattro 5.0
- Pick Notebook, text import
- find the file, choose the radio button comma and " delimited file
- Now you have two columns of data which you can plot with an XY, 2D graph.
Excel 7.0
- Open the text file, which will invoke the text import wizard.
- It will pick a delimited file for you, make sure it has chosen space as the delimiter.
- Now you have two columns of data which you can plot with the chart wizard as an XY graph.
The lab report should consist of the following:
You may submit this report in hard-copy form, or email it to the instructor. If you choose email, it may be convenient to put the textual portion of the report directly in the message, and to attach the other files. Be sure to keep copies of all your files, in case something gets lost; it may be wise to keep a diskette backup as well.
The lab report is due by the beginning of the following lab period, though you are encouraged to submit it sooner if you can. Your grade will depend on quality of design and clarity of the code and documentation. If you have any questions, consult the instructor.
In addition to the lab report, please demonstrate your final program to the instructor. This may be done during the lab period, or at another mutually agreed time. The demonstration should be done no later than the first hour of the following lab period.
This page was last updated on October 21, 1997; send comments to Mark Sebern.