## Learn Computer Programming and Audio Engineering

Welcome to Hack Audio. Here you will find informational content related to audio and computer programming, as well as a community of coders/engineers interested in music.

New information will be posted on a weekly basis for a range of experience levels – from beginner to professional.

If you feel like you don’t have a lot of experience with programming, that’s great! This blog is meant to help you go from zero to hacker hero.

Content is organized by specific topics in the side menu or chronologically (by most recent) below.

All code examples are available for download to go along with the explanation of each topic.

This site is a work-in-progress. I have planned out a road map of initial topics. Therefore, I will be filling in content over the next few months.

Finally, I always appreciate connecting with other engineers. Feel free to contact me with feedback. I will try my best to respond as soon as possible.

My name is Eric Tarr. I am an audio enthusiast, musician, computer programmer, electrical engineer, hearing scientist, and educator. I hope you consider yourself one (or more) of these things, too.

I started this blog to deliver content about coding and signal processing for audio applications. I hope you will join me as part of a community of people interested in these topics.

Have you ever wanted to:

• Learn how to program a computer to process or synthesize audio?
• Create your own mobile app, DAW effect plug-in, or virtual instrument?
• Apply the computer programming skills you already have towards your passion for music?

If so, you’ve found the right place! I hope to help you with everything you need to get started and help you have a fun time in the process.

Additionally, if you are looking for resources on music production/recording/mixing, check out some my tutorials on YouTube and at theproaudiofiles.com.

Thanks for stopping by!

I created the Hack Audio blog to focus on demonstrating skills and techniques associated with computer programming and audio.

Additionally, I started a podcast to share stories, interests, passions, anecdotes, philosophies, interviews, and everything else about the same topic.

Subscribe to the Hack Audio Podcast to stay up on the latest developments in our community of audio programmers. Take it with you wherever you go.

Subscribe to the podcast on iTunes here.

Represent the community with some Hack Audio gear. Click the images below to visit the online store.

There are all kinds of items: from clothing to coffee mugs. We all know coders love (need) their java.

Do you want to stay up-to-date on the latest content from Hack Audio? Join the mailing list to receive weekly email notifications.

# Echo Effects

Echo effects are one type of audio effect based on delaying a signal over time. In this case, listeners perceive an audible repetition of a signal after some duration of time.

Listeners perceive distinct echoes when the time delay is relatively long (greater than ~30 milliseconds). When a time delay is short, listeners do not perceive echoes. Instead, a single “fused” sound is perceived. This makes it possible to create other types of effects like: chorus, flanger, phaser, and spectral filters.

This important time delay duration of ~30 milliseconds is called the echo threshold. It represents the minimum time delay for a listener to perceive a distinct repetition…

# I/O Characteristics

One way to visualize the processing of an audio effect is to compare the amplitude of the output signal versus the amplitude of the input signal. This relationship is plotted on a two-dimensional axes, with the input amplitude on the horizontal (x-axis) and the output amplitude on the vertical (y-axis).

This is a different way to visualize the amplitude of a signal than the waveform.

For this plot, the idea is to display how the entire range of input amplitude values (e.g. from -1 to 1) get mapped to output amplitude values. For instance, if the input amplitude is 0.5, the plot will show…

# Soft Clipping

Soft clipping is a type of distortion effect where the amplitude of a signal is saturated along a smooth curve, rather than the abrupt shape of hard-clipping. Soft clipping is similar to the type of distortion found in certain analog audio systems. Examples include a vacuum tube amplified and the saturation of magnetic tape.

There are many non-linear functions that can be used to digitally create a soft-clipping distortion effect. One example of soft-clipping distortion is based on using a cubic function. An equation to create…

# Infinite Clipping

Infinite clipping is a type of distortion effect where the amplitude of a signal is processed to only allow a maximum and minimum amplitude value. All positive amplitude values are processed to equal the maximum value. All negative amplitude values are processed to equal the minimum value.

Infinite clipping can be implemented in computer code using a conditional statement. If the signal amplitude is greater than zero, then the output amplitude is set to 1. Otherwise (else), the output amplitude is…

# Full-wave Rectification

Rectification is a type of distortion effect where the negative amplitude values of a signal are distorted in different ways. For full-wave rectification, the negative amplitude values are changed to the identical positive amplitude values.

There are several ways full-wave rectification can be created in computer code. First, the absolute value function from mathematics can be used to process the individual samples of a signal. Second, a conditional statement can be used to invert the polarity of the negative samples in a signal without processing…

# Functions

A very useful aspect of many programming languages, including Matlab, is the ability to create functions. (Note: the term, function, is used here as a computer programming concept. This is not the same thing as a function in mathematics.)

Already, we have looked at how to use built-in functions including: sqrt, abs, log10, audioread, audiowrite,
plot
, etc. These functions help us as programmers accomplish complicated tasks using a single command.

Additionally, we can create our own user-generated functions. In this case, we are writing the code to perform the task of a function. Then, we can execute the entire process by using a single command.

A user-generated function is created by putting the keyword, function, at the start of an m-file. This gives a special designation to the m-file and makes it possible…

# Conditional Statements

Another type of control structure is called a conditional statement. In this case, the typical flow of executed commands is interrupted such that certain commands are only executed under certain conditions. If those conditions are not met, then those commands are not executed.

Conditional statements make it possible for our computer programs to make decisions. This makes our computer more flexible, responsive, and intuitive to us as programmers. We make decisions using conditional statements regularly in our lives.

As a simple example…

# Stereo Image Widening

Stereo image widening is the process of changing the perceived panoramic width of an audio signal. It can be used to make a signal sound wider, as well as narrower.

This process is based on mid-side processing. As part of the process of encoding and decoding, the relative amplitude of the “mid” and “side” channels are adjusted.

