We will be using direct digital synthesis to generate the call of the northern cardinal. Specifically, this adult male northern cardinal recorded by Gerrit Vyn in 2006. This bird was recorded in Texas, but cardinals are also common in Ithaca and throughout the eastern United States. If you pay attention when you're walking through campus, you may hear one singing. The males are a very striking red. You can read more about the cardinal here.
Cardinals have a variety of songs and calls. We will be synthesizing on of its most common songs, which you can hear in the first ten seconds of the recording below:
Here is a screenshot of the spectrogram for the song that we'll be synthesizing. Cardinals and many other songbirds produce almost pure frequency-modulated tones. As can be seen in the spectrogram below, the cardinal sweeps through frequencies from ~2kHz to ~7kHz. We'll assume that the dominant tones (the darkest lines on the spectrogram) are significantly louder than all other frequencies (the lighter lines). We'll only synthesize these loudest frequencies. The generated song sounds quite realistic under this assumption. Your synthesizer will be controlled via a Python-based user interface.
This song can be decomposed into three sound primitives: a low-frequency swoop at the beginning of each call, a chirp after each swoop which moves rapidly from low frequency to high frequency, and silence which separates each swoop/chirp combination. We will synthesize each of these primitives separately, and then compose them to reconstruct the song.
By pasting the above spectrogram in Keynote and drawing lines on it, you can determine that the length of the chirp is approximately 130 ms. Since the DAC gathers audio samples at 44kHz, this means that the chirp lasts for $0.130\text{sec} \cdot \frac{44000\text{ samples}}{1\text{ sec}} = 5720\text{ samples}$. We'll approximate the frequency curve by sine wave of the form:
where $y$ is the frequency in Hz, and $x$ is the number of audio samples since the chirp began. Since the swoop starts and ends at 1.74kHz and peaks at 2kHz, we can setup the following system of equations to solve for the unknown parameters $k$, $b$, and $m$:
From which we can find $b=1740$, $k = -260$, $m = \frac{-\pi}{5720}$. So, the equation is given by:
Plot the simulated swoop of frequencies:
plt.plot(-260*numpy.sin(-numpy.pi/5720*numpy.arange(5720)) + 1740)
plt.xlabel('Audio samples'); plt.ylabel('Hz'); plt.title('Swoop frequencies')
plt.show()
Some DDS parameters, including the sample rate (44kHz), a 256-entry sine table, and the constant $2^{32}$:
Fs = 44000 #audio sample rate
sintable = numpy.sin(numpy.linspace(0, 2*numpy.pi, 256))# sine table for DDS
two32 = 2**32 #2^32
And now we can synthesize audio samples.
swoop = list(numpy.zeros(5720)) # a 5720-length array (130ms @ 44kHz) that will hold swoop audio samples
DDS_phase = 0 # current phase
for i in range(len(swoop)):
frequency = -260.*numpy.sin((-numpy.pi/5720)*i) + 1740 # calculate frequency
DDS_increment = frequency*two32/Fs # update DDS increment
DDS_phase += DDS_increment # update DDS phase by increment
DDS_phase = DDS_phase % (two32 - 1) # need to simulate overflow in python, not necessary in C
swoop[i] = sintable[int(DDS_phase/(2**24))] # can just shift in C
In order to avoid non-natural clicks, we must ramp the amplitude smoothly from 0 to its max amplitude, and then ramp it down. We'll do this by multiplying the chirp by the linear ramp function shown below:
# Amplitude modulate with a linear envelope to avoid clicks
amplitudes = list(numpy.ones(len(swoop)))
amplitudes[0:1000] = list(numpy.linspace(0,1,len(amplitudes[0:1000])))
amplitudes[-1000:] = list(numpy.linspace(0,1,len(amplitudes[-1000:]))[::-1])
amplitudes = numpy.array(amplitudes)
plt.plot(amplitudes);plt.title('Amplitude envelope');plt.show()
# Finish with the swoop
swoop = swoop*amplitudes
Here is what the swoop looks like:
plt.plot(swoop);plt.title('Amplitude-modulated swoop');plt.show()
And here is what it sounds like:
Audio(swoop, rate=48000)
By pasting the above spectrogram in Keynote and drawing lines on it, you can determine that the length of the chirp is also approximately 130 ms (5720 samples). We'll approximate the frequency curve by a quadratic equation of the form:
where $y$ is the frequency in Hz, and $x$ is the number of audio samples since the chirp began. Since the chirp starts at 2kHz and ends at 7kHz, we can setup the following system of equations to solve for the unknown parameters $k$ and $b$:
This is two equations with two unknowns. Solving, we find that $b=2000$ and $k\approx 1.53 \times 10^{-4}$. So the quadratic equation for the chirp is:
Plot the simulated chirp frequency sweep:
plt.plot(1.53e-4 * numpy.arange(5720)**2. + 2000);plt.title('Chirp frequencies');plt.show()
And now we can synthesize audio samples.
chirp = list(numpy.zeros(5720)) # a 5720-length array (130ms @ 44kHz) that will hold chirp audio samples
DDS_phase = 0 # current phase
for i in range(len(chirp)):
frequency = (1.53e-4)*(i**2.) + 2000 # update DDS frequency
DDS_increment = frequency*two32/Fs # update DDS increment
DDS_phase += DDS_increment # update DDS phase
DDS_phase = DDS_phase % (two32 - 1) # need to simulate overflow in python, not necessary in C
chirp[i] = sintable[int(DDS_phase/(2**24))] # can just shift in C
In order to avoid non-natural clicks, we must ramp the amplitude smoothly from 0 to its max amplitude, and then ramp it down. We'll do this by multiplying the chirp by the linear ramp function shown below:
# Amplitude modulate with a linear envelope to avoid clicks
amplitudes = list(numpy.ones(len(chirp)))
amplitudes[0:1000] = list(numpy.linspace(0,1,len(amplitudes[0:1000])))
amplitudes[-1000:] = list(numpy.linspace(0,1,len(amplitudes[-1000:]))[::-1])
amplitudes = numpy.array(amplitudes)
# Finish with the chirp
chirp = chirp*amplitudes
The entire amplitude-modulated chirp looks like this:
plt.plot(chirp);plt.title('Amplitude-modulated chirp');plt.show()
And it sounds like this:
Audio(chirp, rate=44000)
The amount of time between swoop/chirps is also (approximately) 130ms or 5720 cycles.
silence = numpy.zeros(5720)
We assemble the song by playing the swoop/chirp/silence in succession:
song = []
for i in range(5):
song.extend(list(swoop))
song.extend(list(chirp))
song.extend(list(silence))
song.extend(list(swoop))
song.extend(list(chirp))
song.extend(list(silence))
song = numpy.array(song)
plt.plot(song);plt.title('Full song');plt.show()
Listen to it:
Audio(song, rate=44000)
And view the spectrogram of the Python-generated song:
f, t, Sxx = signal.spectrogram(song, Fs)
plt.pcolormesh(t, f, Sxx, shading='gouraud')
plt.ylabel('Hz'); plt.xlabel('Time (sec)')
plt.title('Spectrogram of Python-generated birdsong')
plt.ylim([0,10000])
plt.show()
The Big Board which you will be using features a port expander, DAC, TFT header-socket, programming header-plug, and power supply. See the construction page for specific code examples of each device on the big board. The connections from the PIC32 to the various peripherals is determined by the construction of the board. The list is repeated here.
Any pin can be recovered for general use by unplugging the device that uses the pin (of course, if you're doing this lab remotely, you will not be able to unplug these devices). SPI chip select ports have jumpers to unplug.
RA0 on-board LED. Active high.
RA1 Uart2 RX signal, if serial is turned on in protothreads 1_2_2
RA2 port expander intZ
RA3 port expander intY
RA4 PortExpander SPI MISO
-----
RB0 TFT D/C
RB1 TFT-LCD SPI chip select (can be disconnected/changed)
RB2 TFT reset
RB4 DAC SPI chip select (can be disconnected/changed)
RB5 DAC/PortExpander SPI MOSI
RB6 !!! Does not exist on this package!!! (silk screen should read Vbus)
RB9 Port Expander SPI chip select (can be disconnected/changed)
RB10 Uart2 TX signal, if serial is turned on in protothreads 1_2_2
RB11 TFT SPI MOSI
RB12 !!! Does not exist on this package!!! (silk screen should read Vusb3.3)
RB14 TFT SPI Sclock
RB15 DAC/PortExpander SPI Sclock
But note the few silk-screen errors on the board.
SECABB version 2 silk screen errors. (fixed on version 2.1)
DACA
DACB
Software you will use is freely downloadable and consists of:
(All of this is already installed on the lab PC's). More information can be found at the links below.
- JTAG enable overrides pins 13, 14, and 15
- Primary oscillator enable overrides pins 9 and 10
- Secondary oscillator enable overrides pins 11 and 12
- This is huge, better to go to the PIC32 page then Documentation --> Reference Manual and choose the section
- PIC32MX250F128B PDIP pinout by pin
- PIC32MX250F128B:: Signal Names $\rightarrow$ Pins::1, 2, 3, 4, 5, 6, 7 PDIP highlighted in green (for PPS see next tables)
- PIC32MX250F128B Peripheral Pin Select (PPS) input table
- Example: UART receive pin ::: specify PPS group, signal, logical pin name
PPSInput(2, U2RX, RPB11);
// Assign U2RX to pin RPB11 - Physical pin 22 on 28 PDIP
- PIC32MX250F128B Peripheral Pin Select (PPS) output table
- Example: UART transmit pin ::: specify PPS group, logical pin name, signal
PPSOutput(4, RPB10, U2TX);
// Assign U2TX to pin RPB10 - Physical pin 21 on 28 PDIP
Oscilloscope software:
Here is the pinout for the PICKIT3, the programmer which was used to develop the boards you will be using. On both the big and small boards, J1 marks pin1 of the 6-pin ICSP header.
Signal | PICkit3 (ICSP) connector on board |
---|---|
MCLR | 1 |
ground | 3 |
prog data (PGD) | 4 |
prog clock (PGC) | 5 |
In this lab and in all subsequent labs, you will build a Python-based user interface for controlling the PIC. Please see this page for examples of and information about the Python interface.
The SPI DAC you will use is the MCP4822. The DAC analog output is marked DACA or DACB on the SECABB. The SPI connections are supplied on the Big Board (SECABB), as long as you connect the DAC_CS jumper. Section 5.1 and 5.2 of the MCP4822 datasheet discussed the way the the DAC uses SPI. The edge connector pin marked DACA or DACB will go (optionally through a low pass filter) to an audio socket, to be connected to the green audio plug of the speakers. If you are going to design a lowpass filter for the DAC, then you need to know that the input impedance of the speakers is around 10 Kohm.
Generally speaking you want to make pleasant sounding synthesis. You may want to look at the sound synthesis page. I suggest starting with a linear attack of about 1000 samples, a decay of about 1000 samples, and a sustain as long as necessary for a 130ms chirp/swoop.
I suggest that you organize the program as follows. If a different organization makes more sense to you, that's ok. This is just a suggestion.
- DDS of sine waves
- DAC output
- Sample count for precise timing of sounds
- Parses events from your Python interface
- On "Record mode" to "Play mode" transition (the release of a toggle switch), signals the Play Thread to play the recorded song
- Records a particular button press in a global array.
- If in "Play mode," signals the Play Thread to play the sound associated with that button.
- If in "Record mode," waits to add more button presses to the array
- Waits to be semaphore-signalled by a different thread
- Plays the sound associated with each button press recorded in the global array by setting up the DDS parameters associated with each sound
- Maintains playback timing using the sample count from the timer ISR
You may find some previous year's lectures useful. Particularly lectures 2, 3, 4.
If your program just sits there and does nothing:
YIELD
(or YIELD_UNTIL
, or YIELD_TIME_msec
) in the while(1)
loop?If your program reboots about 5 times/second:
If your thread state variables seem to change on their own:
Note that these checkpoints are cumulative. In week 2, for example, you must have also completed all of the requirements from week 1.
By the end of lab session in week one you must have:
DACA
, Probe 2 <--> DACB
)#include "pt_cornell_1_3_2_python.h"
Timing of all functions in this lab, and in every exercise in this course will be handled by interrupt-driven counters, not by software wait-loops. ProtoThreads maintains an ISR driven timer for you. This will be enforced because wait-loops are hard to debug and tend to limit multitasking
Write a Protothreads C program which will:
- Upon power-up or reset the system should go into play mode and play a sound for each button press (swoop for one button, chirp for another, and 130 ms of silence for a third).
- Pressing a toggle-switch in your Python interface puts the system into record mode so that each button press is recorded for later playback. Recording continues until the record mode toggle is released. The duration of each button press does not affect the recording.
Write a Python program which will:
When you demonstrate the program to a staff member, you will be asked to play back a sequence of sounds which simulates the birdsong above, and to play back a random sequence of swoops/chirps/silences. At no time during the demo can you reset or reprogram the MCU.
If you find this as interesting as I do and you'd like to add some sophistication to your lab, there are lots of opportunities to do so. You might, for example, synthesize other songs produced by the cardinal. If you listen to the full recording at the top of this webpage, you'll hear the sounds associated with the spectrogram shown below. You may deconstruct and synthesize this song also, if you would like.
You can also synthesize songs for other, more challenging birds like the Baltimore oriole. (If you're lucky, you can spot these in Ithaca too. They're gorgeous and have a very distinctive song).
Your written lab report should include the sections mentioned in the policy page, and:
Here is recording of the user interface in action. This was recorded with Zoom. DAC output from the PIC is plugged into the mic input of the lab PC so that audio is available through a Zoom call.
By plugging the Pic into the microphone input of the PC, we can record the sound that it generates and create a spectrogram of the Pic-generated birdsong:
samplerate, data = wavfile.read('./Bird.wav')
frequencies, times, spectrogram = signal.spectrogram((data[80000:191000,0]+data[80000:191000,1])/2., samplerate)
plt.pcolormesh(times, frequencies, spectrogram, shading='gouraud')
plt.ylabel('Hz'); plt.xlabel('Time (sec)')
plt.title('Spectrogram of PIC-generated birdsong')
plt.ylim([0,10000])
plt.show()
import numpy
import matplotlib.pyplot as plt
from IPython.display import Audio
from IPython.display import Image
from scipy import signal
from scipy.fft import fftshift
from scipy.io import wavfile
plt.rcParams['figure.figsize'] = [12, 4]
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
""")