Wednesday, 18 April 2018

Stop and Wait Protocol in WSN

# stop and wait protocol in normal situation
# features : labeling, annotation, nam-graph, and window size monitoring
set ns [new Simulator]

set n0 [$ns node]
set n1 [$ns node]

$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"

set nf [open A1-stop-n-wait.nam w]
$ns namtrace-all $nf
set f [open A1-stop-n-wait.tr w]
$ns trace-all $f

$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10

Agent/TCP set nam_tracevar_ true

set tcp [new Agent/TCP]
$tcp set window_ 1
$tcp set maxcwnd_ 1
$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]
$ftp attach-agent $tcp

$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_

$ns at 0.1 "$ftp start"
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"

$ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\""

$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""

$ns at 0.11 "$ns trace-annotate \"Send Packet_0\""
$ns at 0.35 "$ns trace-annotate \"Receive Ack_0\""
$ns at 0.56 "$ns trace-annotate \"Send Packet_1\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack_1\""
$ns at 0.99 "$ns trace-annotate \"Send Packet_2\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack_2  \""
$ns at 1.43 "$ns trace-annotate \"Send Packet_3\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack_3\""
$ns at 1.88 "$ns trace-annotate \"Send Packet_4\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack_4\""
$ns at 2.32 "$ns trace-annotate \"Send Packet_5\""
$ns at 2.55 "$ns trace-annotate \"Receive Ack_5    \""
$ns at 2.75 "$ns trace-annotate \"Send Packet_6\""
$ns at 2.99 "$ns trace-annotate \"Receive Ack_6\""

$ns at 3.1 "$ns trace-annotate \"FTP stops\""

proc finish {} {
 global ns nf
 $ns flush-trace
 close $nf


 puts "running nam..."
 exec nam A1-stop-n-wait.nam &
 exit 0
}

$ns run


Sliding Window Protocol in Wireless Sensor Network

set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]

$ns color 1 red

$ns at 0.0 "$n0 label Host-1"
$ns at 0.0 "$n1 label Host-2"
set nf [open out.nam w]
$ns namtrace-all $nf
set f [open out.tr w]
$ns trace-all $f

$ns duplex-link $n0 $n1 1Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10

Agent/TCP set nam_tracevar_ true
set tcp [new Agent/TCP]
$tcp set windowInit_ 4
$tcp set maxcwnd_ 4
$tcp set fid_ 1
$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]
$ftp attach-agent $tcp

$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_

$ns at 0.1 "$ftp start"
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.0 "finish"

$ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal operation)\""

$ns at 0.52 "$ns trace-annotate \"Send Packet_0,1,2,3\""
$ns at 0.92 "$ns trace-annotate \"Receive Ack_0,1,2,3\""
$ns at 0.93 "$ns trace-annotate \"Send Packet_4,5,6,7\""
$ns at 1.32 "$ns trace-annotate \"Receive Ack_4,5,6,7\""
$ns at 1.33 "$ns trace-annotate \"Send Packet_8,9,10,11\""
$ns at 1.72 "$ns trace-annotate \"Receive Ack_8,9,10,11 \""

$ns at 1.73 "$ns trace-annotate \"Send Packet_12,13,14,15\""
$ns at 2.14 "$ns trace-annotate \"Receive Ack_12,13,14,15\""
$ns at 2.15 "$ns trace-annotate \"Send Packet_16,17,18,19\""
$ns at 2.55 "$ns trace-annotate \"Receive Ack_16,17,18,19\""
$ns at 2.56 "$ns trace-annotate \"Send Packet_20,21,22,23\""
$ns at 2.96 "$ns trace-annotate \"Receive Ack_20,21,22,23\""
$ns at 2.97 "$ns trace-annotate \"FTP stops\""

proc finish {} {

        global ns
        $ns flush-trace
        exec nam out.nam &
        exit 0
}

$ns run

BLACK HOLE ATTACK IN WSN

