Assignment 1: Audio Processing

Due Wednesday 2/12/2020

The purpose of this assignment is to get students familiar with numpy arrays and some audio concepts with a fun application. Please visit this link to get the starter code for this assignment. You will be editing the file HW1_AudioProcessing.ipynb.

Part 1: Comb Filters (10 pts)

We talked about comb filters in class. Amazingly, when you a bunch of echoes of random noise to itself, it makes a pitched sound whose fundamental frequency is equal to 1 over the period of the shift. This is like what happens in a resonant cavity of a particular length.

In this part of the assignment, you will fill in a function get_comb_sound which creates the result of applying a comb filter to random noise. This function takes as input the following:

  • fs: The sampling frequency (number of samples per second)
  • note: The number of halfsteps above a 440hz "concert A" the note is. Recall that the formula for the fundamental frequency is

    \[ f = 440*2^{h/12} \]

    Then, the shift between pulses in the comb filter is the period associated to this frequency, which is 1/f.
  • duration: The number of seconds of audio to generate.
  • n_pulses: The number of pulses to use in the comb filter. They should all be equally spaced by the period you find.

Here are the steps you should follow to implement the function. Note that all code should be written in the get_comb_sound function before the return statement

  1. Compute the frequency using "note" with the familiar formula 440*(2(note/12))
  2. Use this frequency to figure out a period (T) of oscillation. Note that the sample rate equals the frequency times the period, which will help.
  3. Create a "comb filter" h which is all zeros except for a "pulse train" of 1s with a period T in between each pulse. There are examples of how to do this in the class notebook using array slices. But the key is the spacing should be in terms of the period T. Also, be sure that your h array is long enough so that it has n_pulses unique pulses.
  4. Convolve the random samples x with your comb filter h, and return the result

After you implement the function, please answer the following questions:

  • In your notebook, please describe what changes in the sound as you add more pulses to the comb filter. You can do this using a markdown cell.
  • In your notebook, please discuss what happens to the Fourier Transform of the signal when you add more pulses to the comb filter. Based on what you heard, why do you think this is happening? Please submit at least two plots with your final submission to demonstrate this.

Tips

  • If you pass in a decimal number for the duration (which you should be able to do), then the total number of samples will be of a fractional type. We need to "truncate" this number (drop the decimal), or numpy will not be happy using this as the length a list. This is done for you. You will need to do a similar thing with the period between pulses in the comb pulse train.

Part 2: Square Waves (10 pts)

A square wave of a particular frequency can be obtained by starting with a sinusoid \[ f(t) = sin(2 \pi f t) \] and "quantizing" it by making all positive values 1 and all negative values -1. The figure below shows what this looks like:

Create a square wave of a particular frequency by first starting with a sine wave, and then passing it to the np.sign() function (the "sine" and "sign" get a bit confusing here). To show you what np.sign does, here's an example
x = np.array([-1.1, -0.5, 0.01, 100])
np.sign(x)

will give
[-1, -1, 1, 1]
If you've gotten this to work properly, it should look like the picture above.

Once you get this to work, have a listen to the sound, and plot the magnitude of its Fourier Transform. Describe what pattern you notice, and come up with an approximation of the square wave by adding sines together. Verify visually and by listening that this approximates the square wave. Please make sure your final notebook contains a plot of the square wave, a plot of its Fourier Transform, and a plot of your sinusoidal approximation.

For your own edification, note how your approximations sound a bit like a clarinet. Click here to see why this is the case.

Tips

  • Hint: This is like the triangle wave series we went over in class, except it only uses half as many sinusoids.

Part 3: Musical Statement (5 pts)

An echo pattern is also known as an impulse response. The comb filters we've looked at so far are the impulse responses of 1D resonant cavities with no loss, but you can measure much more complicated and realistic impulse responses by clapping or popping a balloon in some environment and then recording all of the echoes that come back. You then use these recorded impulse responses to simulate what any sound would sound like in that environment. For example, consider the following audio clip:

Now consider the following impulse response, which was obtained from someone clapping in the JFK tunnel

When we do convolution of the audio clip by this impulse response (also called "convolving" the audio clip with the impulse response), the result is as follows

It really sounds as if "Jessie's Girl" was played in the JFK tunnel!

Your task in this part of the assignment will be to record an impulse response and find a sound to convolve it with. There will be a class wide competition to see who can come up with most creative convolution, and the winner will receive 5 extra credit points on the assignment.

NOTE: You will need to convert any audio you want to use to a .wav file before you can use the above code. You can do this using a program called Audacity. Or, if you're having trouble, simply e-mail me the files and I'll convert them for you. I want to help you realize your creative vision!

What to submit

When you are finished the above tasks, please submit your HW1_AudioProcessing.ipynb file to canvas, along with your impulse response, your audio file of choice, and the result of convolution for part 3. Please also submit a README with the answers to the following questions:

  • Approximately how many hours it took you to finish this assignment (I will not judge you for this at all...I am simply using it to gauge if the assignments are too easy or hard)
  • Your overall impression of the assignment. Did you love it, hate it, or were you neutral? One word answers are fine, but if you have any suggestions for the future let me know.
  • Would you like your impulse response + sound to be posted publicly on the class web site? If so, what name/pseudonym would you like to use?
  • Any other concerns that you have. For instance, if you have a bug that you were unable to solve but you made progress, write that here. The more you articulate the problem the more partial credit you will receive (fine to leave this blank)