Monday, May 9, 2011

Energy Consume calculation from trace file

The simulation of S-MAC protocol is incomplete unless we analyze its energy consumption. We are interested on Analysis of Average energy consumption and energy consumption in each sensor node. I am going to present here a perl script that calculates the energy consumption of each node and average energy consumption.

# ============================================
# Average energy consume for new wireless revised trace format
# ============================================
# take input
$infile=$ARGV[0];
# open data file
open (DATA,"<$infile") || die "Can't open $infile $!";
# some looping condition
while (<DATA>) {
    @x = split(' ');
    # checking if column is r,s,d or f and column 1 is -t
    if (($x[1] eq '-t')&&(($x[0] eq 's')||($x[0] eq 'r')||($x[0] eq 'd')||($x[0] eq 'f'))){
        # column 8 is for node id. column 16 is node energy
        if ($x[8] eq '0'){
            $enerygy_0=$x[16];
        }
        if ($x[8] eq '1'){
            $enerygy_1=$x[16];
        }
        if ($x[8] eq '2'){
            $enerygy_2=$x[16];
        }
        if ($x[8] eq '3'){
            $enerygy_3=$x[16];
        }
        if ($x[8] eq '4'){
            $enerygy_4=$x[16];
        }
        if ($x[8] eq '5'){
            $enerygy_5=$x[16];
        }
        if ($x[8] eq '6'){
            $enerygy_6=$x[16];
        }
        if ($x[8] eq '7'){
            $enerygy_7=$x[16];
        }
        if ($x[8] eq '8'){
            $enerygy_8=$x[16];
        }
        if ($x[8] eq '9'){
            $enerygy_9=$x[16];
        }  
    }
}
# Total initial energy in this case is 500. So we subtract from 500 to calculate energy remain
# Define it as you defined in your tcl script '$ns_ node-config -initialEnergy 500 \' 
# print Energy consumed by each node.
print "Energy consume of 0 node: \t\t",(500-$enerygy_0),"\tJoule\n";
print "Energy consume of 1 node: \t\t",(500-$enerygy_1),"\tJoule\n";
print "Energy consume of 2 node: \t\t",(500-$enerygy_2),"\tJoule\n";
print "Energy consume of 3 node: \t\t",(500-$enerygy_3),"\tJoule\n";
print "Energy consume of 4 node: \t\t",(500-$enerygy_4),"\tJoule\n";
print "Energy consume of 5 node: \t\t",(500-$enerygy_5),"\tJoule\n";
print "Energy consume of 6 node: \t\t",(500-$enerygy_6),"\tJoule\n";
print "Energy consume of 7 node: \t\t",(500-$enerygy_7),"\tJoule\n";
print "Energy consume of 8 node: \t\t",(500-$enerygy_8),"\tJoule\n";
print "Energy consume of 9 node: \t\t",(500-$enerygy_9),"\tJoule\n";
# calculate average energy consume
# Total initial energy of all 10 nodes=500J*10=5000J, so we subtract from 5000.
$average_energy_consume=((5000-($enerygy_0+$enerygy_1+$enerygy_2+$enerygy_3+$enerygy_4+$enerygy_5+$enerygy_6+$enerygy_7+$enerygy_8+$enerygy_9))/10);
$total_pkt_in_byte=$pkt_recv_cnt*512;
# print average energy consume
print "Average energy consume:\t", $average_energy_consume,"\n";
close DATA;
exit(0);
# ============================================
For 10 linear nodes, i have simulated the average energy consumed. The graph is as given below:
figure1: Average energy consumed vs Packet inter-sending time of transmitter

The nature of curve suggest us that 5% duty cycle consumes less energy than 10% duty cycle, 10% duty cycle consumes less energy than 15% duty cycle, and so on. Hence no periodic sleep consumes greater energy. The reason behind this is very simple, 5% duty cycle node only listens for 5% of its hearing cycle and hence consumes less energy, whereby no periodic sleep always listens and consumes more energy. The intermediate duty cycles fall in between them.
Taking about traffic, the graph suggests that more traffic (pkt inter sending time 1,2,3)  has more energy consume and less traffic (pkt inter sending time 8,9,10) has less energy consume; which seems obvious since more traffic means more pkt to send and sending more pkt requires more energy to consume. Similarly, less traffic is less number of packet to send, and sending less number of packets requires less energy to consume.

As i am doing project on analysis of S-MAC protocol, suggestions are hearty welcomed. You can mail me at shrestha.sp11 at gmail dot com.

Saturday, May 7, 2011

Calculate Throughput From Trace file

I am doing my final year undergraduate project in Network Simulator. Our project is study and simulation of S-MAC protocol. At the very first stage, we created a wireless scenario, of 10 linear nodes as shown below:
 
 figure1: 10 linear nodes