To make a signal sound wider, the amplitude of the “side” channel is increased compared to the “mid” channel. To make a signal sound narrower, the amplitude of the “mid”channel” is increased…

# Mid-side Encoding

There are two necessary steps to perform mid-side processing. First, the two-channel, left-right stereo signal must be encoded as a two-channel, mid-side stereo signal. Then the process is reversed by decoding a conventional two-channel, left-right stereo signal.

Mid-side processing is based on an interesting consequence of stereo panning. For signals panned to the center of the stereo field, the amplitude of the signal is identical in the left channel and the right channel. Therefore, if the right channel is subtracted from the left channel, any signals panned to the center will cancel out. However, any signals panned to either of the sides will be left over.

Furthermore, the right channel can be added to the left channel. This will result in a new signal called the “mids.” This is because…

# Mid-side Processing

Mid-side (MS) processing is a special method of processing stereo signals. The purpose of MS processing is to decompose a stereo signal into different parts.

On its own, MS processing does not actually change the signal. However, it is meant to be used with other processing like equalization or compression.

When using MS processing, a conventional stereo signal with a left channel and right channel is separated into a mid channel and a sides channel. This way it is possible…

# Linear Panning

The panning potentiometer (pan pot) is a variable control allowing an audio engineer to place a signal at different locations across the stereo field. With a stereo pair of loudspeakers, the pan pot changes the signal from the left side over to the right side.

The basic function of a pan pot is to change the amplitude of a signal in the left channel and also change the amplitude in the right channel. When the pan pot is turned to the left, the amplitude of the signal in the left channel is increased while the amplitude in the right channel is decreased. When the knob is turned to the center, the amplitude is identical in both channels.

There are several mathematical panning functions which can be used to determine the level of the amplitude should be for each channel based on the position of a (virtual) panning potentiometer. The first, and most intuitive, function to consider is the linear panning function.

Here are the steps required to calculate the linear panning functions. First, assuming the value…

# Mono to Stereo

Every electric signal created by a single microphone capturing acoustic vibrations starts as a mono audio signal.

When that signal is routed through the channel of an audio console or of a digital audio workstation, the mono signal is converted to a stereo signal with two channels.

When working with computer code in Matlab, a stereo signal is represented as an array with two columns. The first column is for the “left” channel and the second column is for the “right” channel.

The process of converting a mono signal to a stereo signal…

# Axes Labels

There are several parts of a plot within a figure window which can be labeled by the programmer. These parts are locations relative to the two-dimensional axes.

The horizontal and vertical axes can be labeled by using the commands: xlabel(‘string’) and ylabel(‘string’), respectively.

The title of the axes can be labeled using the command: title(‘string’).

Within the axes, a legend can be included to label each line…

# Tremolo

Tremolo is the audio effect created from using amplitude modulation. Examples of the effect can be found in many styles of music from different eras in time. Tremolo creates the perception of a pulsating signal.

It is an effect used most commonly with electric guitar. There are many guitar amplifiers with the effect included. Additionally, there are many versions of the effect in pedal form.

There are two main controls of the effect. First, the “rate” or “speed” controls how often the amplitude pulses occur. This is controlled by modifying the frequency of the LFO modulator. Second, the “depth” or “intensity” controls the strength…

Amplitude fades are a method to smooth out the transitions of amplitude changes. Audio engineers regularly use fades at the beginning and end of a sound file. A “fade in” gradually increases the amplitude of the signal from 0 to 1 (unity gain). A “fade out” gradually decreases the gain of a signal from 1 (unity gain) to 0.

The process of adding a fade to a signal involves an element-wise multiplication to scale the amplitude over time. In Matlab, we can create an array for the fade and multiply it by signal to create a smooth amplitude change. By indexing a portion of the signal, we can apply the fade to the appropriate part at the beginning or end.

# Subtracting Signals

Another method signals can be combined is by subtracting the amplitude of one signal from another signal. When working with signals (or arrays in Matlab) this is accomplished using element-wise subtraction.

Audio engineers use this operation to perform a “null test”. This test is used to determine if two signals are identical to each other. If the signals are identical, then by subtracting one from the other the result is a signal with an amplitude of zero.

# Combining Signals

It is common for audio engineers to work with many signals simultaneously. In live sound, there might be dozens of microphones used to capture the concert performance. Each signal is sent to engineer at the front-of-house to create the mix for the audience.

In the studio, an engineer works with the audio files from a multi-track recording to create a single stereo file. As part of the mix, the engineer might include additional signals from effects like reverb, echo, compression, etc.

Therefore, an important task for audio engineers to perform is the process of combining signals together…

# RMS Amplitude

An analysis used for the overall amplitude of a signal is called the root-mean-square (RMS) amplitude or level. Conceptually, it describes the average signal amplitude. However, it is different than simply measuring the arithmetic mean of a signal.

An audio signal can have both positive and negative amplitude values. If we took the arithmetic mean of a sine wave, the negative values would offset the positive values and the result would be zero. This approach is not informative about the average signal level.

This is where the RMS level can be useful. It is based on the magnitude…

# Peak Amplitude

One measurement of a signal’s amplitude is to determine the peak. This process involves analyzing and comparing each sample to find the one with the greatest amplitude.

Audio signals can have a positive and negative amplitude. Therefore, the amplitude peak is actually based on the sample with the largest deviation from zero, positive or negative. This is specifically called the signal’s peak magnitude.

Here is a two step process to determine the peak magnitude. First, take the absolute value of the signal. This will allow for a simple comparison between both positive and negative values. Second, go through the samples to find the maximum…

# DC Offset

If multiplying a number by a signal scales the amplitude, adding a number to a signal performs an offset. From a waveform plot, addition shifts the value of every sample up (or down) by the same amount.

