Tuesday, February 23, 2010

Sampling, Quantization & Encoding

Since we have discussed on what digital signals are, the next question that obviously comes up is how do we generate a digital signal? Digital signals can be generated from analog signals by the means of 3 basic steps:
  • Sampling
  • Quantization
  • Encoding
We will take a look at each of these steps one by one.

What should be the first step in getting a digital signal? You need to discretize the analog signal in time. This process is called the process of sampling. For the purpose of sampling, the analog signal values are measured at discrete intervals of time, say at an interval of 1 second each. Physically this can be done with the help of a simple circuit that uses a switch. The switch is on for a very small amount of time at an interval of one second. Thus, the analog signal passes through the switch during this small amount of time only, when the switch is on. Hence we approximately get the amplitude of the analog signal at these discrete periods of time.

Theoretically, this is represented as multiplying the analog signal by a train of impulses. An impulse is a signal that has some value only during the present time instant, ie, at time t = 0, but is zero elsewhere.A train of impulses are impulse signals occurring periodically over time. The train of impulses can thus be represented by the following diagram.


Thus, on applying the following input to the sampling circuit, the output is obtained as:




Once a signal is sampled, it is called a discrete signal. The next step towards digitization is Quantization. In this step, we do what is left for a signal to be called as digital, that is, amplitude discretization. We divide the amplitude into discrete levels. Usually the number of levels is a power of 2. This is done for easier binary representation. This will be further illustrated when we look into encoding. The resulting signal looks like this:

 
 Here, discretization of amplitude has been done with unit amplitude as the step.

The resulting signal is a digital signal. However, the digital systems we generally use, have binary representations only, that is, they can understand only two states, zero or one, or on or off. This is due to the fact that they are implemented using switches, namely, transistors, which can be either switched on or off. For this reason, the number of levels we have divided the amplitude into, have to be encoded in binary. This process is known as Encoding. Here we find the use of taking 2^n levels in amplitude. Any radix 2 number (having number of values equal to 2^n),  can be encoded into binary using n-bits, where each bit represents either zero or one. Any value within this 2^n can then be represented by an n-bit number. This can be demonstrated by a simple example.

Let us consider that we divide the amplitude into 8 equal levels. Now we know,
8  = 2^3
Thus, according to the previous argument, the number of bits required to represent any of these levels is 3. This is shown as follows:
Level 0 = 000 binary
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
6 = 110
7 = 111
Thus, we get 8 levels, from level 0 to level 7, which can be represented by 3-bit binary numbers. It is to be noted that the levels are numbered from 0 to 7, thus making a total of 8 levels.

For the digital signal we have generated, the encoded signal is as follows. We need 4-bits for encoding here, as 2^4 = 16:


The process of encoding provides us with the required digital signal, which can be transmitted over any transmission medium and can also be used by digital systems for different purposes.

We will try to explore the world of digital further in the up-coming posts. And as we move on, we'll try to focus on the practical aspect of designing a digital circuit. But before that, I must keep my promise. Here is the solution to the question that I asked: 
Why do we need a usb programmer rather than serial programmer on laptops?
Well, the answer is simply because laptops do not have a serial port for communication. Hence we are obliged to use a usb port and hence a usb programmer.

Friday, February 19, 2010

An Introduction to Digital Electronics

Ever wondered what the term digital really means? Literally, digital means 'relating to digits or fingers'. In other words, something that is countable. The signals we come across in nature are continuous in nature. By continuous I mean that they vary continuously in both time and amplitude. For example, take the case of your voice signal. As you speak, it produces a continuous variation, maybe in its amplitude or frequency with respect to time. Such signals are called analog signals. The following is an example of a typical analog signal.


Do you think this signal is countable? Yes, maybe in terms of peak amplitude or frequency, but will you be able to measure accurately the amplitude of the signal at any given time? Even the variation in time is infinitesimally small, which is not possible to measure or count. As a result, we get infinite number of  amplitude levels possible and infinite number of time durations. Thus, the signal is not countable.

So, how can we make this signal countable? We do this by measuring the signal at specific intervals in time, say after 1 second duration. This leads to discretization in time. What about the amplitude? That can also be discritized or divided into levels. For example, any amplitude between 0 and 0.9 Volts may be treated as 0 Volts, considering the signal is a voltage signal. The resulting signal may be as follows:






Here, we have discretized in time taking each sample after 3 seconds. The first diagram shows the signal discretized in amplitude, each amplitude level differing by one unit. The second diagram shows a discrete signal, that is the signal discretized in time. These two together result in a digital signal, as shown in the third figure, since at every point, its amplitude and time can be measured with 100% accuracy.

So, now that we have got a more clear idea of what a digital signal is, let us see what digital electronics deals with. Well, obviously digital signals. Digital systems are systems that can work on digital signals and transform them for various purposes according to our needs. However, I said a little while ago that most of the signals we come across in nature are analog signals. Then why do we need digital signals? It is due to the fact, that digital systems such as our computers can deal only with the digital signals. Digital electronics grew in demand due to the fact that the digital systems can be implemented very easily, using only switches. In the present day market, switches are implemented using transistors, thus making a system able to incorporate a huge number of switches as well as increasing the speed of operation of systems manifold. Digital systems ensure better security and less noise on transmission. Due to all these factors, digital electronics has become a very important and fast developing branch of electronics.

Having said so, we will next concentrate on how an analog signal may be converted into a digital signal in my next post.

As for the question I asked you in my last post,
"Why do we need a usb programmer rather than serial programmer on laptops?"
think a little more. I'll give you the answer on my next post. Till then, happy electronizing!

Thursday, February 18, 2010

Starting with Microcontrollers!

My very first day with ATmega16.. Well, I mean including hardware. Last time, when I programmed the 'SNAKE', I just did the programming part, but had no idea about the hardware and how to actually burn the code into it. It almost feels as if a new era is starting for me, the era of controllers. It feels wonderful!

Started with the 'Hello World' for microcontrollers, a small program in which we blink an LED at a frequency of 1KHz. It's really very simple, just switch on and off a port. When the LED started blinking, I felt on top of the world.

I'm using Atmel's ATmega16 and USBasp as programmer. I am using WinAVR. Needed lots of help, which I got from my ready resource, Merin. There are so many things you need to know in order to program a microcontroller! First you need to edit the 'makefile', which I couldn't understand how! Really, sometimes I can be so dumb! After I got that (thanks to Merin) things appeared simpler. You just need to select 'Enable Editing of Makefile' and save it in the same directory as your C-program. Oh, and don't forget to set the name of your program in 'Target file name'. You can edit the same from 'Makefile'->'Main File Name...' Next from Programmers' Notepad select 'Tools' -> 'make all' and 'program'. The program's programmed into the microcontroller and up and running in no time.

Here's the code for this 'Hello World' program. I am using port b, pin 0 as the output port:

#include <avr/io.h>
#include <util/delay.h>

void main()
{
    DDRB = 0xFF;        //set port B as o/p port
    PORTB = 0x00;       //initialize LED as off
   
    while(1)                 //loop indefinitely
    {
        PORTB |= 0x01;       //set LED on
        _delay_ms(500);      //delay in mseconds
        PORTB &= 0x00;       //set LED off
        _delay_ms(500);
    }
}

Ok, here's a question for you guys (only for those new to microcontrollers) - why do we need a usb programmer rather than serial programmer on laptops? Answer on next post, so keep a look out!