Week 3: Timbre And Harmonics
Chris Tralie
Learning Goals
- Practice allocating numpy arrays and performing element-wise operations on numpy arrays.
- Practice loops in python in the service of modeling natural sounds using harmonics.
Background
As we saw at the end of module 3, vibrating strings contain harmonics because of the geometry of a string. So, for example, a 440hz base frequency will also contain harmonics at 880hz, 1320hz, etc. But for other sounds that may also have harmonics, nothing's to say that any of these harmonics need to have the same amplitude. Furthermore, some of the harmonics may even be missing. In fact, both of these things determine the timbre, or qualitative aspects of the resulting sound. In other words, the difference between a 440hz trumpet sound and a 440hz plucked string.
In this exercise, we will explore different types of patterns of harmonics and to plot the waveforms and listen to the sounds. Then, we'll see if we can relate them to some other fundamental waveforms we've generated in the course so far.
Here are a few things to think about as you mess with these things:
- Does this look our sound like anything we've seen before?
- Does this sound like any instruments we've heard?
- What happens to the waveform and the sound when you use more or fewer harmonics?
Exercise 0
Create a pure 440hz tone for reference to compare to what you'll do below.
Exercise 1
Create a waveform what is a sum of pure sine waves at a 440h base frequency, where the amplitudes of the harmonics are in a "harmonic sequence" ratio; that is, the base frequency has a ratio of 1, the first harmonic (2x the frequency) has a ratio of 1/2, the second harmonic has a ratio of 1/3, etc. Plot the first 300 samples of the audio, and also listen to the sound.
Exercise 2
Write a loop to create a waveform that is similar to the one above, except use cosine instead of sine, only include the odd harmonics. Furthermore, every other odd harmonic should be negated. For example, for 440hz base, the amplitudes of sinusoids would be
- 440hz: 1
- 1320hz: -1/3
- 2200hz: 1/5
- ...
Exercise 3
Write a loop to add all odd powers with alternating positive and negative frequencies, just like before, but this time, use sines, and scale the amplitude down by 1 over the index squared instead of just one over the index.