The term, “DC Offset,” is commonly used by audio engineers to describe this operation. From the days of analog circuits, there were two types of current: direct current (DC) and alternating current (AC). When direct current was applied to  alternating current, the result was a signal shifted away from zero.

Generally speaking, when we play audio signals over loudspeakers, it is better for the signal to be centered around zero. Therefore, most of the time a DC Offset should be avoided. However, there are a few situations…

# Linear Gain Change

The underlying process to control the amplitude of a signal is a linear gain change. This process involves multiplying the value of each sample in a signal by a number relative to a linear scale.

If the value of each sample is multiplied by a number less than 1, then the amplitude of the signal will be reduced. If the number is greater than 1, then the amplitude will be increased. If the number is 1, then the amplitude is unchanged. Audio engineers use the term, unity gain, to refer to a process where the amplitude is unchanged.

When we write computer code to perform a linear gain change, it is necessary to process the individual samples of a signal. One way to do this is to use a loop to index the individual elements of an array. As we access each sample, we can multiply…

# Loops

In programming, a loop is a control structure used for repeating commands several times. Rather than having to type of the repeating commands each time, a programmer uses the loop to make writing the code more efficient and easier to read.

Computers excel at repeating commands very quickly. Therefore, the loop construct is very common in programming. We will use loops in several different ways when working with audio.

In Matlab, there are a couple different ways to create loops. The first way we will consider is called a “for loop.” As part of creating the loop, we need to specify in our script exactly which lines of code should be repeated, as well as how many times they should repeat.

Therefore, it is necessary to use keywords at the start and end of the loop. Additionally, a counting variable is created to keep track of how many times…

Comments allow a programmer to include text in their code that a computer will ignore, or not execute. Initially, it might seem unnecessary to write text other than the required executed commands. There are several beneficial reasons why Comments are a part of almost every programming language and why you should use them.

One reason to use Comments is to describe the code you have written. This way it easier for a reader to make sense of the Matlab syntax. It is common to add Comments for the details of a script like individual commands, as well as providing a high-level overview about an entire program.

Another reason to use Comments is to temporarily stop the computer from executing a command without having to entirely remove the text. This way it is possible to save a command for future use, and quickly include it again by removing the Comment.

The Matlab symbol for creating a Comment is…

# Current Folder

The Matlab Current Folder is the working directory for the development environment. The Current Folder can be changed to different directories on your computer.

When writing a program, Matlab will implicitly reference files contained in the Current Folder. If you are working with a sound file, Matlab will search for the file contained in the Current Folder. It is possible to reference files outside of the Current Folder, but the location of the file must be explicitly provided.

# Workspace

The Matlab Workspace is the space in the computer’s memory where information is stored while working in the development environment. Here, variables are stored when created and can also be deleted. Several characteristics of variables are displayed in the Workspace including the variable name, data type, and value.

As a programmer, it is important to be aware of which variables are in the Workspace at different places in your code. Referring to the Workspace can help during the process of trouble-shooting code errors…

# Impulse Train

An impulse train is a signal that contains an impulse in every cycle. The signal starts a new cycle every time there is a new spike. One sample per cycle has a non-zero amplitude. All other samples have an amplitude of zero.

This signal is periodic because it repeats the same cycle. The fundamental frequency of the signal is based on the period of time between each impulse. Spectrally, this signal has harmonics at both even and odd frequencies. All harmonics have…

# Triangle Wave

A triangle wave is a signal with amplitude increasing like a ramp for half a cycle and amplitude decreasing like a ramp for the other half a cycle. There is not a function in Matlab called triangle. However, a triangle wave can be synthesized by modifying the sawtooth function. The basic syntax for using the function is the following:

$[y] = sawtooth(2\cdot\pi\cdot f \cdot t,0.5)$

Input Variables:

f – frequency of the signal (scalar in Hz)

t – an array of time samples for a signal.

0.5 – for width where half the cycle is increasing amplitude and half the cycle is decreasing amplitude.

Output Variables:

y – an array containing a triangle-wave signal.

Further documentation…

# Square Wave

A square wave is a signal which oscillates between only two possible amplitude values. There is a function in Matlab for synthesizing a square wave which oscillates between $\pm 1$. The basic syntax for using the function is the following:

$[y] = square(2\cdot\pi\cdot f \cdot t,duty)$

Input Variables:

f – frequency of the signal (scalar in Hz)

t – an array of time samples for a signal.

duty – for duty cycle. The percentage of a cycle which is positive from [0,100].

Output Variables:

y – an array containing a square-wave signal.

Further documentation…

# Sine Function

Periodic signals complete repetitive cycles. Each point within a cycle can be described with cyclical units – either degrees or radians. A cycle in units of degrees starts at ${0}^{\circ}$ and finishes at ${360}^{\circ}$. Equivalently, a cycle in units of radians starts at $0$ and finishes at $2\pi$. Half a rotation is ${180}^{\circ}$ or ${\pi}$ radians. One quarter of a rotation is ${90}^{\circ}$ or $\frac{\pi}{2}$ radians, etc.

One way to describe and visualize a cycle is to consider a circle, with each point on the circle representing a corresponding point in a cycle. The units of degrees (or radians) are based on an angular rotation of the circle relative to the start of a cycle.

Let’s place this circle on the cartesian axes…

# Signal Synthesis

In audio, it is common to work with signals that were originally created by acoustic vibrations and captured by a microphone.

In other cases, it can be helpful to work with signals that were created as digital information. Any time a signal is created directly as digital information (or also as electricity), that signal can be described as a synthesized signal. In other words, synthesis is the process of creating a signal.

The nice thing about working with synthesized signals is we can create them to have desirable characteristics. This can be helpful when using “test signals” to analyze various audio effects (i.e. signal processing methods).

There are several common synthesized signals used in audio applications. This section demonstrates the synthesis…

# Signal Reverse

