Wednesday, July 18, 2012

Why Square Waves Matter

A while ago I wrote a post about what Square waves are, how they are formed, and how they are really just made up of a bunch of increasingly higher-pitched sign waves.

I'm sure that was more than enough graphs and lines to scare anyone into submission. That's not such a bad thing.

But one thing that I didn't get into is why square waves are important to the modern sound engineer.

Distortion - nothing's perfect
Whether you want it or not, Distortion is at the heart of every piece of reproduced music. It doesn't matter if that reproduction is "live" (as in a microphone running through a mixing console at a rock concert) or if it was recorded 15 years ago and played back on a CD player.

There are a number of different reasons for this, some of which (like the reaction of various electronic components like capacitors) will be left for later; there's just simply too much to cover.

What I'd like to cover is the linking between Square Waves and distortion, specifically in two areas: Gain and Quantisation.

Gain Management
If you've ever used a mixing console (or pretty much anything that shows you the audio levels in the standard green-orange-red format) you've probably kept on turning up the volume until you've got some pretty red lights blinking all over the place. If you ask any experienced engineer they'll tell you that this means that they are "saturating" the console's amplifiers, leading to "some nice compression" and/or distortion.

But what's actually happening here?

At the heart of every bit of audio equipment is a humble little circuit; the Operational Amplifier (Op-Amp). This simple circuit has a never-ending number of variations and uses, but essentially all we need to know is that it takes an input signal and some power, and outputs the same signal but with more volts.
Your stock-standard op-amp drawings. 
Okay, once again I can feel that this picture means nothing to you.  Never fear; let me explain.
 The top picture is how most of us think of amplifiers: you put a signal (+ and -)  into a circuit and you get some kind of output (shown here as the line out the right hand side of the Amp).

In fact, it's perfectly acceptable to only show the +ve side of the input.
Essentially what this image shows is what we mentioned above; you put a small signal into the amp, and you get a bigger signal out. What actually goes on inside the triangle is inconsequential.

However, the bottom picture gives us a little more information. Here we still see our inputs and outputs (U+, U- and U0). There's no difference there. But we now also see two new variables; Ucc and Uee. These represent the "power" that we are supplying to the amplifier.

In normal operation, the input signal is small enough so that the output (U0) is less than the total power that you are feeding to the op-amp (Ucc and Uee).

But if your input is too big, you can get to a situation where U0 needs to be bigger than Ucc. Since the op-amp has run out of power it stops amplifying and simply outputs the maximum power; i.e. Ucc.

Two examples of clipping; we're interested in the lower example


If you look at the image above, you'll see what I mean. The dashed "threshold" lines indicate Ucc and Uee; in other words, the maximum and minimum possible voltages of the output, U0. For the moment, we're only going to look at the lower example.

You'll notice that as soon as the output reaches the maximum (Ucc) or minimum (Uee), the waveform flattens out, no matter what the input signal looks like.

You'll also notice that this looks exactly like a square wave.

If you have a look at the previous article on Wave Theory, you'll note that a Square Wave contains the original frequency as well as a number of  harmonic frequencies. If we were to look at the above waveform on a spectrum analyser you would initially see a single peak (the input frequency). As the input increases beyond the Ucc threshold, you would start to see the single peak get shorter and more harmonic peaks growing up, as if out of nowhere.

As the clipped wave starts to look more like a square wave, you get more harmonics

These harmonics are the "distortion" that we are hearing. 

Since no amplifier circuit is perfect, you'll always have a bit of this Harmonic Distortion cropping up along the way. It's one of the measurements of amplifier power; Total Harmonic Distortion (or THD), and is expressed as a percentage. The percentage refers to the amount of power that is "lost" in the harmonic peaks. In short, a lower number is better - the number is telling you how much "false" information you're hearing due to the amplifier circuit.

Essentially, you can "fake" an amplifier's power rating by increasing the THD tolerance. An amplifier than can deliver 100W at 0.1% THD might be able to deliver 300W at 5% THD. That's because you're increasing the output of the amplifier, and also increasing the harmonics that are creeping into your original signal.

Is that a bad thing?
Distortion isn't necessarily a bad thing. Rock 'n' Rollers have been using fuzz pedals and distortion generators for decades. As with all sound-related things, everyone has their own opinion. But you also need to make sure that you're getting the right gear for the job. A 300W 10% THD amplifier might be perfect for your car stereo, but you might want a 300W 0.1% THD amplifier for your Hi-Fi speakers in your living room.

