Building an electronic guitar digital sound synthesizer using a programmable SoC - Embedded.com

Building an electronic guitar digital sound synthesizer using a programmable SoC

Digital sound synthesis has always been a subject of interest to me as a hobbyist. Recently, my interest was further piqued by a wiki page on the Karplus-Strong Guitar synthesis. I was intrigued by the simplicity of the synthesizer design. Here's the diagram from the Wiki page (Figure 1 ):

Figure 1: K-S block diagram from the Karplus-Strong Guitar synthesis Wiki

Also mentioned was the pitch of the note was decided by the delay L by a simple relationship,

Equation 1

where the filter coefficients determine the ‘stringiness’ of the note.

A noise burst? That’s it? That can’t be right! No way it’s that simple!

Like all hobbyists, I began by jumping right in to the implementation. The code took only a couple of hours to put together and I hooked up the DAC output to an oscilloscope. I could tell it looked vaguely sinusoidal with harmonics put together but had no clue what it would sound like (obviously).

A quick search later, I hooked up a pair of old head phones. I wasn’t expecting much, a lot of us know, the first time most projects either don’t do anything or if you’re really down on your luck, emit a tiny puff of white smoke(I prefer to think of it as the spirit of the chip passing on to the great beyond). I was really surprised when it actually did sound like a guitar pluck.

Something was up. Math ahead – you have been warned.

Let’s take a closer look at the diagram in Figure 2 below:

Figure 2: K-S block diagram – Annotations

The output of the synthesizer is a simple summation of the low pass filter output and the noise burst. In all the equations below a subscript ‘n’ indicates the current sample.

The Filter is a simple low pass filter of the form,

We’ll get back to what the exact coefficients a and b are a little later.

So essentially we get,

But since the input of the filter is nothing but the output delayed by L samples,

Rewriting the output of the filter as a difference of the output and input and moving things around a bit we get,

Changing this to time domain is achieved simply by substituting

where Ts is the sampling time.

After a little more effort we end up with a quite a large expression,

Thankfully, it’s pretty easy to find the magnitude response by sweeping the frequency ω with Excel.
The sampling rate is set to 44.1ksps (being the least amount which can sweep the whole range of hearing).

From Equation 1 , to generate a 200Hz tone the delay ‘L’ will be 44100/200 = 220.5

Since our delays are digital, A close value of 220 is chosen. I arbitrarily chose the pole of the low pass filter value of 10KHz to start off with. This pole frequency decides the value of ‘a’ and ‘b’ we mentioned earlier.

Figure 3: Frequency Response

From the response shown in Figure 3 ,, we can clearly see that if we pass a wide-band noise signal (or as I prefer to look at it: a signal whose energy is spread across all frequencies), the output you would see is a fundamental of 200Hz followed by the 2x, 3x, 4x…. harmonics. This is almost exactly how an actual guitar behaves when plucked; The fundamental is followed by the associated overtones as shown in Figure 4 .

Figure 4: Guitar Overtones

Another interesting point to note is that as you push the filter pole tolower frequency, the higher order harmonics are snubbed and the notebecomes ‘cleaner’ but begins to sound quite unlike a guitar, almost likea drum with the note dying out much quicker.

Figure 5: Frequency response with different cutoffs

Also interesting to note is the slight shift in note frequency when moving filter cutoffs (Figure 5 ).

Theshift isn’t very significant around the fundamental but higher orderharmonics begin to shift more due to the phase loss associated with theLow pass filter.

Below in Figure 6 you can see a quicksnapshot of the practical digital sampling oscilloscope (DSO) waveformobtained when playing the note E, around 156 Hz.

Figure 6: Waveform capture of Note ‘E’ – Fundamental of 156 Hz


Figure 7: FFT of the note ‘E’ – Fundamental of 156Hz

Thefrequency peaks are distributed as expected, the magnitude roll offisn’t quite as clean as the theoretical calculations but the overallpicture matches up quite well.

Implementing the digital guitar with a PSoC
Theimplementation was done entirely inside the PSoC5LP chip, CypressSemiconductors flagship model of very powerful mixed signal SoCs. Tomake things more interesting, I added some user interface into thesystem by introducing a few touch buttons which pluck different noteswhen touched. Figure 8 is a system level block diagram of the basic building blocks.

Figure 8: System level block diagram

Armed with a trusty CY8CKIT-050 developer kit , an expansion board for the touch buttons, and some very nice speakers I ‘borrowed’, the entire setup looks like that shown in Figure 9 and Figure 10 :

Figure 9: Guitar Synthesizer Setup

Figure 10: A closer look

Concluding thoughts:
The Karplus-Strong synthesizer is probably one of the simplest synthesizer algorithm to code but theoutput sounds surprisingly good even with a 8-bit DAC (which is strictlyunderpowered for audio reproduction).

The only drawback is therather large amounts of buffer we need when playing low frequency notes.For example, playing a 100Hz signal will require a buffer of 441 whensampling at 44.1Ksps. This can be a problem if we want to add multiplesimultaneous strings and code size is a concern.

Adding theflexibility of PSoC also opens up a lot of new options such as using anI2S interface to output the signal to a standard audio DAC. Moving thesynthesis code to the Digital Filter Block(DFB) to free up the main coreto add any other calculation intensive process.

Sree Harsha Angara works as an Applications Engineer at Cypress Semiconductor Corporation where he is involved in a wide variety of Embedded system designs using the PSoC platform. He holds a Masters degree in Electrical Engineering from the Birla Institute of Technology and Science, Pilani and his research interests include Control Theory, Embedded applications in Power as well as digital filter theory.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.