One method to process a digital signal is to rearrange the order of the samples contained in the signal. More specifically, to put the samples in the opposite, or backwards, order.

This creates a signal which has been reversed over time. The last sample becomes the first sample. The second-to-last sample becomes the second sample. This continues until the original first sample is placed as the last sample.

This effect produces some interesting results. One famous example can be heard in the guitar solos of Castles Made of Sand by Jimi Hendrix. This effect was originally created by feeding analog tape is the reverse order through a tape machine.

With digital signals, this effect can be created by indexing an array to extract the elements in a reverse order.

# Signal Splice

One of the most essential and basic edits is the signal splice. This involves splitting one signal into two or more parts. A similar task involves extracting a portion of a signal to create another signal.

Both of these edits are performed by indexing a subset of the elements of an array.

# AudioWrite

The samples from an array can be converted and saved as an audio file using the built-in function: audiowrite.

The basic syntax for using the function is the following:

$audiowrite(filename,y,Fs)$

Input Variables:

filename – a string containing the name of the file. Example: ‘testSignal.wav’

y – an array containing the samples of the audio signal.

Fs – a scalar of the sound file’s sampling rate.

Further documentation for the function is available here.

The samples from a digital audio file can be imported into Matlab by using a built-in function: audioread.

The basic syntax for using the function is the following:

$[y, Fs] = audioread(filename)$

Input Variable:

filename – a string containing the name of the file stored on the computer’s hard drive. Example: ‘testSignal.wav’

Output Variables:

y – an array containing the samples of the audio signal.

Fs – a scalar of the sound file’s sampling rate.

Further documentation for the function is available here.

# Plot Function

A simple way to visualize the data in an array is to use the built-in, “plot” function for Matlab. When this function is called, a new figure window will open and display a two-dimensional axes. The value of elements in the array are plotted on the vertical axis versus the element number along the horizontal axis.

The basic syntax for using the function is the following: $plot(y)$

Input Variable:

y – an array to values

# Audio Signals

Sound is vibration. It is created by the movement of a sound source. It travels through air (or another medium) by the movement of air molecules. It is captured with a receiver (a microphone or our ears) by the movement of a diaphragm.

Sound is a signal because there is information in these vibrations. When a person is speaking, vibrations carry the content of the speaker’s message. When a guitar is played, vibrations from the strings carry information about the melody and rhythm of the performance.

There are several important characteristics of these vibrations. The rate at which these vibrations occur is called a signal’s frequency. The strength at which these vibrations occur is called a signal’s amplitude. These concepts will be explored in much greater detail in subsequent posts.

Audio (as a term) is more general…

# f(x) – Functions

Mathematical functions describe the relationship between multiple things.

In our everyday lives, there are many examples of functions. The position on a map describes a location, north/south versus east/west. Similarly, coordinates are used to describe a particular longitude and latitude.

With math, variables are used to represent these individual things. This is written generally as: y = f(x).

The previous equation can be read, “the variable, y, is a function, f, of the variable, x.” In other words, “y is a function of x.”

Another way to describe functions…

# Creating Arrays

In programming, it is common for there to several methods to perform the same task. As a programmer, it is beneficial to consider the best option to perform a task.

The best option may be the choice which uses the least amount of code, makes the code run fastest, allows the code to be easy to read, or a combination of all these things.

The Matlab programming language has several different methods which can be used to create arrays.

Each method has advantages and disadvantages. As a programmer, you should consider each option in different situations.

One efficient method to create arrays is based on using the colon operator (“:”) and another method is based on using the built-in function, linspace.

# Arrays

Just as strings are a type of variable where multiple text characters can be organized and grouped together, arrays are a type of variable where multiple numbers can be organized and grouped together.

Each individual number in an array is called an element.

In Matlab, there are rules for how the elements must be organized. The elements must be organized in the shape of a rectangle.

The dimensions of the rectangular elements are described as rows and columns.

Some arrays have multiple rows and multiple columns. Another name for a multi-dimensional array is a matrix. One of the distinguishing factors…

# Text Strings

Many computer programming languages (including Matlab) interpret text information differently than numerical information.

As a programmer, there are situations where it is appropriate to use the number 2, and other situations where it is appropriate to use the text ‘two.’

Text can be an individual character, like the letters in the alphabet. Text can also be entire words or multiple words.

Regardless, when text is assigned to a variable, the data type of that variable is a string.

A string consists of text including: letters, symbols, and numbers. In this case, symbols and numbers are interpreted…

# Naming Conventions

When naming variables as a programmer, there are several rules which must be followed and several “best practices” which should be followed.

The Matlab programming language has several restrictions for how variables can be named. Variable names may be individual text characters or multiple text characters. Variable names may contain numbers, but may not begin with a number. Variable names may not contain spaces. Variable names may not contain symbols such as: #, \$, %, &, etc.

There are several recommendations worth using when naming variables. Use a variable name with a descriptive meaning whenever possible. Think about the purpose of the variable and for what it will be used when determining a name. This will make your code easier to read, especially if you are having to review it months or years later.

Many programmers across many programming languages use a convention of character capitalization called: camel case. Here, the first character in the variable name is lowercase…

# Variables

Computer programs are comprised of executed commands. When each command is executed the computer determines the result.

Typically it is helpful for the computer to remember the result by storing it in memory. This way the information can be used throughout the program.

It might be as simple as storing the answer to a basic math equation. Or it might be storing an audio file in memory for processing and mixing with other audio files.

To organize and keep track of the information, the result of an executed command can be assigned to a variable.

After a variable is created and assigned, it can be…

# Math Operators

If you have ever had the experience of working with a calculator, then you are already familiar with some of the most important parts of programming a computer.

When programming with audio, the mathematical operations of addition, subtraction, multiplication, and division are very important.

In the Matlab programming language, these operations can be executed by using the same symbols…

# Variable Types