Quantisation Noise
The other, very similar form of distortion that I'd like to discuss here is Quantisation Noise.

As we saw in my PCM post, digital audio effectively segments up an analogue signal into a bunch of "stepped" signals.
A 4-bit quantisation (grey) of a sine wave (red)

Hopefully, it won't take you much imagination to realise that the grey, "digital" signal will have a number of harmonic frequencies tied up within its square-wave-like structure.
If not, then scroll back up and have a look at the last image showing the build-up of harmonics in the Square Wave example again!

Of course, just as we explained in the PCM article, the above representation is a gross simplification of a digital signal; even a 16-bit signal has millions of steps, making the harsh corners a little more manageable.

That being said, a digital recording will always have a certain amount of distortion, or "Quantisation Noise," inherent in the system. In low bit-rate recordings or transmissions (e.g. bad digital radio), this comes through as a watery-garbling of the audio signal.

Here is an example of an 8-bit recording, and then the noise generated as this is reduced in bit-depth to:
4 Bits (like the above image)


But does it matter?
I've just had a look at a couple of Analogue-to-digital converters in 16 bit terms. These list the THD of the entire converter (including the op-amps and the rest of the circuitry) at about -100dB. That equates to a THD of about 0.0009%. 

Whilst there is a lot of complaints about the "sound" of digital recordings (and yes, I do admit that there are differences between the two), the chances are that the THD of the amplifier is many, many times greater than the harmonic distortion that is caused by the "digital-ness" of the recording.


Monday, December 19, 2011

Sunday, November 20, 2011

Wave Theory - Part 1

Apologies for the delay here. I started writing a number of things into this post, however it became a long, boring, technical marathon. So I'm going to break up these types of articles and post some interstitial things as well. I originally wanted to look at Quantization Noise, but there were so many concepts involved that I decided to start with the "Basics".


Superposition

Science and nature detest corners. Curves, even constantly varying ones, are much easier to deal with. This is because curves add together nicely through a principle called "Superposition." Long story short, curves can be added together and taken apart without any difficulty. 

If you've ever looked at music on an oscilloscope, you'll see a random squiggle that never seems to stop changing. Whilst it can make for an interesting visual effect, it's not very helpful. However, due to Superposition, we can break that seemingly random signal down into the component inputs. This is usually called the Fourier Function Transform, but you might be more familiar with the term "Spectrum Analyser." (I strongly advise not looking too deeply into it unless you really like maths.)

If you run a signal though a Spectrum Analyser, you change the random squiggle into a series of columns. Each column represents a frequency range; the height of each column denotes the amount that each particular frequency range is contributing to the original signal.


Input signals (Left) and the resultant Spectrum Analysis (Right)
Thanks Wiki Commons
Let's have a look at some pictures; because they are easier to understand than words.
The top picture is your standard sine-wave. Since there is only one frequency, the Spectrum Analysis shows us that information; one tall column. All of the power in this signal is contained within that narrow frequency range.

The second set of images shows static; a totally random signal at low level. If we look at the Spectrum Analysis, you'll see that the power is spread randomly across all frequencies. We'll get into the origins of static one of these days. Just not today.

The last set of images shows these two signals superimposed onto each other. At each point, the "height" of the two signals is added together to make the bottom left image. It's a bit hard to see in the image, but you'll note that the curve is no longer smooth. However, this is nearly impossible to tell by looking at the input signal.
However, when we look at the Spectrum Analysis, we can clearly see that most of the energy is still in that main frequency range, but there is energy spread out across the other frequencies.

This effect is normally called "Noise".

Repeating Patterns
There is one other cool thing about superposition.

Basically, any repeating pattern can be built up with the right combination of sine waves. Take, for instance, a square wave:

Once again, Thanks Wiki!

If you have a look at the above image, you'll see three lines.
The Red line is a "true" square wave. The Green-dashed line shows a Fourier Approximation of the square wave using 5 component waves. The blue-dashed line uses 15 component waves. These waves are superimposed onto each other, like this:


The left-hand images shows our four component waves and their relative powers. Just by looking at the left-hand side, we can see that the "Fundamental" frequency, which is the same as the frequency of the square wave, has the most power.

The second column shows the superposition, but without adding the waves together. The third column shows the resultant, superimposed wave. As we go down the list, it starts looking more and more like a square wave.

The right-hand column, again, is what you'd see if you put the signal into a Spectrum analyser. There's a couple of points to note here:

  •  For a square wave of a fundamental frequency F, the frequency of the component waves (f) is as follows - f = (2n+1)F, where n starts at 0 and goes to infinity
  •  The power of each wave drops off significantly as n gets bigger (or, to put it another way, as the frequency of the component wave goes up, the power of that component goes down).


