You are on page 1of 6

Wireless Sensor Networking in Matlab:

Step-by-Step
Milan Simek, Patrik Moravek and Jorge sa Silva
AbstractWe present an extensive guide for the researchers
that aim to validate proposed algorithms for wireless sensor
networks in Matlab environment. In this paper, we describe step
by step all necessary tasks that must be accomplished for the
full operation of the Matlab WSN simulation model. The goal of
this paper is to describe processes how to simulate the lifetime
of the WSN network during a data gathering. We describe tasks
such as topology generation and visualization, data routing and
communication cost calculation. To estimate lifetime of network,
we have proposed the Matlab energy model that goes from the
analysis of the real Zigbe network.
Index Termswireless sensor networks, Matlab, implementa-
tion, simulation, guide, matrix.
I. INTRODUCTION
T
HIS paper introduces step-by-step implementation of
the wireless sensor networking in Matlab environ-
ment(particularly Matlab 2007b). It aims to bring detailed
directions for under/postgraduate students and researches deal-
ing with the wireless sensor simulation under the Matlab
environment. Hence a longterm data gathering is the funda-
mental task of the wireless sensor networks, we show how to
implement all necessary tasks related with the data gathering
processes. In the considered simulation, each battery equipped
node unicasts in the regular intervals (refferred to as rounds of
data gathering) a one packet with the dened size. The packets
are multihoped through the network to the base station that
is power supplied. Simulation ends when there is no path to
the base station, meaning that all neighbors of base station
are out of energy. The results of the simulation are presented
by the colored topology showing the residual energy on each
node and a plot of number of alive nodes in the network
during increasing number of gathering rounds. The rest of
paper is structured as follows: Section I brings the steps of
network topology denition. In Section II, we show how to
nd the shortest path between two nodes in ad-hoc network.
An evaluation of communication cost is introduced in Section
III, while Section IV describes the proposed energy model.
The result of the entire simulation are presented in Section V.
The section VI brings the conclusion and future work.
II. NETWORK TOPOLOGY
A network with N nodes can be modeled as a N-vertex
undirected graph G = (V = {1, ..., N}, E), where 2D/3D
Manuscript received December 15, 2010; revised January 11, 2010.
M. Simek and Patrik Moravek are with the Department of Telecom-
munication of Brno University of Technology, Czech Republic, e-
mail:simek@feec.vutbr.cz
Jorge sa Silva is with Department of Informatics, University of Coimbra,
Portugal, email:sasilva@dei.uc.pt.
position p of each vertice i is identied by the set of coordi-
nates p
i
= (x
i
, y
i
and z
i
respective). An euclidean length d
i,j
between two vertices i, j V is refferred to as E. The network
topology is then interpreted by a three/four row matrix, where
the 2D/3D position of nodes are stored in individual columns,
see Table I. Furthermore for a simplicity, only the 2D plane
is considered for the simulation.
TABLE I
TOPOLOGY MATRIX
ID 1 2 3 ... N
X coor x
1
x
2
x
3
... x
N
Y coor y
1
y
2
y
3
... y
N
For the simulations, the networks are considered to be fully
connected, meaning that all nodes are reachable due to the
multihop communication. The connectivity depends on the
radio range, thus the radio range of the nodes should be
congured optimally. One can estimate the optimal radio range
empirically or to use an Eq. 1 that estimates the minimum
radio range ensuring the full connectivity.
R =