Many programming languages have been designed so that computers can understand and store various types of information.

This is helpful because people also categorize information.

As an example, numbers and text are two distinct categories of information.

Sometimes it is advantageous for a person to use “3” instead of “three.” Other times the opposite is beneficial. The same is true for a computer.

Additionally, some programming languages have specific categories of information based on content…

# Install Help

Here are some resources to help with the installation of Matlab.

Mathworks provides instructions and documentation for installation, licensing, and activation.

Here is a YouTube tutorial for installing Matlab on a Windows operating system. A similar tutorial is available for the Mac OSX operating system…

# Programming Languages

When programming a computer, you as the programmer tell a computer what to do. In order to communicate with your computer, it helps to be using the same language.

A programming language is nothing more than a set of letters, numbers, and symbols that have meaning to a computer. A programmer can use the language to provide instructions that can be interpreted and implemented by a computer.

Computers can understand many programming languages. There are programming languages that were specifically created for music…

# Distortion Analysis

There are many types of distortion effects used in audio.  Each type of distortion produces different results when processing signals.

There are a couple different ways to analyze the differences between the distortion effects. One way is to visualize the output amplitude versus the input amplitude of the effect. In this case, a plot of the processing I/O Characteristics is used.

Another way is to compare the harmonics created by processing a sine wave with the effect. This can be done using a spectrum analyzer to display the amplitude of the different frequencies in the distorted…

# Bit Reduction

For digital signals, amplitude resolution (number of possible amplitude levels) is based on the number of bits used per sample. A bit is a place in memory for binary numbers (base 2) which can either be a 1 or a 0, just as a digit is a place which can hold a value from 0-9 for base-10 numbers.

Bit Reduction is a type of distortion effect where the number of possible amplitude values of a signal is reduced. When used as an audio effect, bit reduction can also be called bit crushing…

# Hard Clipping

Hard-clipping is a type of distortion effect where the amplitude of a signal is limited to a maximum amplitude. This effect can be created in the analog or hardware world when a transistor is pushed to a maximum amplitude. At this amplitude, the transistor saturates and cannot output a signal above a specific level, so the input signal is clipped.

This type of distortion effect can also be created using software. This is accomplished by detecting when the amplitude of a signal goes above a specified threshold. If the amplitude of the signal goes above the threshold, then the amplitude is assigned a new value (resulting in clipping).

Audio signals have…

# Half-wave Rectification

Rectification is a type of distortion effect where the negative amplitude values of a signal are distorted in different ways. For half-wave rectification, all negative amplitude values are changed to zero.

This type of distortion is related to, but different from, full-wave rectification.

One method to create half-wave rectification in our computer code is to use a conditional statement. Here, we set up one condition for when the signal is greater than zero and a different condition for when the signal is less than or equal to zero. When the signal is positive, the output signal should equal the input signal. When the signal is negative, the output signal should equal zero regardless…

# Distortion Effects

There are many audio effects created by multiplying and/or adding a scalar (single number) to each sample of a signal. Examples include: linear gain change, DC offset, polarity inversion, amplitude normalization, digital summing, amplitude fades, amplitude modulation, and stereo panning.

Effects based on this type of processing are categorized as linear effects. The other category of effects is called non-linear effects. Non-linear processing is based on any method other than scalar multiplication and addition.

Distortion audio effects process signals using a non-linear function. There are many audio effects that belong to the distortion category including: overdrive, fuzz, bit crushers, and aural exciters. Additionally, many audio technologies process signals in a non-linear manner such as guitar tube amplifiers, saturated analog tape, and transformers in an audio console. Distortion effects can also be created digitally…

# Logicals

Another data type used in computer programs is the logical. This data type is used in commands associated with computer logic; in other words, when our computer evaluates something to be true or false.

Therefore, the logical data type has only two possible values : 1 (true) and 0 (false). These values are used to represent the result of boolean expressions, comparing different variables, numbers, and strings in a program.

Logical operations perform a comparison to determine…

# Mid-side Decoding

The second step of mid-side processing is decoding. This involves the process of turning the encoded “mid” and “sides” signals back into a conventional two-channel, left-right stereo signal.

Typically when using MS processing, some other additional type of processing (equalization, compression, etc.) is applied separately to either the “mid” or “sides” signals. Then, it is the processed versions of the signals which get decoded.

The following equations can be used…

# Auto Pan

The auto-pan effect performs an automatic, repeating pattern with a virtual panning potentiometer. This creates the perception of a sound moving from side to side across the stereo field.

Typically, a low-frequency oscillator (LFO) signal is used to change the pan value. Specifically the amplitude of the LFO at a given sample sets the pan value. Different types of LFOs will create different repeating patterns. Common examples include a sine wave, triangle wave, and square wave.

Because the panning potentiometer is fundamentally an amplitude control, the auto-pan effect processes a signal by increasing the amplitude in one stereo channel and decreasing the amplitude in the other channel. As the effect continues, the LFO will…

# Sine-Law Panning

A third type of panning function is Sine-Law Panning. As the name suggests, this type of panning is based on the sine function.

Similar to linear and square-law panning, an amplitude value for the left channel and right channel of a stereo signal can be calculated based on the sine-law panning functions. Then a mono signal can be converted to a stereo signal by multiplying by the amplitude value…

# Square-Law Panning

In addition to the linear panning functions, there are other mathematical functions to calculate the amplitude of each stereo channel based on the position of a panning potentiometer. Let’s now consider the Square-Law panning functions.

The motivation for using the Square-Law functions is based on the perceived strength of the signal across different panning positions. By using the square-law functions, equal combined power between the channels is achieved. Whereas, the linear panning functions achieve equal combined amplitude. In many ways, listeners perceive the strength of the signal based on the power, rather than the amplitude. In other words, listeners will perceive the same signal strength regardless of panning position…