It turns out that once you get past n=16 or so, the power in the higher frequencies is so low that it no longer matters if you include them or not.



Next Time
It is very tempting to plough ahead here and talk about why I just wrecked your mind with superposition, but I won't.

Here's a hint though, it has to do with square waves and noise, and why digital and analogue aren't all that different.

As always, please feel free to post questions in the comments section!

Tuesday, November 1, 2011

Pulse-Code Modulation - Music to our ears

Okay, so we've just spent a lot of time looking at how data moves around a network. I hope that you were all with me for the ride. If not then please feel free to comment on any of the posts and I'll answer your post.

For the next couple of weeks I'd like to start looking at the data that you actually put into those packets.

One of the easiest places to start looking at digital signals is the humble Pulse-code Modulation, or PCM, method of encoding analogue information into a digital signal.

Analogue vs Digital
No, I'm not going to get into the "aesthetic" differences between analogue and digital, save to say that the only instrument that you can trust is your own ears. If it sounds better to you, then it sounds better to you.

I would like, however, to clarify something quickly. An "Analogue" signal is a proportionate signal with no real limitation. The local air pressure around a microphone or a signal can be measured by a device and turned into an electrical signal that is proportionate to the pressure. The higher the pressure, the higher the voltage. The voltage is an "Analogy" of the pressure.
This is what we refer to as an "Analogue" signal. Most simple electronic devices will process and run on analogue signals.
At some point, everything is an "Analogue" signal; the pressure changes that reach your ear drums or the light changes that reach your retinas are "Analogue".

A "Digital" signal, however, is somehow encoded so that it is no longer proportionate to the original signal. Through some kind of electronic process the Analogue signal is broken down into symbols of some kind that are readable only by devices that use that format. Before we can interact with them again then they need to be converted into an Analogue signal again. This process is called "Encoding" (Analogue to Digital) and "Decoding" (Digital to Analogue). Combine "enCOder" and "DECoder" and you get CODEC... but we'll get to those later.

Pulse-Code Modulation
Anyone who's ever Google'd "Digital Audio" will have seen a picture similar to this one:
Let's assume that we are looking at the Encode (Analogue to Digital) side of things (although the process is exactly the same in reverse).
The red line is our input signal; a standard sine-wave. This could be anything;  an audio signal, the number of people that like or dislike the current Prime Minister... it doesn't matter. We have a signal that is changing as time goes on.

The analogue signal is continuous and unbroken.

Pulse-Code Modulation sets a value (shown above as 0-15) for each equivalent amplitude. To convert the signal into a digital one, we record the value of the analogue signal at the start of each of the time divisions shown along the bottom access. This process is called "Sampling" - you are taking a sample of the Analogue signal at each of the time divisions.

You'll note that on the image above you can see a difference between the continuous Analogue (Red) signal and the Digital (Grey) one. It looks like a lot, right? In fact, the small differences in images like the above are one of the main arguments used by Analogue supporters. However, there is something missing from this picture...

Bit Rate
The picture above gives you a pretty good look at what you'd see in a phone-line; a 4-bit system. A bit is a single binary "symbol". One bit gives two states; two bits gives four, three bits give eight and four bits give sixteen states.
The number of "bits" that a digital signal contains is referred to as the Bit Depth. In basic terms, the higher the bit depth, the better the quality.

Even your most basic audio (CD-quality) has a bit depth of 16 bits; or about 65 thousand different states. The Human ear isn't really able to detect that kind of resolution; it would be like trying to look at the millimetre markings on a ruler that was 10 meters away.

Still, there are higher bit depths; standard digital audio (AES/EBU-3, which I will cover in a future article) runs at 20 bits (1,048,576 states) or at 24 bits (16,777,216 states). At this point, you're pretty much splitting hairs with a 2000-pound bomb...

But there is another factor that affects the quality of the sound; the rate at which the audio is sampled.
As a general rule, you want to take a sample at twice the frequency of the highest frequency you want to hear. The label on a new-born baby reads 20Hz-20,000Hz, although by the time you've used an MP3 player and gone to a concert or two you will be lucky if you can hear above about 17,000Hz.
Thus, the main sampling rate used in digital audio is 44.1kHz (CD-quality). "Professionals" will use 48kHz, or even go as high as 96kHz. Once again; at that level you are recording detail that humans just can't perceive. It's like taking a photo in ultraviolet; it might look brilliant, but there is no way for us to see the result.