The each nodes are separated by 200 m distance. The maximum range of hearing of each node is 250 m. So a node can only hear its neighboring node. For example, node 2 can hear only node 1 and node 3. In the simulation, node 0 creates CBR (Constant Bit Rate) traffic packets and transmits it. Node 9 is sink node and receives the incoming traffic packets. After simulation i got my trace file "filename.tr". 

At first i was interested to calculate Average throughput. I used perl script to calculate throughput form this trace file (filename.tr). This script is as below:

# ==============================================================
#THROUGHPUT FOR NEW WIRELESS TRACE
# ==============================================================
# Usage:         
# perl throughput.pl  filename.tr 
# ==============================================================
#Get input file (first arg)
$infile=$ARGV[0];
# new wireless trace format is used. For example:
# s -t 147.236000000 -Hs 9 -Hd -2 -Ni 9 -Nx 0.00 -Ny 0.00 -Nz 0.00 -Ne 905.466702 -Nl MAC -Nw ---[0.06 0 9]
# start some initial values
$flag = 0;
$grantsum = 0;
# Open input file
open (DATA,"<$infile") || die "Can't open $infile $!";
while (<DATA>)
{
    # Tokenize line using space as separator
    @x = split(' ');
    # check if column 0 is for send, column 1 is field for time, column 8 is node 0, packet is cbr
    # $clk_fst stores time of first packet send. Time is column 2. Node id is column 8.
    if (($x[0] eq 's')&&($x[1] eq '-t')&&($x[8] eq '0')&&($x[39] eq 'cbr')&&($flag eq 0))
    {
        $clock_fst=$x[2];
        $flag = 1;
    }
    # $clk_lst stores time of last packet received. Time is column 2.
    # $grantsum stores total packet received in byte.
    if (($x[0] eq 'r')&&($x[1] eq '-t')&&($x[4] eq '9')&&($x[39] eq 'cbr'))
    {
        $grantsum=$grantsum+$x[31];
        $clock_lst=$x[2];
    }
}
# find total time between first packet send and last packet received
$clock=$clock_lst-$clock_fst;
# print throughput
print "End to end throughput:\t ", $grantsum/($clock), "\t(Bytes/sec)\n";
close DATA;
exit(0);
#==============================================================
So our throughput is the total packet received in receiver node (In this case: node 9) divided by time between first packet send and last packet received.

We (me & Gaurav ) have calculated throughput using this perl script for different duty cycle of S-MAC varying packet inter-sending time of transmitter node (increasing and decreasing traffic). A typical result is as shown in graph plotted in 'gnuplot'.
figure 2: Average throughput vs Packet inter-sending time of tx

Increasing packet inter sending time is actually decreasing traffic load. So there is maximum traffic load when x axis has value 1 and minimum traffic load when x axis has value 10. When the traffic load is very light, the five curves nearly overlap, because the five S-MAC modes spend almost the same time to send a fixed number of packets from the source to the sink. So performance is satisfactory. As the traffic load increases, the throughput of all three modes increases with different rate. No periodic sleep has high throughput at high load because it is always on (spends must of the time to send packet) and chance of packet loss is low. But duty cycle of 5% has poor throughput at high load because it is only 5% on (spends less time to send packet) in a cycle and chance of packet loss is high. So performance with sleep listen is poor.

As i am doing project on analysis of S-MAC protocol, suggestions are hearty welcomed. You can mail me at shrestha.sp11 at gmail dot com.



Saturday, April 23, 2011

Plot graphs: gnuplot

I am doing my final year project on network simulator. I had to plot my results in Ubuntu. I found "Gnuplot"; a free, command-driven, interactive, function and data plotting program. We can easily plot graphs of common functions like sine, cosine, sinc, and many more. We can also plot the tabulated data. Gnuplot on my terminal looks like as shown below.
figure: terminal window

Interesting feature about this program is that you don't have to define x-range, x-scale and y-scale. Just type "sin(x)" and you have plotted graph of sin(x). The ranges are suitably defined unlike Matlab, and other languages where you have to define x range from 0 to pi or something like that. I have plotted sin(x) and sin(x)/x as shown in figure below.

figure: plot of sine function
 figure: plot of sinc function

Important commands:
Create a title:                    > set title "title"
Put a label on the x-axis:   > set xlabel "xlabel"
Put a label on the y-axis:   > set ylabel "ylabel"
Change the x-axis range:   > set xrange [x1:x2]
Change the y-axis range:   > set yrange [y1:y2]
For notes and documentation, click here.

Monday, April 4, 2011

Free Space Propagation Model