# Stereo Audio

The two-channel stereo format is the most common format for audio signals. This format is used for most recorded music. Additionally, most live-sound systems are set up to play back stereo sound.

A stereo signal is comprised of two separate mono signals: one signal for a “left” speaker and one signal for a “right” speaker.

The typical listener uses two ears when listening to music. Whether the listener uses a pair of headphones or a pair of loudspeakers, a stereo signal provides the capability of presenting different signals to each of the listener’s ears.

The benefit of stereo…

# Multiple Plots

In many situations, it can be desirable to plot and compare several different functions at the same time. There are a few different options available in Matlab to accomplish this task.

Multiple plots can be displayed in their own separate figure windows by using the command: figure; between each instance the plot function is called. More explicitly, the commands figure(1), figure(2), figure(3), etc. can be used to assign a particular use of the plot function to a particular figure window.

Multiple plots can be displayed in a single figure window by using several methods. First, simply adding additional input variables in the plot function will display multiple lines on the same axes. As an example…

# Line Specification

The resulting visualization from the built-in plot function can be customized in several ways. One way is by setting the look of the actual line being drawn. Customizing this part of the plot is called changing the Line Specification.

There are a few attributes of the line that can be set including the line color, style, and thickness. The following table describes many of the options available for setting the line color and style.

The line thickness can be set by using the input parameter Line Width and then specifying…

# Amplitude Modulation

When two signals are multiplied together, the amplitude of one signal modulates the amplitude of the other signal. In audio, it is common to use a low-frequency oscillator (LFO) as the modulator. The other input signal is commonly called the carrier signals.

The amplitude of the LFO periodically increases and decreases the amplitude of the carrier signal. The frequency of the LFO has an important impact on the process. Usually the frequency is selected to be less than 20 Hz.

Another way to change the LFO is to modify…

# Multiplying Signals

Another way to combine signals together is by using element-wise multiplication. This can be used to create amplitude fades and also amplitude modulation for the tremolo effect.

In Matlab, it necessary to use a period before the multiplication operator to specify the point-wise (or element-wise) multiplication. This is written: $y = x .* w$. For the signals, $x = \{ {x}_{1},{x}_{2},\hdots,{x}_{n} \}$ and $w = \{ {w}_{1},{w}_{2},\hdots,{w}_{n} \}$, the element-wise multiplication…

Digital signals can be “mixed” or “blended” by using digital summing. This process involves adding two or more signals together using element-wise indexing.

The first sample of one signal is added to the first sample of another signal. The result represents the first sample of the output signal. This process is repeated for the second sample, and all subsequent samples in the signal.

The end result is a single signal comprised of both original signals. This process is similar to the task an analog mixing console…

# RMS Normalization

Another way to normalize the amplitude of a signal is based on the RMS amplitude. In this case, we will multiply a scaling factor, $a$, by the sample values in our signal to change the amplitude such that the result has the desired RMS level, $R$.

If we know what the desired RMS level should be, it is possible to figure out the scaling factor to perform a linear gain change. This is done by rearranging the equation used to calculate the RMS level…

# Peak Normalization

Normalizing the amplitude of a signal is to change the amplitude to meet a particular criterion. One type of normalization is to change the amplitude such that the signal’s peak magnitude equals a specified level.

By convention in Matlab, the amplitude of an audio signal can span a range between -1 and +1. Therefore, the maximum magnitude (difference from 0) a signal can take is 1. This maximum value can be used as a reference level – called full scale (FS). As the reference level, it will have the decibel value:

$0 \mbox{ } dBFS = 20 \cdot {log}_{10}(\frac{1}{1})$

A signal’s peak magnitude can be normalized to the value of 1 FS (0 dBFS). This process will scale the amplitude of all samples in a signal such that the peak magnitude has a value of 1. An expression to create a normalized output signal, out, by scaling the input signal, in, is shown below:

$out = \frac{1}{max(abs(in))} \cdot in$

The amplitude could also be normalized to any level relative to 0 dBFS. As an example…

# Decibel (dB) Scale

One of the most common controls audio engineers use is the channel fader. It is used to increase or decrease the amplitude of a signal. The relative amount the amplitude is changed, and the units of the fader, are based on the decibel (dB) scale.

Previously, we looked at changing the amplitude of a signal based on a linear scale. From a signal processing standpoint, we will program our computer to change the amplitude of a signal by multiplying by a scaler number. When writing software for an audio engineer to use, it is necessary to know how to interpret a change in amplitude based on the dB scale. Therefore, it is necessary to work with the relationship between the linear scale and the dB scale.

An amplitude on the decibel scale, ${a}_{dB}$, can be determined from an amplitude on the linear scale, ${a}_{lin}$, using the relationship: ${a}_{dB} = 20 \cdot {log}_{10} (\frac{{a}_{lin}}{1})$.

In reverse, the decibel scale can be converted to the linear scale using the relationship: ${a}_{lin} = {10}^{\frac{{a}_{dB}}{20}}$.

A general rule of thumb audio engineers should know is, “doubling a signals amplitude is a $\sim6$ dB increase. Whereas, halving a signal’s amplitude…

# Polarity Inversion

One common and basic way a signal can be processed is to invert the polarity. Almost all audio mixing consoles have a button on individual channels to invert the polarity of the input signal. Many software plug-ins which perform different kinds of processing, have a button to also invert the signal’s polarity.

Polarity Inversion is a special case of the linear gain gain. In this case, the amplitude of the signal is multiplied by -1. Therefore, the positive values become negative and the negative values become positive.

# Amplitude

One of the most common ways an audio signal can be processed is by changing its amplitude.

Amplitude conceptually represents the strength of a signal. It is the force sound exerts on an environment. It is the level of sound pressure.