log N
N
(1)
The parameter stands for a 2D plane diameter directly
proportional to the number of nodes N. Calculating the R
for the 100 nodes randomly placed in the 2D plane with the
300m x 500m dimension, the minimal R should be 82 meters.
According to the experiences with the Crossbow IRIS 2.4
GHz node [7], the radio range of 82 meters corresponds to
the transmitting power of 3.2 dBm.
Once the network matrix is created and radio range calcu-
lated, the network topology can be printed. The layout of the
network consists of the vertices and edges between vertices.
The edge or link between two nodes can be printed only in
case that the euclidean distance d
i,j
between two nodes i, j
is smaller than R of the considered nodes. Since the wireless
links are considered to be bidirectional and symmetric then
d
i,j
= d
j,i
. A pseudocode of the layout printing is introduced
in more details in the following section.
In the real wireless network, a distance between two nodes
can be derived from RSSI parameter (Receive Signal Strength
Indication) or estimated by methods such as ToA (Time of
Arrival) or AoA (Angle of Arrival) [11]. These techniques
suffer from the certain distance estimation error and thus this
error should be also implemented into the simulation model.
We consider that range-error of the distance measurement
methods has a Gaussian distribution. The range error
r
is
modeled by the Gaussian distribution with a mean = 0
and a standard deviation . In contrast to the many current
researchers dealing with the range errors modeling, it is
considered that is spread on the both sides from the
value. Thus, the distance r
i,j
measured under the
r
can
be calculated as shown in Eq. (2). The employed approach
expresses situations where the measured signal strength is
either strengthened by the interferences or suppressed by walls
and obstacles. A decision about adding or subtracting the error
portion to the distance should be made randomly.
r
i,j
= d
i,j

d
i,j
100

r

(2)
If some WSN algorithms are to be investigated and theirs
performance compared, they should be tested under the
identical conditions. Hence, it is recommended to use the
identical set of the networks graphs that should have different
topologies (grid, random, L-shape, T-shape), scales (ten
nodes up to thousands nodes) and node degrees (8 up to 26
neighbors per node). For the storing of all prepared networks,
the struct function can be used.
%STORE
someName=struct();
someName.network(:,:,1)=network_1;
save(file.mat,someName);
%LOAD
load file.mat;
network_1=someName.network(:,:,1);
In the previous paragraph we have mention that range of the
node degree should be between 8 and 26. This range was taken
from the work of Bettstetter [2] and Xue and Kumar [14].
They investigated the required R radio range and average node
degree m (average number of neighbors) to ensure the con-
nectivity of network. They stated that network is connected if
for every pair of nodes there exists one-hop link or one multi-
hop link respective. Results of these works showed that node
degree in WSNs considered for probability of the connectivity
greater than 0 has range of < 8, 26 > (see Fig. 1). Thus,
this node degree range was implemented into the network
models so that for each network size N = 50, 100, 400
nodes, ten degrees models were implemented, creating the set
of 30 networks with different network size and degrees. This
network database together with the demonstration Matlab les
can be found in [10].
III. ROUTING IN AD HOC NETWORK
The data routing in wireless sensor network is realized on
the links comparison base. The considered links between a
sender and a receiver can be compared in terms of the length,
link quality or residual energy of the node pairs. Nevertheless,
the path with the smallest investigated value is selected as the
route for the data delivery. For the discovering of the optimal
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
0
20
40
60
80
100
Average node degree []
P
r
o
b
a
b
i
l
i
t
y

o
f

c
o
n
n
e
c
t
i
v
i
t
y

[
%
]

