Instructions | Deliverables | Walkthrough | References

Detailed Instructions

These instructions are optional. You can follow any method you like, in any programming language you like, as long as it results in the deliverables listed below under deliverables.

  1. Download and install anaconda.
  2. Open spyder, the integrated development environment. The left side of the screen is a text editor, showing the file temp.py. The right-hand side should have some variable navigation stuff at the top, and a tiny console window at the bottom. I recommend closing the variable navigation window, and any windows that are hidden beneath it, so that the console window can grow to fill the whole right side of your screen.
  3. The open script file is called temp.py. Choose save as from the file menu (or use Ctl-Shift-s) to give it a better name, perhaps something like lab1.py. While you're doing this, also choose a better directory in which to put it -- some place where you'll remember it.
  4. In the console, use the cd command to change your working directory to the same one that has the lab1.py file.
  5. Enter the line 2+2 in the console, and hit return. It should say 4.
  6. Enter the line 2+2 in the file lab1.py, then hit Ctl-S to save it. Now run it. You can do this either by hitting the green arrow at the top of the screen, or by typing runfile('lab1.py') in the console window. In either case, you should see the result 4 appear in the console window.
  7. We're going to use three libraries for this lab. The numpy library is used to generate the time axis (it has many other uses that we'll discuss later). The math library is used for trigonometric functions. The matplotlib.pyplot library does the plotting. Import these by adding the following lines in your lab1.py file:
    import numpy as np
    import math
    import matplotlib.pyplot as plt
    Run it (using the green arrow, or runfile). Now, in your console window, type dir(plot). You should see a long listing of all the methods available in the matplotlib.pyplot module.
  8. We'll define five variables: sampling frequency, frequency and phase shift of the cosine, period of the cosine, and length of the plot window. Underneath your previous code in lab1.py, enter something like this:
    Fs = 16000
    Omega = 2 * math.pi * 440
    theta = math.pi / 4
    T0 = 2 * math.pi / Omega
    windowlength = 2 * T0

    Run the file. Now in the console window, if you type theta and hit return, it should tell you the value of pi/4 (about 0.79).
  9. Now create the time axis, and the cosine. The first line creates a time axis with Fs*windowlength samples between t=0 and t=windowlength using the numpy linspace function. The second line computes the cosine of each time step, times Omega, shifted by theta. This line uses a slightly obscure but amazingly useful python syntax called list comprehension. If you've never used list comprehension before, study the list comprehension tutorial page, and try a few examples in the console window, until you understand basically how it works.
    t_array = np.linspace(0,windowlength,int(Fs*windowlength))
    x_list = [ math.cos(Omega*t - theta) for t in t_array ]
    Now run the file. In the console window, if you type t_array, you should see a printout of all of the time samples; if you type x_list, you should see a printout of the cosine evaluated at all time samples.
  10. Now create the plot, add xlabels and ylabels, and save it to an image file. Put these commands into your lab1.py file, and run it.
    plt.plot(t_array,x_list)
    plt.xlabel('Time (seconds)')
    plt.ylabel('Pressure (Pascals)')
    plt.savefig('lab1output1.png')
  11. Modify your lab1.py file to change the amplitude, frequency, and phase shift of the cosine (trick: we never defined an amplitude variable. How do you define one?). Save the result to a different image file, lab1output2.png, and hand in both.

Deliverables

Upload, to Compass (page will be open by Monday), a ZIP file containing the following three files:
  1. An image file lab1output1.png showing two periods of a cosine with amplitude=1 Pascal, frequency=440 Hertz, and phase shift=pi/4 radians. Abscissa must be labeled in seconds, ordinate must be labeled in Pascals.
  2. An image file lab1output2.png showing two periods of a cosine at some other amplitude, frequency, and phase shift, with labeled abscissa and ordinate.
  3. Code that produces the second image.
    • If your code is a .py file that runs under spyder, you can assume that I know how to run it.
    • Otherwise, comments at the top of the file must specify (1) exactly what version of what software you are using to run your code, and (2) exactly what line I must enter, and into which window, in order to cause the code to run.

Walkthrough

Here is a walkthrough of lab 1.

References

Your code can be in any programming language you like. I will be demonstrating a python solution here, using numpy.linspace to generate time, row, and column axes, matplotlib pyplot to plot functions, matplotlib image to show images, sounddevice to record and play audio, wave to play audio, and standard python tools to read and write text files. These toolkits (and scipy, which we will need later in the semester) are all pre-installed in the anaconda distribution of python, which is the one I will be using.