Basics of Audio DSP in C for Rapsberry Pi Zero

6 years 10 months ago - 6 years 10 months ago #853 by Ray
This short document aims to introduce you into Digital Signal Processing for audio, so you will learn how to code audio effects for guitar pedals.
This subject is huge so I will focus into standard C programming under Linux (Raspberry Pi) to be used with the Pedal Pi hardware, although most of the concepts could be ported to other platforms.

Basics of Audio DSP in C for Pedal Pi:
Digital Signal Processing for audio sounds like a complex topic but in fact it could be reduced to 3 simple tasks:
  1. Read the guitar analog input signal.
  2. Play with the signal in the digital domain.
  3. Generate the analog output signal.

note: The time between reading (1) and generating (3) the signal is called latency, the shortest latency the better (the system reacts faster).

1. Reading the Analog Signal:
The electric guitar signal is usually a 0.2Vpp amplitude signal, the first op-amp stage will filter the signal to avoid aliasing and also give amplification to reach 3.3Vpp so the ADC will use all its span for better S/N ratio.

The analog signal is feed into an ADC ( MCP3202 ) that will generate a 12bit resolution signal (0 to 4095 values) for the Pi ZERO to represent a signal that goes from 0 to 3.3V, check the image below. The communication between the Pi Zero and the ADC is done using SPI interface (4 lines).

note: With a 12bit ADC, the maximum guitar signal level is 4095, the minimum level is 0 and in absence of signal the values float around 4095/2=2047*.
* Ideally because due to offsets and non linearities this values can slightly deviate from the ideals, not a big issue anyway.

Sampling Rate.
It is the number of samples of audio read per second, the highest the better (more resolution). Following the Nyquist Theorem the sampling rate should be at least double than the maximum frequency we want to sample. The audio band is 20Hz to 20KHz so at least 2x20KHz=40KHz should be used as sampling rate.

The ideal maximum sampling rate limit is defined by the ADC, with the MCP3202 we have 50KHz (working at 3.3V). In practice Pi Zero acts like a master in the SPI bus and decides when to read the ADC (slave), so the sample rate is defined by the Pi Zero and its around 40KHz.


2. Play with the Signal (aka DSP)
In guitar pedal effects there are two main effects groups:
  1. Effects based on signal Amplitude variations: All volume expression, boosters, distortions (overdrive, distortion, fuzz, bit-crushing, etc). They basically take the original guitar signal and play with its amplitude, inverting it, clipping it, reducing it, amplifying it, etc... They are very easy to code and give a lot of fun playing with different variations or ideas.
  2. Effects based on time: In this group we have delays, echoes, reverbs, phasers, octavers, etc..The idea is to store the guitar signal is a buffer to play it again later (creating a delay for example) or multiple times (echo).

For any of the effects mentioned above, they require you to understand the nature of the effect, if you want to code a distortion, delay or flanger first of all you need to go to wikipedia (or any guitar pedal forum and understand how this particular sound effect works.

This subject can be expanded for many pages, so to summarize it we are going to present 2 examples:
  1. Example of guitar pedal based on amplitude variations: Distortion Effect.
  2. Example of guitar pedal based on time variations: Delay Effect

3. Generate the analog output signal.
The last step after processing the signal in the digital domain is to generate again an analog waveform. There are different ways to do this but maybe the simplest is to use a PWM signal to generate the different analog signal levels:
  • If you want to understand how to configure the PWM audio in Pi ZERO check this topic.
Attachments:

Please Log in to join the conversation.

Time to create page: 0.060 seconds
Powered by Kunena Forum
Joomla SEF URLs by Artio