Binary Code and Storing Music on Computers

Sian Langham

Why do we study Maths? A question I’m sure most of us have asked ourselves at some point – maybe it was when studying long division in school or when trying to help your child with their maths homework for the umpteenth time… What’s the purpose of it all? Well, let’s start simple. We need to use arithmetic when adding up the cost of our groceries in a supermarket, or percentages when working out the sale price of a discounted shirt. Delving a little deeper you will in fact discover that maths can be found in the most unexpected places, and in this series of articles I plan to explore some of these in detail to give you the perfect answer next time someone asks “why do we study maths?”.

First up, let’s talk binary code and its importance in storing music on computers…

The counting system we use in every life is called the decimal system, or ‘base ten’ as each digit position represents a power of ten: 1, 10, 100 etc. For a more detailed example 432 is 4 x 102 + 3 x 101 + 2 x 100. Whilst we choose to generally count in base ten, we can in fact count in any base we like. Counting in base two is known as the Binary system. 

When we count in base ten, each digit can take one of ten values 0-9, and so following the same pattern when we count in binary each digit can take one of two values which are 0 or 1. Again, for a more detailed example the number 13 in binary form would be written as 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 1101. The table below shows a conversion table from decimal to binary for the numbers 0 to 9.

DecimalBinaryPowers of 2
000000 x 23 + 0 x 22 + 0 x 21 + 0 x 20
100010 x 23 + 0 x 22 + 0 x 21 + 1 x 20
200100 x 23 + 0 x 22 + 1 x 21 + 0 x 20
300110 x 23 + 0 x 22 + 1 x 21 + 1 x 20
401000 x 23 + 1 x 22 + 0 x 21 + 0 x 20
501010 x 23 + 1 x 22 + 0 x 21 + 1 x 20
601100 x 23 + 1 x 22 + 1 x 21 + 0 x 20
701110 x 23 + 1 x 22 + 1 x 21 + 1 x 20
810001 x 23 + 0 x 22 + 0 x 21 + 0 x 20
910011 x 23 + 0 x 22 + 0 x 21 + 1 x 20

Each 0 or 1 digit in a binary number is called a bit, derived from Binary digIT and eight bits make up a byte. A single byte is able to represent the decimal numbers 0-255 because 00000000 is the decimal number zero, and 11111111 = 27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, which means any combination of eight 1’s and 0’s will represent a number between 255 and 0. For instance, 10101010 = 128 + 32 + 8 + 2 = 170. 

Although typically the number 1101 would represent the decimal number 13 as stated above, we can instead choose to assign each binary number to a letter, symbol or action. The figure below shows ASCII (American Standard Code for Information Interchange) which is primarily used for communication between a computer and a keyboard or printer. The binary numbers 32-126 are assigned a character and 0-31 and 127 are reserved for control codes such as backspace and delete. Pretty neat, but this is just the beginning of the use of binary in computer systems…

One thing in particular that fascinates me is the ability of a computer to store and play music. Sound is a continuous function that constantly changes with time (represented by a continuous sound wave), but as a computer only has a finite amount of space, we need a way of converting this continuous function into a finite list of numbers that can be stored. This is where binary code comes into play. A device called an ADC (Analogue to Digital Converter) samples the sound wave at specific time intervals and at each instant assigns the amplitude of the sound wave a binary value. This list of binary numbers can then be stored on the computer and a DAC (Digital to Analogue Converter) can convert it back to a wave when needed. The drawings below show a basic demonstration of a sound wave being sampled and reconstructed in binary form. 

Now that we have a method of storing the sound waves in a finite amount of space, the question becomes how do we ensure the sound is reproduced correctly? This relies on ensuring the sound wave does not become distorted and can be achieved in two main ways. First, we need to use the right sample rate. This means we must sample the sound enough times to ensure the same wave is replicated by the DAC, and to do this we need to follow the “Nyquist Sampling Theorem”. In essence, this says a continuous function can be perfectly replicated if its sample rate is at least twice as fast as the highest frequency component of the wave. 

Let’s consider an example of the hopefully familiar sine wave shown below. If we sample the wave at ten times the frequency of the wave and connect the data points, the sampled signal will not look much different to the original wave because the data points are so close together. However, as we decrease the sampling frequency, the discrete time waveform is no longer as good of a representation of the original sine wave, but we can still clearly see the cyclic nature and the frequency of the wave. As we decrease further to twice the wave frequency, the sampled signal looks like a triangle wave as shown in the first image below, but it is still clear the frequency of the sine wave it represents. Anything below this sampling frequency and the cyclic nature of the wave is lost, as demonstrated in the second image below.

If we consider a piece of music which has a highest frequency of 10,000 Hz then according to the Nyquist Sampling Theorem it must be sampled at 20,000 Hz (or 20,000 times a second) to prevent aliasing from occurring. Aliasing is where a signal is disguised as a different frequency signal because it is not sampled often enough, resulting in sample points which align with another wave, as seen in the examples below. Anti-aliasing filters can also be placed in the circuit to stop this from happening, but it can easily be avoided by sampling more frequently. The highest frequency sound that humans can hear is roughly 20,000 Hz so as long as we sample a wave 40,000 times a second or more there should be no issues with aliasing. Nevertheless, this is just a benchmark – the more more often we sample, the better the audio quality. 

The second consideration is how many bits we chose to use to store the sound. For example, if we use 8 bits, the number 11111111 = 255 and so there are 256 binary values that the amplitude can be assigned to (including zero). This is like splitting the wave into 256 levels and assigning a binary number at each time interval based on which level the amplitude of the wave is closest to. The images below show waves being sampled at different bit depths.

Limiting the number of possible values for the amplitude can of course cause some big rounding errors which consequently distort the sound. Therefore, the more bits we use, the more levels there will be and the smaller the rounding error. However, the more bits we use, and the more frequently we sample the signal, the more space it takes up on the computer’s storage space. We need a happy medium. A standard CD uses 16 bits and a sample frequency of 44.1 kHz because the aim is to minimise storage space so that the music can fit onto the disc, whilst still being sampled often enough to avoid aliasing. With the more recent rise in smart phones and their increased storage capabilities, many people now favour better quality audio over storage space and so it is common to see devices use 24 bits and a 96 kHz sample frequency. Some high resolution files even use a sampling frequency of 192 kHz. 

Most electronic devices such as phones, laptops and tablets use binary code to perform all sorts of functions, not just storing music. Taking a photo, sending a text, scrolling through social media, all of this relies on binary code – and therefore mathematics. So, our first answer the question “why do we do maths?” is to develop technology to make our lives easier and more enjoyable. Check out the second article in the series on how maths can be used in golf – coming soon!

3 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s