Lab 8: Chirps, Bells, and other Instruments#

Last updated 8/12/24

00. Content #

Mathematics#

  • Laws of sines/cosines

Programming Skills#

  • specialized flow structures (try/finally, for/else)

  • buffers

  • the I2S protocol

  • blocking code

0. Required Hardware #

Write your name and email below:

Name: me

Email: me @purdue.edu

import numpy as np

1. More on Amplitude Modulation: bell envelopes #

In the last couple of labs, we have explored the modulation of a signal by multiplying it with a periodic signal, such as a sine wave or a triangular wave. We have explored how this changes the sound by listening to it. It was observed that, when the frequency of the carrier is low compared to that of the sound, modulating a sound is perceived as a periodical increase/decrease of the volume of the sound. Mathematically, we say that such modulation modifies the “envelop” of the signal. The envelop of a signal represents the maximal/minimal values between which the signal oscillates over time. (Yeah, his is not very precise, but giving a precise mathematical definition would just be sonfusing at this point. -pm) So amplitude modulation, which as the name indicates changes the amplitude of the signal, naturally changes the envelop of the signal. When the carrier value is very small at some point in time, the amplitude of the modulated signal is decreased, thus making the sound less loud at that moment. Conversely, if the carrier value is very large at some point in time, the amplitude of the modulated signal is decreased, thus making the sound louder at that moment.

This visualization can inspire some more creative ways to modify sound. A fun example is created using “bell envelops” \( c(t) = e^{-t/\lambda}\), \(\lambda > 0\), which decays the amplitude of the sound in an interesting fashion. The parameter \(\lambda\) is called the “time constant” and controls the speed of the decay - smaller the \(\lambda\), faster the decay. So to recap, when a signal \(x(t)\) is modulated by a bell envelop, the modulated signal \(y(t)\) is given by:

\[ y(t)= e^{-t/\lambda} x(t), \text{ for some } \lambda >0. \]

Exercise 1 #

Create a middle C sound that lasts one second. You can use your prefered waveform (sine, triangular, square, etc.) to first create a middle C. Then modulate your middle C using a bell envelop. Plot and play audio with different values of \(\lambda\) and describe how this affects the final sound.

Exercise 2 #

Pick a value of \(\lambda\), and then create an entire scale of notes that sound like bells. Use your scale to play a short song. (no need to be a fancy song, just ‘do-re-mi-fa-so-la-ti-do’ is enough. But if you feel creative, go for it!)

Despite the type of the envelop used, the sounds you create do not quite sound like bells. In order to create bell sounds, we will need to use more than one sine wave frequency. So we move on to the next topic: frequency modulation.

2. Frequency Modulation: Chirps (linearly swept frequency) #

We have explored different ways to modulate the amplitude of a signal, i.e. Amplitude Modulation (AM). One can also also modulate the frequency of a signal, i.e. Frequency Modulation (FM). If \(x(t)\) is a single sine wave

\[ x(t) = C \cos(2\pi f_0 t + \phi) \text{ for some } C, f_0, \phi \in {\mathbb R} ,\]

then, after frequency modulation, the modulated signal \(y(t)\) can be written as

\[ y(t)= C \cos( \psi_{f_0, \phi} (t) )\]

for some function \(\psi_{f_0, \phi}(t)\) depending on \(f_0\) and \(\phi\) .

The instanteneous frequency at time \(t\) of a signal of the form \( y(t)= C \cos( \psi_{f_0, \phi} (t) )\) is given by the derivative of \(\psi_{f_0, \phi}(t)\) divided by \(2\pi\):

\[ f(t)= \frac{1}{2\pi} \frac{d \psi_{f_0, \phi}(t) }{dt}.\]

In particular, the instanteneous frequency \(f(t)\) of the original signal \( x(t) = C \cos(2\pi f_0 t + \phi) \) is a constant:

\[ f(t)= \frac{1}{2\pi} \frac{d \left( 2\pi f_0 t + \phi \right) }{dt}= f_0.\]

Now if we modulate the signal using the quadratic function \(\psi_{f_0,\phi}= 2\pi a t^2 + 2\pi f_0 t + \phi \) with \(a\neq 0\), then the instantenous frequency of the modulated signal varies linearly with time:

