Sunday, 20 November 2011

Ring Modulation

Right, I'm still about 3 weeks behind where I'm currently at, but now things are getting interesting.  This post should be interesting because:
a) I've started to delve into signal processing
b) This resulted in some completely wacky sounds

The ring modulator is called what it is because when it was originally invented, and was done physically using circuits and components (much like a lot of the digital signal stuff we'll look at), one of its distinguishing features is a ring of diodes, illustrated below.  A modulator basically means we're using one signal to directly alter - or modulate - another signal.

An analogue ring modulator - the black arrows are called diodes and only allow current to flow in one direction  

Another reason to be very excited is in this project I not only streamed audio output, but audio input.  Also, in my implementation I allow the user to select the audio API themselves from a numbered list, allowing me to test them all.  It also means I can choose if I stream audio input from a microphone or the line-in.  I'm blessed to have a really nice electric piano, and it ended up being the perfect tool for testing my program's audio.

Now into the theory, which I'll keep as straightforward as possible.  If you're musically inclined there is the chance you've heard of tremolo.  And I'm not referring to the thing in jazz piano.  Tremolo is an effect where amplitude (loudness) is controlled by a low frequency oscillator (LFO, basically a very low frequency sine / triangle wave around the 0.1-2Hz region) that causes steady, repeating, fluctuation in loudness.  Check below in my soundcloud thing for this post if you don't have a clue what I'm going on about.  Although I didn't initially realise this, ring modulation is very closely linked.

Say you have a tremolo at 1Hz, you can identify each individual loud and quiet.  You could tap your finger to the pulse, unless you've got a really bad sense of rhythm.  Set the tremolo to 5Hz, and now the sound is a bit like a machine gun.  But you're still able to tell the audio's loud then quiet, just very quickly.  Now crank it up to 400Hz.  We no longer have a tremolo effect.  We have a freaky effect, and this is known as... ring modulation.  I've demonstrated this with the clip below, using an organ sound because it sustains well and I felt like it (the clip goes from 1Hz to 5Hz to 10Hz to 50Hz to 400Hz)
For those that need a mathematical approach, in order to accept the seemingly dark magic that is ring modulation, here is a demonstration.  First we assume that we're dealing with the simplest of all signals, a raw cosine wave.  Now we reassess what's happening - we've got two cosine waves, one being the carrier signal and one being the modulating signal.  We're multiplying two cosines together.  To find it algebraically, we can look to the product identities.

Maths gets everywhere
 This seems to explain the funky sounds.  We're getting frequencies that are the sum and difference of the two, which for a 440Hz tone modulated by a 300Hz cosine signal, would result in frequencies of 140Hz and 740Hz - both of which aren't notes in the Western scale.  When you put in complex sounds, like the sound of a piano, that have lots and lots of complex harmonics (remember, they're still fundamentally built out of sinusoids), all of these different harmonics are getting changed according to the formula above, and it turns the sound really inharmonic and wacky.

Also, the visual effect you get from it is pretty cool, if you look at the actual waves being produced.  The program I'm using to do this is awesomely free Audacity:

The organ sound isn't great for demonstrating this effect
For such a simple program, the audio produced is pretty interesting, and it got me trying different sounds from different instruments.  Below I've recorded my electric piano through the ring modulator via a line-in connection - there's before and afters so you can listen and compare...

Piano Ring Modulation by Boyley

I'd like to thank Wikipedia for a couple of the images which I've borrowed (the ring modulator circuit diagram and the formula).

No comments:

Post a Comment