Synthesizing and Synchronizing Snowy Tree Crickets¶

Introduction¶

Synchronizaton events are remarkably common in biological systems. Many of these events take place in hard-to-see places (e.g. pacemaker cells of hearts, inside the hippcampus, and in insulin-secreting cells in the pancreas) [1]. Some, however, are spectacularly apparent. One example is firefly synchronization, which happens for a few weeks each year at a few places around the world. Another is snowy tree cricket chirp synchronization, which occurs for much of the year right here in Ithaca and across the United States! Go outside on many evenings and you'll hear it happening.

The snowy tree cricket is interesting for a number of reasons. In addition to synchronizing their chirps, these crickets change the frequency and speed of their chirps as a function of the ambient temperature. For this reason, they are nicknamed the "thermometer cricket." Here on the east coast, you can estimate the temperature outside (in Fahrenheit) by adding 40 to the number of chirps that you count in 15 seconds. In other parts of the country the linear relationship is slightly different, so it's a good idea to calibrate your crickets before taking any measurements. Fascinatingly, this relationship between temperature and chirp-rate has a name (Dolbear's Law). Even if you grew up in a part of the world without snowy tree crickets, you may still recognize their sound! Snowy tree crickets are often dubbed into soundtracks for movies and television to help depict summer evenings.

In this laboratory exercise, you will synthesize two artificial crickets - one on each of the dual-core processors in the RP2040 microcontroller. These crickets will be synthesized using Direct Digital Synthesis and sound shockingly real. You will then use a microphone to detect cricket chirps. This will involve calculating an FFT and looking for power at the chirp frequency. Finally, you will implement a synchronization algorithm so that your two crickets synchronize. Once everyone proves that their own crickets synchronize, we will turn on all of the crickets in the class to attempt class-wide synchronization of over 100 synthesized snowy tree crickets. Will it work? I don't know! But it will be fun to try.

Key concepts: Timers, timer interrupts, direct digital synthesis, SPI communication, ADC, DMA, FFT's, GPIO, VGA, multicore, threading, microphones, synchronization of pulse-coupled oscillators, DAC, fixed-point arithmetic

Demonstration¶

I've embedded a demo below of the system that you will build. Note: watch the demo with headphones. One cricket is synthesized into the left ear, and the other is synthesized into the right ear.

Experience shows that students prefer these webpages short. For that reason, please find the reading and background materials on the webpages linked below. Please note that the information in these readings will be critical for completing the lab.

Engineering background¶

• Building Demo Code: This webpage describes how to download and build the course demo code on the laboratory computers.
• Lab 1 Incremental Examples: In this lab, as in all labs, you are provided with demo code from which to start your assignment. This repository contains a sequence of example projects which incrementally build to the demo code from which you should start your assignment. The documentation for each of these examples is meant to be read in order, starting from the Blinky Demo and moving down.
• SPI Communication: The digital-to-analog converter that we are using is an SPI device. This webpage describes SPI communication. Understanding these concepts will make configuring the SPI channel on the RP2040 much easier.
• VGA Driver: This webpage describes the VGA driver that you will use for visualizing the FFT on the VGA display.
• RP2040 Datasheet: Sections on DMA, GPIO, UART, SPI, Timer, ADC
• C SDK guide: Same sections as above
• DAC datasheet
• Microphone datasheet

Mathematical background¶

• Direct Digital Synthesis: This is the algorithm that we will use for sound production. This is a powerful algorithm, a good one to understand deeply.
• Cricket synthesis: This webpage describes how to use Direct Digital Synthesis (see above) to create a cricket call.
• Fast Fourier Transforms: You will compute an FFT (Cooley-Tukey FFT) of audio samples gathered from a microphone. You will detect a cricket by looking for power at the chirp frequency. This webpage describes how to implement an FFT, and derives Cooley-Tukey from the sine-cosine series.
• Synchronization algorithm: This links to a webpage that describes the algorithm in "Synchronization of Pulse-Coupled Biological Oscillators" by Mirollo and Strogatz (1990). This is the algorithm that you will implement to synchronize your crickets. In class, we will discuss implementation strategies on the microcontroller.
• Fixed-point arithmetic: Sound synthesis requires meeting strict timing deadlines, which means you may not have time to do your computation in floating point. Fixed-point arithmetic allows for you to use integer arithmetic (which is way faster than floating point, on this architecture) while maintaining fractional resolution.

Weekly checkpoints¶

Week 1¶

• Using this webpage as a guide, download and build the course demo code on a laboratory PC.
• In this example code, direct digital synthesis is used to generate two pure sine wave "beeps" of different frequencies - one from each core. Connect the DAC to the Pico per the #define statements at the top of the source code and the DAC datasheet, build the code, and load it onto the Pico. Please note that different manufacturers use different names for signal names! Here are the correct connections:

3.3V(OUT) --> VDD
GND --> VSS
PIN_CS --> $\overline{\text{CS}}$
PIN_SCK --> SCK
PIN_MOSI --> SDI
LDAC --> $\overline{\text{LDAC}}$

• Confirm that waveforms are being generated by the DAC with the oscilloscope, then use a 3.5mm audio socket so that you can plug in speakers and listen to the generated beeps.

Week 2¶

• Modify the "beep beep" example code such that each core produces a cricket chirp, as described on the cricket synthesis page. I recommend putting the chirp state machine in the interrupt service routine.
• These chirps must be the correct frequency and must repeat with the correct timing. They must be amplitude-modulated. There should be no audible clicks or pops. In particular:
• 8 syllables
• Syllable frequency = 2300 Hz
• Syllable length = 17 ms
• Syllable repeat interval = 2 ms
• Chirp repeat interval = 260 ms
• Prove, with the oscilloscope, that the above specifications are being met.
• Add two buttons so that you can pause/play either cricket.
• Please make these parameters easy to change in your code! For class-wide synchronization, we may want a longer chirp repeat interval.

Week 3¶

• In this example code, the ADC is used to sample audio and then the RP2040 calculates/visualizes an FFT.
• Run this demo code while sampling the microphone. Confirm functionality with a tone generator. The demo code uses analog input 0. Adjust the gain potentiometer on the back until you get good response at the chirp frequency.
• Modify this code such that it detects one (and only one!) chirp each time a cricket chirps. Print out "Chirp!" to a serial terminal each time you hear one. You can test with the synthesized crickets on the cricket synthesis webpage linked above.
• Merge this code with your cricket synthesis code. Your system should produce a cricket chirp from each core. Each core should detect the other core's chirps but not it's own chirps.

Week 4¶

• Implement the synchronization algorithm described in the link above so that each cricket adjusts its time to fire when it hears the other cricket. Over time, your two crickets should synchronize and remain sychronized.

Once all groups have demonstrated synchronization between their two crickets, we will attempt class-wide synchronization.

Lab report¶

Your written lab report should include the sections mentioned in the policy page, and:

• A scope display of a syllable, with cursors placed to measure syllable length
• A scope display of the end/start of two subsequent syllables, with cursors placed to measure syllable repeat interval
• A scope display of a full chirp, showing 8 syllables
• A scope display of two chirps, with cursors placed to measure chirp repeat interval
• A heavily commented listing of your code.