Fig. 1. Probability of connected network for 802.15.4 radio range (redrawn


from [4]).
Algorithm: E=createNbrTable
1: row=1;
2: for all node pairs
3: x=abs(x
i
-x
j
);
4: y=abs(y
i
-y
j
);
5: dist=sqrt(x
2
+ y
2
);
6: if dist
i,j
<R;
7: plot([x
i
, x
j
],[y
i
, y
j
]; %draw edge
8: E(row,1)=ID
i
;
9: E(row,2)=ID
j
;
10: E(row,3)=dist
i
, j;
11: row + +;
E matrix
ID
i
ID
j
dist
i,j
1 2 15.65
1 3 9.21
1 8 21.54
2 1 15.65
2 8 11.12
.
.
.
.
.
.
.
.
.
Fig. 2. Pseudocode of layout visualization and E matrix denition. E matrix
is illustrated bellow code.
route between two nodes in the graph data structure, a Matlab
implementation of Dijkstras algorithm can be used. The
Dijkstras algorithm is implemented within a grShortPath
function that is included in grTheory Matlab toolbox [8].
[dSP,sp
i,j
]=grShortPath(E,ID
i
,ID
j
);
The grShortPath function takes a E matrix of neighbors,
source i and destination j node as an input arguments. It
returns a dSP matrix with the shortest path between all
node pairs in the network. Furthermore, it returns an sp
vectors with the nodes constituting the shortest path between
nodes i, j. The E matrix must have an exact form for the
correct grShortPath algorithm processing. It contains three
columns, where the rst two columns contains the all nodes in
the network that are neighbors of each other and third column
contains the euclidean distance in meters between them. The
E matrix can be created during the network layout printing,
see Fig. 2. As was mentioned before, the link lengths between
all node pairs are compared with the uniform radio range R
and if the condition d
i,j
< R is accomplished the link is
displayed. The E matrix is created within this condition since
all required parameters such as IDs of two neighbors and their
distance d is known and condition of lengths is accomplished.
The context of the E matrix is visualised also in Fig 2.
If the link quality or residual energy are to be used instead
of the distance between nodes for the route establishment, the
information in the third column of E matrix can be substituted
by the required information. Then the path between two nodes
is selected as the path with the highest quality or path with
the maximum energy. Fig. 3 shows results of the described
functions. Source le oneRound.m for the WSN routing can
be found in [10].
0 50 100 150 200 250 300 350 400 450 500
0
50
100
150
200
250
300
x [m]

y


[
m
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Fig. 3. Network layout with the highlighted shortest path estimated by the
grShortPath function.
IV. COMMUNICATION COST OF DATA GATHERING
Once the paths from all nodes to the given node representing
the base station is known, the number of messages per each
node processed during the data gathering can be estimated.
Consider line of nodes n
1
up to n
4
in Fig. 4, where the
n
1
node initiates the unicast transmission. Given message is
propagated to its neighbors due to the omnidirectional nature
of the transmission. The desired neighbor n
2
receives message,
checks the routing information, checks cyclic redundancy
(CRC) and forwards this message through the downstream
path. As nodes use an omnidirectional antenna, a copy of this
message with changed destination address is again received
by the initiator node n
1
. But at this moment, after the whole
message is received, it is discarded since its destination address
does not complies with an ID of a demanded node. Node
n
1
thus overhears the message that was not addressed for it.
The overhearing effect in dense sensor networks was detaily
studied by Basu and Redi [1]. For the simulation, it is
considered to employ a Store-and-Forward mechanism. The
nodes receive whole incoming message and after checking the
header in the buffer take decision about a further processing.
Hence, the energy cost E
Ox
of the overhearing process can
equal to the E
Rx
. The n
1
spends energy E
1
= E
Tx
+ E
Rx
.
Furthermore, the relaying nodes receive message (1 E
RX
),
forward message (1 E
Tx
) and overhears the message sent
from downstream node (1E
Ox
). The energy dissipated by the
relaying nodes equals to E
rel
= E
Tx
+2E
Rx
. This assumption
is implemented into the simulation network model.
ni
Txi
Rxi
b) Broadcast cost
R
Rxi
Rxi
Rxi
Rxi
Rxi
m1 x Rx
Tx2
Tx3
Rx4
Tx1
Transmission of one broadcast message induces
a multiple receptions of its copy on same node:
Ei=ETxi+mi ERxi+1
Rxi
a) Unicast cost
Relay node forwarding unicast
message receives a one copy from
the downstream neighbor:
Ei=ETxi+2ERxi
n1
n2
n3
n4
m2 x Rx
m3 x Rx
n M1
One transmission
process Tx ...
... induces reception
process Rx at all
neighbors
Radio Wave
symbol explanation
n M2
n M3
Fig. 4. Estimation of number of messages processed during unicast and
broadcast transmissions
To express the broadcast cost, the WMA (Wireless Mul-
ticast Advantage) introduced by Wieselthier et al. [13] was
employed. The WMA describes rebroadcasting process as
follows: All nodes lying within the communication range of
the broadcasting node can receive its transmission. Consid-
ering the broadcast transmission where all nodes represent
destinations of the demanded message, the communication
cost of node n
i
(see Fig.4 b) ) that rebroadcasts message is
affected by the presence of its own neighbors. Meaning that as
node n
i
receives and rebroadcasts the message while consumes
E
Rx
+E
Tx
, it also subsequently overhears the communication
of all neighbors. This broadcast overhearing process is refereed
to as a passive acknowledgment. The energy consumed by the
broadcasting nodes can be expressed as:
E
i
= E
Rx
+ E
Tx
+ (m
i
1)E
Rx
(3)
The (m
i
1) parameter represents a convention that n
i
overhears the broadcast message from all nodes M
i
, except
one which receives message from. The M
i
parameter stands
for the set of the neighbor nodes of n
i
. This communication
model was used in many recent works such as [12] and
[13]. Readers interested in multicast transmissions we reffer
to reference [6].
Calculation of the communication cost during a data gath-
ering process can be easily realized by the periodic route
establishment from all nodes to the base station and by
recording of the message number processed by each node on
the each route. The numbers of messages processed can be
recorded into the RxTx matrix (see example in Table. II) that
can classify the number of messages received, transmitted and
overheard.
The message processed by node is recorded only in case
that given node has a sufcient energy to process it. This
TABLE II
RXTX MATRIX
ID 1 2 3 ... N
Rx 10 44 6 ... 154
Tx 2 56 15 ... 89
Ox 15 89 22 ... 354
TOTAL 27 189 43 ... 597
condition is guaranteed as follows. If node i are to send the
message, the shortest route to the base station is established.
Subsequently, for each node in the route is launched control
mechanism that checks if all nodes has the sufcient energy
to perform required task such as transmitting and forwarding
(pure receiving process is realized only by the base station
that is power supplied). To do so, energy model of message
processing is dened and introduced in the following section.
V. ENERGY MODEL OF MESSAGE PROCESSING
To dene the energy model we have studied a Zigbee
specication [9] and supported it with the Zigbee protocol
analysis with Daintree Sensor Network Analyzer [3]. The
PHY and MAC of Zigbee nodes layers are dened by IEEE
802.15.4 while upper communication layers (NWK and APS
layers) are dened by a Zigbee specication. The energy
consumption of the certain communication mode such as sleep,
idle, receive, transmit is directly proportional to time that
given device spent in this mode. Energy consumption of the
communication modes can be found in the device datasheets.
For the simulation we use the information from Memsic IRIS
datasheets [7]. A ATRF230 radio chip implemented in IRIS
consumes 8A in sleep mode, 8 mA in idle mode, 16 mA
during Rx and 17 mA in Tx mode.
If a node wants to send a message, it turns on the radio
chip into the the active mode and before accessing to the
medium it waits a random time in units of backoff period
and subsequently performs CCA (Clear Channel Assessment).
The random number is integer from a range < 0; 2
BE
1 >,
whereas the BE value is incremented with each unsuccessful
attempt [9]. Then the access time to the wireless medium can
be calculated according to Eg. 4 [5]. The CCA detection time
is dened as a period of a 8 symbols (16 s/symbol).
InitiallBackoffPeriod + CCA =
= (2
BE=3
1) UnitBackoffPeriod + CCA
= 7 320 s + 128 s
= 2.368 ms
(4)
If the node successfully accesses to the medium, it rst
sends a Data Request message including ID of the demanded
neighbor. The time that device spends in Rx/Tx mode during
message processing is derived from the message size and the
bitrate [kbps] of the radio chip (b =250 kbps for ATRF230).
It was found by the analysis that size of the Data Request
message is k =12 bytes, see Fig. 5.
Fig. 5. Estimation of Data Request message size.
Thus, the process time of k size message is equal to t
k
=
(k 8)/(b 1000) = 0.38 ms for Data Request message.
Then the energy consumed during transmitting E
Tx
equals to:
U[V] I[A] t[s] = 2.4 V17 mA0, 32 ms = 15 J. Here
U parameter stands for a voltage supplement and I parameter
stands for the current consumption in Tx(power of 3.2 dBm)
mode.
Payload (3 B) Zigbee APS (8 B)
IEEE
802.15.4
(9 B)
Zigbee NWK (8 B)
FCS (2 B)
Fig. 6. Analysis of Zigbee data packet.
When the Data Request message is received by the receiver
device, it transits from Rx to Tx mode (192 s) and sends
back ACK message (size of ACK = 5 bytes). Once the ACK
is received by the origin node it starts with transmitting of
required data. Size of DATA message equals to the size of
DATA PAYLOAD (data eld) plus necessary OVERHEAD
(headers of the APS, NWK, MAC, PHY layers). It was studied
by means of the Daintree analyzer that APS and NWK layer
add 8 bytes plus 8 bytes and MAC and PHY layer extend
the message for 9+2(FCS) bytes, see Fig. 6. Thus the overall
size of DATA message with the 3 bytes payload equals to
30 bytes. The energy consumption during transmission of 30
bytes message equals to 3.9 mJ. The received data are again
acknowledged by the ACK message. It is worthwhile no notice
that Zigbee does not specify CCA and CSMA/CA method to
applied for ACK messages.
For illustration of the entire energy calculation, we assume
only active mode to be included in the calculation together
with the condition that I
Rx
= I
Tx
. Then the entire energy E
consumed by the transmitting device can be derived from the
time that it spends in the active mode:
t
total
=
+
+
=
+
(CCA)+(Tx data request)+(Rx/Tx receiver)+
(Rx ack)+(Rx/Tx sender)+(Tx data)+
(Rx/Tx receiver)+(Rx ack)=
2.368 + 0.32 + 0.192 + 0.16 + 0.192+
0.96 + 0.192 + 0.16 = 4.54 ms
(5)
The entire energy dissipated by the sender radio chip equals
to: E
sender
= 2.4 [V] 17 [mA] 4.54 [ms] = 185 J. Detailed
description of the IEEE 802.15.4 time processing can be found
in [5].
In same way, the energy consumption for all nodes in the
unicast route can be estimated. However, the energy model
should be optimized with the exact current consumption of
the Rx and Tx processes, we have showed only simplied
calculation.
VI. SIMULATION OF DESCRIBED MODEL
The introduced model was implemented in the Matlab
environment to validate its performance. We have congured a
scenario with N = 100 randomly deployed node, where each
node had energy storage of 1 J. The data from all nodes is
unicasted in rounds meaning that during one round, all nodes
unicast data toward the base station (randomly selected node).
The residual energy of the nodes should be visualized by the
color of nodes.
The result of simulation is the energy colored topology and
graph of the live nodes number in dependency on the number
of rounds. As was mentioned in the Introduction section, the
simulation ends when there is no way how to route data to
the base station. It means that all one-hop nodes around base
station depleted own energy. Results of the simulation are
shown in Fig.7 and Fig. 8. One can notice from Fig. 7 that
nodes placed close to the base station (placed on the x-axis) are
loaded with the enormous network trafc since their residual
energy is half in comparison to the far-away nodes.
The simulation statistics showed that nodes with one Joule
transmitted 2243 messages. We can see from Fig. 8 that
network operated more than 160 rounds while the rst nodes
depleted energy before the 80th round (2243 messages trans-
mitted). From the mentioned implies that with the 1 Joule
origin energy, the entire wireless sensor network can transmit
maximally 160 measurements.
VII. CONCLUSION
This paper does not aim to bring the novel results from
the wireless sensor networking. Instead it aims to serve as
100-81% 81-60% 61-40% 41-20% < 20%
0 10 20 30 40 50 60 70 80 90 100 110
0
10
20
30
40
50
60
70
80
90
100
110
x [m]
y
[m
]