\[ f(t)= \frac{1}{2\pi} \frac{d \left(2\pi a t^2 + 2\pi f_0 t + \phi \right) }{dt}= 2at+f_0.\]

So the frequency of the modulated signal begins at \(f_0\) at time \(t=0\), and increases/decreases linearly at a rate (slope) of \(2a\) per time unit. The sound produced by such a signal is similar to a bird chirp, hence the name “chirp”.

Exercise 3 #

Create a chirp lasting 0.5 seconds with parameters \(f_0=252\) and \(\phi=0\). Play audio with different values of \(a\).

Exercise 4 #

Repeat Exercise 3, but this time begin your chirp at \(t=-0.25\) and end it at \(t=0.25\). What difference do you notice?

Exercise 5 #

Based on Exercise 3 and Exercise 4, how would you go about creating a chirp that sounds like a middle C? Explain your strategy and code it to check that it works.

Exercise 6 #

Similar to Exercise 2, create an entire scale of notes that sound like chirps. Use your scale to play a short song. (It can be the same song as for Exercise 2.)

3. Putting it all together: Synthesis of Bells and other Music Instruments #

The previous examples have taught you how to synthesize a bell sound and a chirp using basic operations on the frequency and amplitude of a periodic signal. Hopefully, you have developed a bit of an intuition on how these simple operations change the sound you hear. Now we generalize these concepts to be able to synthesize more general music instruments. Following the technique proposed by Stanford Professor John Chowning in 1973, the general equation we will use for the sound of the instrument (one note) is

\[ y(t)= A(t) \]

You will recognize the factor A(t) in front, which controls the envelop of the signal, as in amplitude modulation. You will also recognize the original signal

\[ x(t) = \cos(2\pi f_0 t + \phi) \]

In general the modulation of a signal \(x(t)\) can be written as

\[y(t)= A(t) \cos \left[ 2 \pi f_0 t + B(t) \cos \left( 2\pi f_1 t + \psi_1 \right)+\psi_0 \right]. \]

The function A(t) modifies the envelop of x(t), and as we have seen previously, can take on different forms (e.g. periodic, exponential, etc.). To understand the rest of the expression, we compute the instantenous frequency of \(y(t)\):

(1)#\[\begin{align} f(t) & = \frac{1}{2\pi}\frac{d}{dt} \left( 2 \pi f_0 t + B(t) \cos \left( 2\pi f_1 t + \psi_1 \right)+\psi_0\right),\\ & = f_0 - B(t) f_1 \sin \left( 2 \pi f_1 t + \psi_1 \right) + \frac{1}{2\pi} B'(t) \cos \left( 2\pi f_1 t + \psi_1 \right). \end{align}\]

So the frequency of \(y(t)\) changes with time based on the value of \(f_1\), called the modulating frequency, and the function \(B(t)\), called the (frequency) modulation index envelop. For example, one can create a vibrato around the frequency \(f_0\) using the frequency \(f_1\). The function \(B(t)\) is used to control what other frequency components are present in the signal. In future lab, we will learn how to analyze these frequency components using the DFT. For now, let’s just have some fun and use this model to create a cool music instruments.

Exercise 7 #

Create bell sounds using the following parameters:

\[ A(t) = e^{\frac{-t}{\lambda}}, B(t)= B_0 e^{\frac{-t}{\lambda}}, f_1= 2 f_0, \]

with either

a) \(f_0=110\), \(B_0=10\), \(\lambda=2\), duration 6 seconds

or

b) \(f_0=210\), \(B_0=5\), \(\lambda=2\), duration 6 seconds

or

c) \(f_0=110\), \(B_0=10\), \(\lambda=12\), duration 3 seconds

(Ref: DSP First, a Multimedia approach, p. 451)

4. Update your synthesizer #

Exercise 8 #

Now that you know how to create cool sounds, modify the synthesizer you built in Lab 2, and make it play your own unique notes. You can use the previously synthesized bells or chirps, or create your own personal combination of parameters. Include code below and a description of what was changed.

Reflection #

Do not skip this section! Lab will be graded only on completion of this section.

1. What parts of the lab, if any, do you feel you did well?
2. What are some things you learned today?
3. Are there any topics that could use more clarification?
4. Do you have any suggestions on parts of the lab to improve?

Write Answers for the Reflection Below