In any wireless communication system power received by receiver and the path loss are important parameters to be considered. There are several models to calculate these parameters, but the general and easiest one is the free space propagation model. This model is able to calculate power received and path loss for far field. The mathematics of this model is Friss’s free equation which is as follows:
Pt is transmitted power. Pr is received power. Gt is gain of transmitter. Gr is gain of receiver. λ is wavelength. d is distance between transmitter and receiver. Similarly the path loss is 
I have created a Matlab GUI that calculates power received and path loss using above two formulas. It also plots them in dBm amplitude with respect to distance in x-axis. It looks like as follows:
figure: GUI of free space model

It takes input parameters as: Tx power (Pt), frequency (f), transmitter gain (Gt), Receiver gain(G­r) and distance(d). From calculation, it shows Rx power (Pr), path loss (PL) in dBm. It has graph scheme which plots graph of received power vs tx-rx distance and path loss vs tx-rx distance respectively.

Interested can download freespace.m and freespace.fig files, click here.
(Reference: Wireless Communication by Theodore S. Rappaport)

Tuesday, March 29, 2011

Snakes and Ladders: Java Work

Snakes and Ladders was one of the favorite indoor games of my childhood. We called it LUDO. I have made LUDO in NetBeans IDE 6.9(Java). It tested my programming skill. It required concept of classes, objects (OOP); which were completely different than other simple programming languages(C, Matlab). I cant help thanking my friend Pranav, for setting up the groundwork. I faced lots of problems, but at last i made it. Truly writing, i hadn't ever written the codes in such depth. 
(For detail information about LUDO, click here).

My LUDO GUI looks as shown below.
 
My Gui is a simple model of LUDO. I have made it just for fun. It has 2 player scheme, one player red and one player blue. To roll, you have to click on “Click” button. Your rolled number from 1 to 6 is shown above the “Click” button. Similarly the turn of players is shown below that button. Snakes and ladders do their work on the way of gotties. First player reaching "100 place" wins.

Interested viewers can access my source codes/NetBeans files from here.
You can download *.jar file from here.

Thursday, March 17, 2011

MATLAB: Radiation pattern of Thin Linear Antenna


Radiation pattern refers to the plot of radiation properties of any antenna. Thin linear antennas are similar to dipole antennas with symmetric fed source at the center by a balanced two wire transmission line. Moreover linear antennas have sinusoidal current distribution. We consider the diameter of the antenna d<λ/100, which provides good natural approximation to the current distribution. Current distributions for various antennas are as shown in figure below.

Fig. Current distribution (source: JD kraus, Antenna and propagation, Thanks Gaurav for this photo)


I have plotted the radiation patterns of these antennas with the help of Matlab GUI. The pattern is of electric field is in theta direction. The equation is 


This GUI is as shown in the figure below.


Fig. λ/2 antenna

The GUI has facility to choose the antenna size (i.e. length of the antenna, L) = λ/2, 3λ/4, λ, 3λ/2 and 2λ. It plots 3D pattern of chosen antenna as shown in figure above; which is field pattern of λ/2 antenna. The plots of patterns for other antennas are as follows:



Fig. 3λ/4 antenna

Fig. λ antenna

Fig. 3λ/2 antenna
Fig. λ antenna
You can view the source code from here.
(Reference: Antennas and Wave Propagation, JD Kraus, 4th edition)
 

Tuesday, March 15, 2011

Our first year project: Clap Switch

“Clap Switch” simply refers to a switching system whose switching mechanism depends on clapping of our hand. At one clap switch is on and at another clap switch is off. We can turn on or off any electrical equipment with a simple clap. Clap Switch project has been very famous project for first year Electrical and Electronics students of Kathmandu University since it is very simple but requires knowledge of lots of components. One has to use sensor, operational amplifier, filp flop, timer, relay, transistor, etc; looks like whole electronic knowledge is required. The block diagram of our project was as follows.


Our system consists of a microphone which is a clap sensor. The sound of the clap detected by the microphone is sent to the comparator (OP-AMP 741) which compares inverting and no inverting voltages and sends the higher signal to the 555 timer. Now, the signal received by the 555 timer (monostable mode) is converted to a single square wave for a certain time interval. The signal from the 555 timer is sent to the flip flop circuit (IC 4013 DUAL D), which gives logic for the switching. It holds the high signal constantly even after discontinuing the clap and holds low signal when clapped again. The constant signal is feed to the relay which is a switching device. Once the switch gets on triggered by the clap we can lit up the lamp and it gets switched off when again clapped.
The circuit diagram is as follows.
 Hope this content may help first year engineering students. 
(Please don't try to copy this circuit, try to understand electronics behind it. If you become able to understand all electronics behind this, you can design you own circuit diagram.)


Visualization of Electromagnetic wave in MATLAB

Wave is imaginary and mysterious thing to us. Many of them are invisible and we even don’t know their pres...