Wednesday, 15 May 2013

Algorithms of the street - Part 2: Amplitude Modulation

So, I've already shown you Frequency Modulation (FM) in pure data, it's an algorithm, or recipe for Pure Data, meaning you can use it in other contexts, change some parameters to vary your sound and if you're really lucky, reading Algorithms of the Street - Part 1: Frequency Modulation will have told you exactly what FM is.

Given that you might well have read that post by now (if not, I would recommend you do) you'll have noticed that there's some similarity between the title of that post and this one. The fact that there's only one word and one number different, perhaps?

As the name suggests, Amplitude Modulation is a lot like Frequency Modulation. Only instead of modulating (changing, in a regular way) the pitch, or frequency of our tone, it modulates the volume (amplitude).

If you've read the FM tutorial, some of this will sound quite familiar. If you're feeling confident you could always skip to the end of this one and see if you can see the difference in the patch.

Step 1: No Modulation Yet



Once again, I've used a version of the generative patch from Pure Data and BACON (Part 2) as a starting point. Clicking the toggle at the top there will start a random stream of numbers, converting to frequencies and creating a random, continuous melody. But the tone being used is fairly dull, a simple sine-wave. It's hard to listen to for a long time, and we can take a lot more control of this tone.

Step 2: Modulate the amplitude



First, notice that I added a frequency within the oscillator box, and cut the wire connecting it to our random melody generator. Secondly you'll notice that I've added another audio multitiply (*~) and a second oscillator, which is only working at one hertz, a single cycle every second.

Your ears will be telling you, at this stage, that the sound is fading in and out with a regular pattern, on the same note. This is the simplest form of AM, with an oscillator going very slowly (a Low Frequency Oscillator or LFO) and no extra control.

So what's going on here? Let's look at some 'known facts' I've mentioned in earlier blog posts:

  • Digital audio is a high-speed stream of numbers which are converted to control signals for a speaker or speakers
  • These numbers range from 1 to -1
  • An oscillator in Pd outputs this same range, 1 to -1
  • Because they're numbers, they follow the same multiplication rules as numbers:
    • If you multiply it by 1, it is unchanged
    • If you multiply it by 0, the result is 0
  • Sine waves (which are also the output of osc~), plotted on a graph, are this shape.

We're multiplying a tone (called the carrier) by a much slower tone, which is changing it in some way, and so is called the modulator. So the volume starts at 0, silent, increases, arcs through it's maximum value, then back down below zero? 

Once again, we can rely on the multiplication rules from the world of maths here. If you multiply a number by -1, the result is the same, but on the other side of zero. So what we're actually doing here is turning our sine wave upside down, then producing the same volume arc as before. Although we can't tell the difference because it's shape is unchanged, we just hear the a repeated volume arc.

So effectively, the amplitude modulation we're hearing is more this shape:


Also, because we're hearing both the up and down parts of the modulating sine wave as an increase in volume, we're hearing this pattern twice a second, instead of once. 

Step 3: Bringing it up to speed

Phew, that was easy.

Okay, so we've increased the frequency of our modulator to half that of the tone itself. We're hearing a thicker, more interesting sound. But why?

Well, as I discussed in Pure Data and BACON (Part 1), the human ear can only recognise around 30 separate sounds in each second. When sounds are more regular than that (have a higher frequency), we hear a continuous sound instead of separate events. So our modulation is now part of a complex tone, instead of making the volume lurch, you can't hear the process as it happens, only that the tone sounds different. 

So... With this algorithm in place, we can just add our melody generator back in, right?

Step 4: The Fly in the Ointment


It seems such a simple idea, we add melody to a tone generator, and we get a melody produced in that tone. 

Our ears, however, tell us that it's not at all like a clear melody. Just like this step in my Frequency Modulation tutorial, we've exposed a weakness in our modulation algorithm. The frequency of the carrier changes but the modulation frequency (literally, the frequency of our modulating oscillator) doesn't.

Unlike FM, however, there's an additional problem. Once again, the world of maths tells us that multiplication can be changed around and the result doesn't change (2 x 4 is exactly the same as 4 x 2, and so forth). Because of this, it's not at all clear in our algorithm which is the carrier and which is the modulator, they act on each other in exactly the same way.