#===================================
#     Simulation parameters setup
#===================================
set val(chan)   Channel/WirelessChannel    ;# channel type
set val(prop)   Propagation/TwoRayGround   ;# radio-propagation model
set val(netif)  Phy/WirelessPhy            ;# network interface type
set val(mac)    Mac/802_11                 ;# MAC type
set val(ifq)    Queue/DropTail/PriQueue    ;# interface queue type
set val(ll)     LL                         ;# link layer type
set val(ant)    Antenna/OmniAntenna        ;# antenna model
set val(ifqlen) 50                         ;# max packet in ifq
set val(nn)     7                          ;# number of mobilenodes
set val(rp)     AODV                       ;# routing protocol
set val(x)      800                        ;# X dimension of topography
set val(y)      541                        ;# Y dimension of topography
set val(stop)   100.0                      ;# time of simulation end

#===================================
#        Initialization       
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object
set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

#Open the NS trace file
set tracefile [open blackhole.tr w]
$ns trace-all $tracefile

#Open the NAM trace file
set namfile [open blackhole.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
#     Mobile node parameter setup
#===================================
$ns node-config -adhocRouting  $val(rp) \
                -llType        $val(ll) \
                -macType       $val(mac) \
                -ifqType       $val(ifq) \
                -ifqLen        $val(ifqlen) \
                -antType       $val(ant) \
                -propType      $val(prop) \
                -phyType       $val(netif) \
                -channel       $chan \
                -topoInstance  $topo \
                -agentTrace    ON \
                -routerTrace   ON \
                -macTrace      OFF \
                -movementTrace ON

#===================================
#        Nodes Definition       
#===================================
#Create 7 nodes
set n0 [$ns node]
$n0 set X_ 99
$n0 set Y_ 299
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 299
$n1 set Y_ 297
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 499
$n2 set Y_ 298
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 700
$n3 set Y_ 299
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 199
$n4 set Y_ 350
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 599
$n5 set Y_ 350
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
set n6 [$ns node]
$n6 set X_ 600
$n6 set Y_ 200
$n6 set Z_ 0.0
$ns initial_node_pos $n6 20


# Node 5 is given RED Color and a label- indicating it is a Blackhole Attacker
$n5 color red
$ns at 0.0 "$n5 color red"
$ns at 0.0 "$n5 label Attacker"

# Node 0 is given GREEN Color and a label - acts as a Source Node
$n0 color green
$ns at 0.0 "$n0 color green"
$ns at 0.0 "$n0 label Source"

# Node 3 is given BLUE Color and a label- acts as a Destination Node
$n3 color blue
$ns at 0.0 "$n3 color blue"
$ns at 0.0 "$n3 label Destination"

#===================================
#    Set node 5 as attacker   
#===================================
$ns at 0.0 "[$n5 set ragent_] hacker"

#===================================
#        Agents Definition       
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set null1 [new Agent/Null]
$ns attach-agent $n3 $null1
$ns connect $udp0 $null1
$udp0 set packetSize_ 1500

#===================================
#        Applications Definition       
#===================================
#Setup a CBR Application over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 0.1Mb
$cbr0 set random_ null
$ns at 1.0 "$cbr0 start"
$ns at 100.0 "$cbr0 stop"

#===================================
#        Termination       
#===================================
#Define a 'finish' procedure
proc finish {} {
    global ns tracefile namfile
    $ns flush-trace
    close $tracefile
    close $namfile
    exec nam blackhole.nam &
    exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Thursday, 5 April 2018

PCA IN MATLAB

clc;
clear;

R =im2double(imread('C:\\Users\\STUDENT1\\Desktop\\364\\band1.gif'));
G =im2double(imread('C:\\Users\\STUDENT1\\Desktop\\364\\band2.gif'));
B =im2double(imread('C:\\Users\\STUDENT1\\Desktop\\364\\band3.gif'));
I =im2double(imread('C:\\Users\\STUDENT1\\Desktop\\364\\band4.gif'));

[a b]=size(R);

Image=zeros(a*b,4);

k=1;
for i=1:a
    for j=1:b
     Image(k,1)=R(i,j);
     Image(k,2)=G(i,j);
     Image(k,3)=B(i,j);
     Image(k,4)=I(i,j);
     k=k+1;
    end
end

Mean=mean(Image);
Covariance=cov(Image);

[V D]=eig(Covariance);


k=1;
for i=1:512
    for j=1:512
        Final1(i,j)=V(:,1)'*Image(k,:)';
        Final2(i,j)=V(:,2)'*Image(k,:)';
        Final3(i,j)=V(:,3)'*Image(k,:)';
        Final4(i,j)=V(:,4)'*Image(k,:)';
        k=k+1;
    end
end

subplot(2,2,1);
imshow(histeq(double(Final1)));
subplot(2,2,2);
imshow(histeq(double(Final2)));
subplot(2,2,3);
imshow(histeq(double(Final3)));
subplot(2,2,4);
imshow(histeq(double(Final4)));




       
            

KNN For Images In MATLAB

clc;
clear all;
I1= im2double(imread('E:\band\band1.gif'));
I2= im2double(imread('E:\band\band2.gif'));
I3= im2double(imread('E:\band\band3.gif'));
I4= im2double(imread('E:\band\band4.gif'));

fileID = fopen('dp1.txt','r');
formatSpec = '%i';
A = fscanf(fileID,formatSpec);
mat1=zeros(50,1);
A=A';
 c=1;
 for j=1:50
      mat1(j,1)=I1(A(c),A(c+50));
      mat1(j,2)=I2(A(c),A(c+50));
      mat1(j,3)=I3(A(c),A(c+50));
      mat1(j,4)=I4(A(c),A(c+50));
      c=c+1;
 end
fileID2 = fopen('dp2.txt','r');
formatSpec2 = '%i';
B = fscanf(fileID2,formatSpec2);
B=B';
 mat2=zeros(100,1);

c=1;
for j=101:150
     mat1(j,1)=I1(B(c),B(c+100));
     mat1(j,2)=I2(B(c),B(c+100));
     mat1(j,3)=I3(B(c),B(c+100));
     mat1(j,4)=I4(B(c),B(c+100));
     c=c+1;
end
k=9;
for i=1:512
    for j = 1:512
        for l = 1:150
        distr(k) = norm([I1(i,j),I2(i,j),I3(i,j),I4(i,j)] - [mat1(l,1),mat1(l,2),mat1(l,3),mat1(l,4)]);
        k=k+1;
        end
        k=1;
        [ans,index] = sort(distr);
        if(index(1) <=50)
            I1(i,j) = 255;
        else
            I1(i,j) = 0;
        end
    end
end

imshow(I1);

Source Code For K-Means Clustering For Images (In MATLAB)

clear all;

kc = 4;

x = double(zeros(512,512,4));
x(:,:,1) = imread('E:\band\band1.gif');
x(:,:,2) = imread('E:\band\band2.gif');
x(:,:,3) = imread('E:\band\band3.gif');
x = double(x);

centers = zeros(kc,3);
for i = 1:kc
    temp = zeros(1,2);
    temp(:) = randi([1,512],1,2);
    centers(i,1) = x(temp(1),temp(2),1);
    centers(i,2) = x(temp(1),temp(2),2);
    centers(i,3) = x(temp(1),temp(2),3);
end

for l = 1:5
    count = zeros(kc,1);
    new_c = zeros(kc,3);
    for i = 1:512
        for j = 1:512
            dist = double(zeros(kc));
            min = intmax;
            pointer = -1;
            for k = 1:kc
                v1 = [x(i,j,1),x(i,j,2),x(i,j,3)];
                v2 = [centers(k,1),centers(k,2),centers(k,3)];
                dist(k) = norm(v1-v2);
                if dist(k) < min
                    min = dist(k);
                    pointer = k;
                end
            end
            x(i,j,4) = pointer;
            count(pointer) = count(pointer) + 1;
            new_c(pointer,1) = new_c(pointer,1) + x(i,j,1);
            new_c(pointer,2) = new_c(pointer,2) + x(i,j,2);
            new_c(pointer,3) = new_c(pointer,3) + x(i,j,3);
        end
    end
    for i = 1:kc
        centers(i,1) = new_c(i,1)/count(i);
        centers(i,2) = new_c(i,2)/count(i);
        centers(i,3) = new_c(i,3)/count(i);
    end
    disp(l);
end

for i = 1:512
    for j = 1:512
        p = x(i,j,4);
        switch(p)
            case 1
                x(i,j,:) = [255,255,255,1];
            case 2
                x(i,j,:) = [255,0,0,2];
            case 3
                x(i,j,:) = [0,255,0,3];
           case 4
              x(i,j,:) = [0,0,255,4];
%             case 5
%                 x(i,j,:) = [300,0,100,5];
        end
    end
end

y = x(:,:,1:3);
imshow(y,[]);