I would like to continue this article, but in the interest of keeping things concise I will hold off for the time being. Next article I will look at a couple of the strange effects of PCM, and how we avoid them.
But for now, I must away. Until next time.

Tuesday, October 25, 2011

1.5 Mile Wireless connection

Just saw this pop up on Engadget. Wireless repeaters that go for 1.5 miles (2.4km).

I'm thinking large stadia or live sites where you'd need to move a bunch of signals around but it would be impractical to run Cat5 across the ground, or events like the Sydney New Year's Eve fireworks where there are barges spread out across the harbour...

Press-release and first thoughts:
http://www.engadget.com/2011/10/24/amped-wireless-gives-your-wifi-1-5-mile-range-never-lose-signal/

Company Signal:
http://www.ampedwireless.com/

Sunday, October 16, 2011

Layers of the Internet - Part 4

If you are new to this area, or are looking for the first three parts of this series, please use the following links:
Layer 1 - Link Layer
Layer 2- Internet Layer
Layer 3- Transport Layer

To recap quickly, the Link layer is the physical connection between devices (be it wireless, electrical or optical).
The Internet Layer is the layer that deals with the virtual separation of the entire Layer 1 network into smaller, virtual networks, and also allows traffic to be routed from one side of the planet to another.
The Transport Layer deals with two main things; where the data goes inside the devices that receive it, and also how the packets are passed around the networks (e.g. error correction protocols - re-sending a packet that never arrived at the destination.


Layer 4 - The Application Layer
For a second, let's think about what we are trying to achieve with a data network. We are trying to move information (be it computer data, an image, live audio... anything) from one point to another. If we look at what we've done in layers 1-3, we can see that we've done nothing but move the data from one end of the earth to the other.

Layers! Layer 4 is like the river and the clouds above; they rely on the lower layers of the earth, but they don't really care about them

.
But before any data can be delivered, we have to actually work out what data we are going to transport, why we around going to transport it and then what we are going to do with it at the other end. Data is useless unless we actually do something with it. For example, you could send a friend a photo, but if they can't view it then there was no point to transferring the data in the first place.

This is where the application layer comes in. It is the layer of the Internet that interfaces with the Users, but it is also the layer where we actually change the information that we are sending.

But Layer 4 isn't all about the Users, there's a lot of background processes involved. Let's have a quick look at a simple internet action; checking this blog:


  • Step 1: You open your browser and type "http://arts-comms.blogspot.com". This is the start of the process; you enter a small amount of data; 30 characters. It's not much when you think about it.
  • Step 2: Your browser tries to find the address of this page. If you were going to send a letter to a friend you couldn't just write their name on an envelope, post it, and hope for the best. The "Human-readable" Universal Resource Locator (URL) is similar to you friend's name; you can use it to describe them and look them up, but it isn't exactly an address. So, before your browser starts downloading this page, it looks out for a Domain Name Server (DNS) that will translate the URL into a TCP/IP Address.This address combines the Layer 2 and 3 addresses for the webpage. In this case the IP part of the address is 74.125.71.132, and the port is 80. This could be written 74.125.71.132:80.
  • Step 3: Your browser now requests a session with the blogspot.com webserver. Now that your browser knows where to look, it will send a request to 74.125.71.132:80 to ask if it can start talking to the Server. The server will normally check that there are no bans on your device, or any other reasons that it wouldn't want to talk to you.
  • Step 4: The Server opens a port and allows a session to begin. Should everything in Step 3 check out, the server will allocate a port number (this can be random, or fixed; it depends on the server) and then sends the information to your browser, letting it know that it's all good to go.
  • Step 5: Your Browser requests the Webpage. Now that the Server and your browser are talking, the browser finally asks for this page, sending along with the request any additional information that might be required (e.g. your login details, or which exact page you're requesting).
  • Step 6: The Server retrieves the webpage from its hard drive, and then transmits it as a series of packets. If you were to send a entire webpage as a single packet, it would be huge, and that would slow it down through the Internet. So the Server splits up the webpage into a number of small packets, adds a bit of information to let your browser know the order of the packets, and then transmits them through the network.
  • Step 7: Your browser assembles the webpage, checking for errors as it goes. There is always a chance that some packets will get lost in transit. However, since the server has added information for your browser, it will know if anything is missing. If something doesn't make it then your browser will re-request the missing pieces.
  • Step 8: Close the Session. Once everything has been checked, and you're happily reading away, the browser sends a message to the Server to finish off the session that was opened in Step 4. Once this is closed the connection disappears from the internet.


So, from your 31 keystrokes (including the "enter" key at the end) your browser and the Server have been communicating at Layer 4. You'll notice that I barely mentioned the Layer 1-3 protocols; and that is because  at Layer 4, just like all of the layers below it, the lower levels are transparent. It doesn't matter that I'm on a wireless connection and the server is on a optical fibre connection; they are connected up to Layer 3, and that's all that matters.

I'll be doing little write-ups on the various protocols as I continue to write this blog, so please stay tuned to the Glossary. Once I write an article on each protocol I will link it from the Glossary.

Monday, October 3, 2011

Layers of the Internet - Part 3

Before I start I would like to make a minor classification.
The "Layers" to which I am referring are the Internet Protocol layers, as specified in RFC 1122. I feel this model is more appropriate to the topics that I cover in this blog.


For those of you that wish to delve further into Networking as a subject, please refer to the Open Systems Interconnection (OSI) model. This model is the one that you would study in a networking degree, however it is slightly too specific for the purposes of this blog.




Layer 3 - The Transport Layer
In the previous two parts of this section of the blog, we looked at Layer 1 - The Link Layer and Layer 2 - The Internet Layer. In those layers, we say that each device was physically connected to all others, but with a bit of technology, we could divide up that huge network into smaller little sections.

I'll admit, it's difficult to visualise the Transport Layer. Here's some model trains.


As we have now connected on Layers 1 and 2, we can assume that our two devices are talking to each other. At this point it doesn't matter how that happens, or how many switches the packets have gone through in order to get from A to B. The tunnel through the Internet has been made, and for all intents and purposes, it isn't broken until the connection is shut off.

Layer 3 tells us two things; how a packet will move through a network, and where it will go when it gets there. In order to understand this we will examine two of the main Layer 3 Protocols, Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). And yes, if you noticed that the "TCP" looks very similar to the "TCP" in "TCP/IP", you'd be correct. Since TDP is the most common Layer 3 Protocol, and IP is the most common Layer 2 protocol, people refer to this model as the TCP/IP suite of protocols. However, due to the complexity of the protocol, I will start off with the UDP.

User Datagram Protocol
Take a moment to think about how many different network-enabled programs you are running now. On my machine I have two browsers with a few tabs in each, Skype, two email clients, a couple of automatic software updaters.... the list goes on. Now think about a lonely packet headed my way. It has the MAC address (from Layer 1) so it knows where my computer is. It has my IP address (from Layer 2) so it knows how to find me on the network. But once that packet finds its way into my wireless adaptor, where does it go? Is it a part of a webpage, or a message from a friend via Skype, or an email?

Both the UDP and TCP use a number, called a "Port," to direct a packet to the program that it needs to arrive at. Your network adaptor reads the Port number and forwards the data to the appropriate program for processing.

A UDP packet contains a bit of information that identifies it as a UDP packet and a Port number. That's about it. The advantage of this is that you have a smaller packet, which means faster transmission times. We'll look into this a bit later.

Transmission Control Protocol
Whilst a UDP packet basically only has the Port number, a TCP packet contains a lot of additional information. TCP packets can contain information relating to tracking and delivery protocols. For example, a TDP packet could identify that it was the 15th packet in a sequence, and that the receiving device should send notification of receipt.

This information can be used to make sure that packets that have been lost due to network errors can be re-requested and re-transmitted. Packets that take too long can killed off. Devices can send packets to each other to check the speed and path of the transmission.

All of this extra information makes the packets bigger, and hence slightly slower. However, in any non-real-time application this extra transmission time doesn't make much difference. Since very few transmissions actually require real-time transmission, the more Robust TCP is more common than UDP.

The main place we see UDP packets in the Arts industry is in audio/video transmissions, like Dante or AVB.  UDP also requires less programming, and thus it is more common in smaller applications, so you may come across UDP systems in some control software.

"Layer 3 Switches"
Normally a switch will read the Layer 2 information (the IP Address) in order to find out where to send the packet. As we saw in the Layer 2 article, this is used to prevent every device receiving every packet on the network. If a switch only does this, then it is known as a Layer 2 switch.

However, some switches can be programmed to read the Layer 3 information. Switches can read the TCP information to adjust flow control (e.g. if the switch is over capacity, it can request the sending device to slow down the transmission rates). This can also allow for the ability for a switch to send more important packets before standard packets (e.g. Telephone or real-time audio is transmitted before a webpage).