So the rather unpleasant sound we're hearing now is two tones competing with each other to become the note you're actually hearing, and they clash. 

There is a solution, however...

Step 5: Control the depth and use a proportion


Okay, the first thing you'll probably have noticed here does nothing to the sound. I've added a couple of comments (ctrl + 5), to tell you which oscillator is the modulator, and which is the carrier. These literally do nothing at all except say something on-screen. Don't believe me? Right-click on one and check it's help patch.

More importantly, I've added another multiply below the modulator, which means it will only change it's volume from 0.5 to -0.5, having half the effect (which we call the modulation depth). Also (as a side-effect) reducing the volume of our sound by half. 

The most important thing here, however, is the multiply above the modulator. This means that whatever frequency of the carrier, the modulator will be at half of that. They are changing frequency in synchrony, so you can hear what is effectively one tone, changing it's pitch, as opposed to two warring frequencies. 

This will produce something like an organ tone, this specific ratio reminds me of the music from the classic game, columns. Happy memories.

We can vary the tone it'self by changing the modulation frequency and depth, just as we did in the FM tutorial. However, before we start playing around, there's a problem we must take care of...

The modulation depth typically just controls how much effect the modulator is having, but here it's also vastly changing the volume of the resulting tone. The best way to do this is by adding an offset to keep the modulating tone from crossing zero.

Step 6: An offset to solve the depth issue


Again, the most noticeable thing here is not what I'm trying to show you, I've changed out the numbers in the multiplies above and below the modulator. These are set to range from 0 to 1 and control the modulation frequency and depth, respectively.

Below the lower of those, however, I'v done something unusual. Two pieces of fixed arithmetic on our modulator frequency. So:

  • Adding 1 (with +~) changes the range from 0 to 2
  • Multiplying by a half changes it to 0 to 1
This means that while changing the depth will affect your volume, a depth of 0 does not mean a volume of 0. It means a volume of 0.5, and a depth of 1 means a changing volume that centres around 0.5. This also means that we no longer have the arcing waveform of earlier. The graph of our modulation volume now looks like this:

Notice how it's all above that centre point (zero). 

Step 7: Two things to control? Use a grid


The first thing you'll notice here, is that I've added a grid (to create your own make an object (ctrl + 1) and type the word 'grid' into it), I've also set the properties like so (to get to the properties dialogue, right click and select properties).

I made it big with the Height and Width fields, then set the range of both axes to 0 to 1 with a small step size. 

I've then taken the output of the grid to two sends, mod_frequency and mod_depth, and replaced the two sliders with the corresponding receives. 

Hey presto, we're generating a melody and changing the sound of an AM tone generator with a dot on a grid. 






Secret extra step: shhh, don't tell

Okay, so there's tutorials for both AM and FM synthesis here on the blog and I've stressed that these are re-usable algorithms, and once you understand them you can swap them out for oscillators in other places. The inquisitive mind is now probably thinking "does that mean I can make a synthesizer with both frequency modulation and amplitude modulation?" 

Of course you can, if you've read both posts you'll know all the parts of this, so I'll just whip up an AM/FM synth patch...


Well, there you have it, I've assumed an FM modulation depth of 0.5 and an AM depth of 1 (you don't need any objects to multiply by 1, I removed that one entirely). 

You can probably work out what each part of this is doing, so you can now have a play with the grid, as in both individual cases, when the frequency is low, you get a thick, heavy kind of sound, and when it's high it's richer, more bell-like. But this effect is compounded by two modulation frequencies. 

Near the bottom left of the grid, when both are low the sound can be heavy, and you may even hear a regular rhythm in the modulation in the bottom left square. To the top right it's more rich and complex than you will have heard so far in this series of tutorials. 

It's starting to get more complex now, and I promise I will eventually get around to blogging about hiding bits of Pure Data patch away so you can concentrate elsewhere, but for now, everything's on show. 



Well, that's it for now. I hope this all made a reasonable amount of sense and enlightened you a little about Amplitude Modulation. 

Did you find this tutorial helpful? Get stuck anywhere? Don't know what on earth I'm blathering on about? Feel free to comment here or get in touch with me direct at @MarmiteJunction

 Thanks for reading, God bless.

No comments:

Post a Comment