ECE 8473 Assignment #4 - Due: 20 Oct 2022


Upload source files - individual files or archive (e.g. zip, tar) - to your osp/a4 upload area.

Programs must compile with no warnings or errors using:

  gcc -std=c11 -pedantic -Wall
Each source file must start with a comment containing your name and a description.

References: rand(), plot.sh, scanf_double.c, quadratic interpolation


  1. [30] a4/p1.c - noisy sine waves

    A noisy sine wave is defined by sin(2πft+φ)+r, where f is the frequency in Hz., t is real time, φ is a phase offset in the range 0 to 2π, and r is additive noise.

    For a discrete-time sine wave, discrete-time is an integer i=0,1,2,... and real time is t=i/8000.0, using a sampling frequency of 8000 Hz.

    Write a C program, without creating any arrays, to generate 61 points of noisy discrete-time sine wave data consisting of either a single sine at 1000 Hz. (if there are no command-line arguments) or the sum (command-line argument "s") or product (command-line argument "p") of two sine waves, one at 1000 Hz. and the other at 1200 Hz.

    Generate random values for the sine phases, just once, and random values for additive noise r for each output value, where r is uniformly distributed in the range -0.75 to 0.75.

    Each line of output must consist of a discrete-time value and a sine data value; just the values, no strings or other messages, so the output will be suitable for plotting.

    Sample output and plot.


  2. [30] a4/p2.c - 3-point moving average

    Write a C program, without using any arrays, to read pairs of double precision floating point values from standard input and use a sliding window to compute and write a 3-point moving average to standard output. See scanf_double.c for an example of reading double from standard input.

    A sliding window consisting of the time values t0,t1,t2 and data values y0,y1,y2 would initialize t1=t2=y1=y2=0, then each time a new input pair t,y is read, slide the window: t0=t1; t1=t2; t2=t; y0=y1; y1=y2; y2=y; The 3-point average is simply a=(y0+y1+y2)/3 and the output is the t,a pair.

    The output must be two values per line, just like p1.

    Test your program using input from the output of p1.

    Sample output and plot.


  3. [40] a4/p3.c - sine peak estimation

    Write a C program, without using any arrays, which uses a 3-point sliding window to detect peaks in noisy sine wave data read from standard input, and uses quadratic interpolation to estimate and display the locations of the peaks.

    For input data, use the output of p1 or p1|p2.

    For three consecutive data samples, y0,y1,y2, starting after the first three data points have been read, a peak is detected when y1 > y0 and y1 > y2. To estimate the location of the peak, use quadratic interpolation.

    Program output must include a copy of the input data (two values per line, suitable for plotting as before), together with additional t,y values to "mark" the plot with vertical lines at the times where peaks were detected.

    To create extra output points which will not be connected to the rest of a plot, just separate with an empty line. For example, this data: "1 1\n2 2\n\n1.5 0\n1.5 4\n\n2 2\n3 3" will draw a vertical line from (1.5,0) to (1.5,4) while plotting the sequence (1,1), (2,2), (3,3).

    Sample noisy output and plot. Sample averaged output and plot.