A signal’s amplitude is related to whether it is perceived as being loud or quiet. Therefore, this is an obvious characteristic of a signal to want to process.

You have likely had an experience using some sort of volume knob or volume slider. In essence, this control allows you to change…

# Control Structures

Control structures are statements in a computer program which alter the order of executed commands. Up to this point, we have written scripts where our computer starts on line 1 to execute commands from left to right, then moves to line 2, then line 3, and so on, in a sequential order. However, this doesn’t always have to be the case.

As a programmer, there are several reasons why it would be helpful to be able to control and change this order of execution. First, if we want our computer to perform the same task many times, then we should be able to tell it to repeat certain commands. This concept in programming is called a loop.

Second, if we want our computer to perform a task under certain conditions, then we should be able to tell it to test and respond to criteria. This concept in programming is called a conditional statement.

Third, if we want to organize and construct a single piece of code to perform a specific task…

# Documentation

Matlab includes extensive documentation resources about the programming language and the built-in functions. This resource can be a very helpful reference while you are programming.

The Matlab Documentation is available within the development environment under the ‘Help’ tab of the menu bar. Additionally, this same resource is available online from the Matlab website.

By accessing the documentation, it is unnecessary for a programmer to memorize the syntax for every function. Rather, an experienced programmer is familiar with quickly searching and understanding the available resources to determine how to write their code.

# m-file

A Matlab m-file is a script of commands to be executed together. This is the method to write and group commands which work with each other to create a computer program.

This file is saved to your computer’s hard drive, and can be loaded again in the future to perform the same commands. The file has an extension – “.m”, to indicate it is a Matlab script.

When an m-file is opened, it is located in the Matlab Editor tab. The script can be executed by pressing the “Run” (Green Arrow) button, or by typing the name of the script in the Command Window.

As a programmer, it is important to understand the order in which commands are executed in a script. Generally, the order of execution is from top-to-bottom and left-to-right.

The Matlab m-file is an essential concept to learn and will be the primary way we will be working as we explore…

# Matlab Environment

Now that we have looked at the basics of working with audio in the Matlab programming language, let’s explore more about the software development environment we are using for programming.

Learning a couple more things about Matlab will make it possible to do more fun things with audio.

Up to this point, I have been demonstrating individual commands that can be executed in our programming language.

However, computer programs are typically created by combining many commands together.

In Matlab, multiple commands can be grouped in a script…

# Sawtooth Wave

A sawtooth wave is a signal with amplitude that increases like a ramp in each cycle. Additionally, there are modified versions of the sawtooth wave in which the amplitude decreases like a ramp for part of a cycle.

There is a function in Matlab for synthesizing a sawtooth wave. The basic syntax for using the function is the following:

$[y] = sawtooth(2\cdot\pi\cdot f \cdot t,width)$

Input Variables:

f – frequency of the signal (scalar in Hz)

t – an array of time samples for a signal.

width – for the width of each cycle when the amplitude is increasing like a ramp. This is a scalar value from [0,1].

Output Variables:

y – an array containing a sawtooth-wave signal.

Further documentation…

# Sine Wave

The sine function can be used to create a signal with a single frequency called a sine wave. This signal is commonly used in audio as a test signal to analyze various processing effects.

There is a function in Matlab for synthesizing a sine wave. The basic syntax for using the function is the following:

$[y] = sin(2\cdot\pi\cdot f \cdot t)$

Input Variables:

f – frequency of the signal (scalar in Hz)

t – an array of time samples for a signal.

Output Variables:

y – an array containing a sine-wave signal.

Further documentation…

# Periodic Signals

Many of the sounds we hear are periodic. When these sounds occur, vibrations repeat the same motion over and over again. One repetition of the vibration is called a cycle. Each cycle occurs over a consistent length of time called a period. Conceptually, a period is the time it takes for a signal to complete one cycle. To represent a signal’s period, we will use the greek letter tau, $\tau$.

$\tau = \frac{time}{1 cycle}$

A related concept to period is a signal’s frequency, which is the number of cycles per second. Therefore, frequency is the inverse of period. To represent a signal’s frequency, we will use the letter, $f$. The unit of frequency is called Hertz $(Hz)$.

$f = \frac{cycles}{1 second} Hz = \frac{1}{\tau}$

# Consolidate Signals

Two or more signals can be daisy-chained together, one after the other, by using the process of consolidating signals. The concept is to place the samples of multiple signals in sequential order. This process could also include adding extra silence before or after a signal.

Signals can be consolidated together by using array concatenation.

This process is the digital method of recreating what audio engineers could do with analog tape to adhere two separate pieces of tape together. One famous (and audible) example of editing tape together is on the song, Good Vibrations by the Beach Boys. During the recording of the song, the verse and chorus were performed separately. Afterwards, the engineer put the two parts together by physically adhering the two tapes. If you listen closely, you can hear the splice between each section.

# AudioInfo

Information about a digital audio file can be recovered by using a built-in function: audioinfo.

The basic syntax for using the function is the following:

$info = audioinfo(filename)$

Input Variable:

filename – a string containing the name of the file. Example: ‘testSignal.wav’

Output Variables:

info – a structure containing the following pieces of information about the file.

info.Filename – a string of the sound file’s name.

info.CompressionMethod – a string describing the method of data compression.

info.NumChannels – a scalar describing whether the file is mono (1) or stereo (2).

info.TotalSamples – a scalar representing the number of samples per channel.

info.Duration – a scalar representing the length of the file in seconds.

info.BitsPerSample – a scalar representing the bit depth of the file.

Further documentation for the function is available here.

# Sound

The samples of an audio signal stored in an array can be played back over your computer’s speakers using the built-in function: sound.

The basic syntax for using the function is the following:

$sound(y,Fs)$

Input Variables:

y – an array containing the samples of the signal.

Further documentation for the function is available here.

# Signal Waveform

