Music 220c: FMGuitar


The Begginnings

Last quarter, Winter 2015, I took Stanford's EE264 (Digital Signal Processing) course. As part of a new labratory unit for that course, we were given a Stanford DSP Sheild to implement many of the theory concepts we learned in class. The DSP Sheild is a board made by Stanford in partnership with Texas Instruments. The board is a fixed point DSP chip that can run in a stand alone mode or as an Arduino shield. It runs at 16 bits, has stereo inputs and outputs for audio, and has a sampling rate as high as 48kHz. For this reason it is almost ideal for audio applications. As part of the final project for the EE264 course, I turned this board into a digital distortion pedal for electric guitar. The setup I used for this project is pictured below:



A Turn For the Computer Music

For this project, I wanted to create another guitar effect, since the setup was already there and I play guitar myself, but this time I wanted things to get weird. I wanted this effect to make my guitar sound almost nothing like a guitar, but still respond to the pitch and amplitude of the guitar. What better way to make such an effect and simultaneously pay tribute to CCRMA than by using fm synthesis? During this quarter, the DSP Sheild will be turned into a device to use an electric guitar to play fm synthesis. The pitch and amplitude will be that of the electric guitar, but the timbre will be that of classic fm synthesis. This would involve several DSP concepts, all done in fixed point including:

First Steps: aka Sine Waves are Harder Than They Look

When working with fixed point arithmetic things that were trivial in floating point can often become exponentially more difficult. Nowhere was this more true, at least in my case, than with creating the sine wave oscillators necessary to generate those classic fm tones. There are many algorithms for generating sine waves, for this project I chose to use the Magic Circle algorithm becuase it is one of the simplest and easiest to implement.

Unfortunately, nothing comes easy in fixed point. Calculating the magic circle epsilon value, what is essentially used as the phase increment in the algorithm, requires a division by the sampling rate. In this case, the sampling rate used is 48kHz. This is larger than the maximum 16bit number we can represent. This means that calculating that division can't be done without some difficulty or loss of precision. To get around this issue, a large set of epsilon values was calculated in MATLAB then used as a lookup table in the code. Luckily, the frequency range of a guitar is not that large, they go from about 80Hz to ~1300Hz, so we can generate up to about 3kHz using 2048 points and have a decent amount of accuracy, and give ourselves enough headroom for modulated frequencies.

Overflow was a constant problem when I was implementing the magic circle algorithm. Once the problem was discovered it was a simple fix, but it took forever to notice. Since the magic circle algorithm depends on a previous y and x value, we have some freedom in setting what the initial values of these parameters should be. Typically, in floating point, we can set one of them to 1 and the other to zero. This creates an oscillation that will never go outside the range [-1:1]. Since one is represented in our 16 bit notation by 32768, it follows that setting one of the intial values to this will create a sine wave that oscillates between [-32768,32768]. Simulating this in MATLAB results in the following plot:

At first glance, this looks good, however closer inspection with MATLAB shows that the magic circle generated sine wave actually ranges between [-32775,32755]. This is bad news, any overflow will create serious errors in the generation of the sine waves regardless of whether we handle overflow with staturation or letting the values wrap to negative values. Indeed, what is produced by the board is total garbage. These errors are most likely introduced by the rounding that occurs when perfomring fixed point multiplies. To fix this, we can simply set a smaller starting value for our magic circle algorithm, in the case of the graph below 32000 was used.

Sure enough, changing the initail value reduced the amplitude without affecting the actual sine wave (in both cases we don't really care about the 180 degree phase shift). Using this on the board results in perfect sine waves.

FM Tones(at last!) and A New Direction

To say the least, generating the sine waves took far longer then it should have. Fixed point arithmetic had gotten the better of me and it was about to do so again. The ultimate goal of this project, originally, was to create something that would allow the user to play an fm synthesizer with their guitar, or any instrument really. That means the pitch of the guitar directly controlled the pitch of the carrier, and the evnvelope of the guitar controlled the envelope of the generated fm signal. While the later of these two control signals was easy to generate, the former was proving more difficult.

As a break, I decided to play around with Ring Modulation. Ring Modulation is a common effect in computer music, because it is probably one of the simplest to implement (all you need to do is multiply two signals). However, using ring modulation provides relatively little control over the harmonic content created. The user is simply at the mercy of whatever signals they happen to be multiplying. Using ring modulation with my guitar was...dissapoiting to say the least. But it did get me thinking, what would this sound like using fm synthesis? A synthesis technique where we have almost direct control over the harmonic content created.

Hearing the dsp sheild produce stable, fm synthesis tones was probably one of the most exciting things to happen all quarter. After struggling for weeks with just the sine wave oscillators I was pretty happy when I heard the first sucessfull tone. There were still some stability issues, modulating by a certain ratio above the carrier frequency was a problem among some other things, but once I figured out a colelction of settings that wouldn't cause the board to go unstable I immediately saught to try out my ring mod fm. The results were a little underwelming but there was promise. A lot needed to happen in the way of experimenting and finding parameters that were fun, interesting and playable. The first iteration of the fm ring mod just completely took away some of the playability of the guitar.

Why Buy What You Can Build/Things Get Fun