Base station
R
Fig. 7. Energy map after simulation.
0 20 40 60 80 100 120 140 160 180
90
91
92
93
94
95
96
97
98
99
100
rounds []

p
e
r
c
e
n
t
a
g
e

o
f

a
l
i
v
e

n
o
d
e
s

[

Fig. 8. Percentage of live nodes during simulation.


the guideline for the student and researchers that want to
validate in Matlab environment own algorithm proposed for
the WSN applications. We have showed how to proposed and
visualize WSN topology and how to route data through the
multihop paths. We have also proposed energy model that
can be used for transformation of the communication cost to
the energy consumption. The ideas in proposed energy model
were supported by the analysis of the real Zigbee network.
We notify, that only simplied energy model was stated here.
The nodes spent most of time in the sleep mode and thus the
signicant attention should be devoted to this factor.
ACKNOWLEDGMENT
This paper was prepared within the framework of No. FRVS
IS18049001 project of the Czech Ministry of Education, Youth
and Sports and No. FRTI2/571 grant project of the Ministry
of Industry and Trade of the Czech Republic
REFERENCES
[1] P. Basu and J. Redi, Effect of Overhearing Transmis-
sions on Energy Efciency in Dense Sensor Networks,
in IPSN 04: Proceedings of the 3rd international sym-
posium on Information processing in sensor networks.
New York, NY, USA: ACM, 2004, pp. 196204.
[2] C. Bettstetter, On the Minimum Node Degree and
Connectivity of a Wireless Multihop Network, in Mo-
biHoc 02: Proceedings of the 3rd ACM international
symposium on Mobile ad hoc networking & computing.
New York, NY, USA: ACM, 2002, pp. 8091.
[3] Daintree Networks. (2010) Sensor Net-
work Analyzer(SNA)@ONLINE. [Online]. Available:
http://www.daintree.net/sna/sna.php
[4] J. C. Hou, N. Li, and I. Stojmenovic, Topology con-
struction and maintanance in wireless sensor networks,
in Handbook of Sensor Networks: Algorithms and Archi-
tectures, I. Stojmenovic, Ed. John Wiley and Sons, Sep.
2005, pp. 311341.
[5] Jennic., Jennic Inc., Calculating 802.15.4 Data Rates,
2006.
[6] M. Koutny, J. Misurec, and P. Mlynek, The new ap-
proaches of a multicasting in the wireless sensor net-
works, in 33rd International Conference on TELECOM-
MUNICATIONS AND SIGNAL PROCESSING TSP -
2010. Budapest, Szent Istvn krt. 7.: Asszisztencia
Szervezo Kft., 2010, pp. 342345.
[7] Memsic Technology Inc., IRIS XM2110CA Data
Sheet. [Online]. Available: http://www.memsic.com
[8] I. Sergii. (2009) grTheory - Graph Theory Toolbox,
MATLAB CENTRAL@ONLINE. [Online]. Available:
http://www.mathworks.com/matlabcentral/leexchange/4266
[9] F. Shanin, Zigbee Wireless Networks and Transceivers.
Oxford: Elsevier, 2008.
[10] M. Simek. (2010) Low Power Wireless &
Sensor Laboratory@ONLINE. [Online]. Available:
http://wsnbase.utko.feec.vutbr.cz
[11] , Reference Node Selection for Anchor free Local-
ization in Wireless Sensor Networks, Ph.D. dissertation,
Brno University of Technology, Purkynova 118, 612 00,
Brno, Czech Republic, September 2010.
[12] S. Singh and C. S. Raghavendra, PAMASPower
Aware Multi-Access Protocol with Signalling for Ad hoc
Networks, SIGCOMM Comput. Commun. Rev., vol. 28,
no. 3, pp. 526, 1998.
[13] J. E. Wieselthier, G. D. Nguyen, and A. Ephremides,
On the Construction of Energy-Efcient Broadcast and
Multicast Trees in Wireless Networks, in INFOCOM
2000. Nineteenth Annual Joint Conference of the IEEE
Computer and Communications Societies. Proceedings.
IEEE, vol. 2, 2000, pp. 585594 vol.2. [Online]. Avail-
able: http://dx.doi.org/10.1109/INFCOM.2000.832232
[14] F. Xue and P. R. Kumar, The Number of Neighbors
Needed for Connectivity of Wireless Networks, Wirel.
Netw., vol. 10, no. 2, pp. 169181, 2004.

You might also like