A signal’s waveform is a plot of its amplitude over time. This type of visualization is very common, used in almost every digital audio workstation (DAW).

In some situations, it is helpful to visualize the time units of samples. In other situations, the time units of seconds is preferred. In either case, the waveform looks identical except the scale of the horizontal axis is changed.

To control the scale of the horizontal axis, the following syntax can be used for the built-in function: $plot(x,y)$

Input Variables:

x – an array defining the time value of each sample of the signal (equal in length to the signal)

y – the array containing the signal’s sample values

# Digital Signals

Audio signals can be converted to digital information through a measurement process called sampling.

One common, and useful, digital representation of an audio signal records the amplitude at regular intervals of time. This representation is called pulse code modulation (PCM). Here are a few links to find out more about PCM: video, wikipedia.

The number of amplitude measurements, or samples, per second is called the sampling rate. For audio, common sampling rates include…

# Amplitude

One of the most common ways an audio signal can be processed is by changing its amplitude.

A signal’s amplitude is related to whether it is perceived as being loud or quiet. Therefore, this is an obvious characteristic of a signal to control.

You have likely had an experience using some sort of volume knob or volume slider. In essence, this control allows you to change the amplitude of a signal.

If you are working with digital audio, a signal’s amplitude…

# Signal Synthesis

In audio, it is common to work with signals that were originally created by acoustic vibrations and captured by a microphone.

In other cases, it can be helpful to work with signals that were created as digital information. Any time a signal is created directly as digital information (or also as electricity), that signal can be described as a synthesized signal. In other words, synthesis is the process of creating a signal.

The nice thing about working with synthesized signals is…

# Audio Basics

After understanding the basics of how a computer works with general information, it is possible to focus more specifically on audio information.

Even when working with audio, it can be helpful to keep in mind that computers process many types of information at a simple level. I mention this because I hope you do not feel intimidated about now taking the next step to working with audio. Audio is only slightly more complicated, but it is nothing you can’t handle if you are comfortable with numbers, strings, and arrays.

As you will see, digital audio signals are nothing more than numbers. It just so happens that there are a lot of things that we can do with audio when it is represented by numbers.

In this section, we will look at…

# Digital Signal Processing

Digital Signal Processing (DSP) is a set of methods and techniques that can be used to change the information in a signal. It has been an important part in advancing the field of audio engineering.

There are many different processing techniques that have been developed for digital signals. Some of these techniques accomplish similar tasks as analog circuits. Some techniques accomplish tasks that would be very difficult to implement with analog circuits.

This section presents several processing techniques that are commonly applied to audio…

# Indexing Arrays

The individual elements in arrays are organized in a rectangular configuration. Each element is placed into a particular row and column.

An individual element of an array can be referenced, or indexed, by specifying the row and column number.

A group of elements can be indexed by specifying all of their row and column numbers. There are several methods to do this, using a similar syntax for…

# Numbers

Computers are very proficient at performing mathematical calculations with numbers.

This is important and helpful when working with an audio signal, which is nothing more than a sequence of numbers.

The Matlab programming language treats numerical information differently than other types of information. It is necessary for a programmer to understand this, and anticipate how a computer deals with numbers.

When a single number is assigned to a variable, it is called a scalar. Eventually we will also…

# Math Functions

Besides the math operators that use symbols (+, -, *, /, ^), there are other things Matlab can execute related to mathematics.

As an example, if the ^ symbol is used to take a number to a power, then a related idea is to take the square root of a number. However, there is not a specific symbol for square root the Matlab programming language can interpret.

Instead, the programming language has a built-in function which can be used to perform a square root. Rather than a symbol, the syntax requires a function name and a function input.

For instance, sqrt(9) is the command which can be used to perform the square root operation on the number 9. In this case, the function name is: sqrt. The function input is: 9.

Besides square root, there are many other common functions built in…

# Matlab Environment

Now that we have looked at the basics of working with audio in the Matlab programming language, let’s explore more about the software environment we are using for programming.

Learning a couple more things about Matlab will make it possible to do more fun things with audio.

Up to this point, I have been demonstrating individual commands that can be executed in our programming language.

However, computer programs are typically created by combining…

# Error Statements

Every programming language has a set of rules, referred to as syntax, which must be follow for our computer to understand the instructions we create. Syntax is the proper method of putting together the letters, numbers, and symbols of a programming language to form meaningful statements or commands.

If we attempt to execute a command Matlab cannot interpret, then an error statement is created and displayed in the command window.

This statement is displayed in a red color and provides information about the problem with our command.

Error statements can be very helpful for fixing “bugs” in computer code. However, sometimes it can be confusing to figure out…

# Basic Computing

Computers can be extremely powerful machines, capable of sophisticated tasks and routines.

At a fundamental level, many of these complicated tasks are based on basic math: addition, subtraction, multiplication, and division.

The same is true for using a computer with audio. Basic math can be used to accomplish many of the ways audio signals…

# Matlab

Matlab is a desktop software application and programming language created by Mathworks.

I believe it is a great platform to learn computer programming, especially if you are also interested in processing audio signals.

After learning the basics of programming in Matlab, it is intuitive to learn other languages that can be used for other specific purposes.

Furthermore, Matlab is widely used. Many companies in the audio industry use Matlab internally. It is the ‘standard’ for proof-of-concept signal processing algorithm design. Matlab is also taught in many engineering schools. Therefore, if you want to learn computer programming with audio…

# Introduction

Computers are at the center of almost everything related to audio these days. Digital Audio Workstations (DAW) are commonly used for recording music. More and more aspects of live performance are incorporating computers. Audio effects plug-ins and virtual instruments are examples of computer software that can be used to process and synthesize sound. Apps for mobile devices increase the portability, accessibility, and convenience of music making.

There is similarity across all of these examples – computers were intentionally programmed…