ECE 1620 - Assignment #5 - Due: 18 Feb 2022


  1. a5/p1.c - Sum or Product of Sine Waves

    Write a function in the form double f(double t) to compute and return the sum of two sine waves with periods T1 and T2 at a point t:

      y = sin(2πt/T1) + sin(2πt/T2)
    
    with T1 = 20/3 and T2 = 8.

    In the main program, call the function in a loop for values of t from 0 to 100 in steps of 1.

    Define preprocessor symbolic constants for π, T1, T2, and 100 (the final t value). (notes)

    The output must be suitable for plotting, i.e. just one column of numbers (the y values), with no headings.
    Plot the program output using the VECR 2D plot button:
    Your plot should look similar to the one in the class notes for sum of two sine waves.

    Optional: if you change the addition to multiplication in the formula for y, your plot should look similar to the one in the class notes for product of two sine waves. You can also plot the frequency content of the sum or product output using the Discrete Fourier Transform (DFT) by piping to the VECR dft program using args: | dft

    Optional: if you change the number of points from 101 to 50000 then the output can be played as audio using the VECR audio/speaker button.


  2. a5/p2.c - Noisy Sine Wave

    Write a program to produce 101 samples of a noisy sine wave based on the formula:

      y = A*sin( w*t + p) + n
    
    for t = 0, 1, 2, ..., 100 where the amplitude A, radian frequency w = 2*pi/T, phase shift p, and additive noise n are randomly generated in the following ranges:
         1.0 <= A <= 2.0
        20.0 <= T <= 40.0
         0.0 <= p <= 2*pi
      -0.3*A <= n <= 0.3*A
    
    Program output must be suitable for plotting.

    A, T, w, and p must be randomly generated just once at the beginning of the program, then they remain constant for the rest of the program. For each value of t from 0 to 100, generate a new value for the noise n to be added to the sine wave.

    #include <time.h> and at the beginning of main use srand(time(0)) to initialize rand with the current time.

    You can use the rand_float function from the end of chapter4_7.c

    Save the output of one run of your program in a file for use as input to problem #3, i.e. Run with args: > p2.out

    Sample output and plot.


  3. a5/p3.c - Three-point Moving Average

    1. Start with a copy of Perry/chapter3/scanf_double.c (use the Copy button at bottom of VECR) which copies doubles from standard input to standard output using scanf() and printf().

    2. After compiling, test the program with input redirected from the file you created in problem #2, i.e. p2.out, by using the 2D plot button with args: < p2.out which should display a noisy sine wave.

    3. Modify the program to perform a moving average using a three-point sliding window, and print the averages, one value per line, with no table headers, so it is suitable for plotting.

      For the three-point sliding window use three doubles, say y1, y2, and y3, all initialized to zero, and every time the program reads a new input value, say d, slide the window by doing: y1 = y2; y2 = y3; y3 = d; so y1, y2, y3 will always hold three consecutive input values. The average to print is just (y1+y2+y3)/3.

    4. After compiling, test the program with input redirected from file p2.out by using the 2D plot button with args: < p2.out which should show that much of the noise in the sine wave has been removed by the averaging.

    Sample output and plot.


  4. a5/p4.c - Birthday Simulation

    Write a program to generate 30 random birthdays, i.e. random integers in the range 1 to 365, one per line.

    Test using the VECR test link above the Run button, which will run your program and sort/count the output, making it easy to see collisions. Sample output and test.