Asked by Suniti
on 18 Dec 2018

For example, if any location is within 10m of the sensor node, it should be coloured in red and if a location is far away from any node it should be coloured in blue. how can i generate such a heatmap

Answer by Don Mathis
on 4 Jan 2019

Edited by Don Mathis
on 4 Jan 2019

Accepted Answer

Is this closer to what you want?

%% 100 points

rng(0)

xcord = rand(100,1)*100;

ycord = rand(100,1)*100;

xcordt = xcord';

ycordt = ycord';

radius = 5;

figure

new=[xcord ycord];

xxx=linspace(min(new(:,1)),max(new(:,1)),100);

yyy=linspace(min(new(:,2)),max(new(:,2)),100);

[XXX, YYY] = meshgrid(xxx,yyy);

D = pdist2([xcordt(:) ycordt(:)], [XXX(:) YYY(:)], 'euclidean', 'Smallest', 1);

sz = size(XXX);

reds = double(D<=radius) .* (1-D/radius);

blues = double(D>radius) .* ((D-radius)/max(D-radius));

Color = zeros([sz 3]);

Color(:,:,1) = reshape(reds, sz);

Color(:,:,3) = reshape(blues, sz);

image(xxx, yyy, Color);

set(gca, 'XLim', xxx([1 end]), 'YLim', yyy([1 end]), 'YDir', 'normal');

%% first 30 points

rng(0)

xcord = rand(100,1)*100;

ycord = rand(100,1)*100;

radius = 5;

figure

xcord = xcord(1:30);

ycord = ycord(1:30);

xcordt = xcord';

ycordt = ycord';

new=[xcord ycord];

xxx=linspace(min(new(:,1)),max(new(:,1)),100);

yyy=linspace(min(new(:,2)),max(new(:,2)),100);

[XXX, YYY] = meshgrid(xxx,yyy);

D = pdist2([xcordt(:) ycordt(:)], [XXX(:) YYY(:)], 'euclidean', 'Smallest', 1);

sz = size(XXX);

reds = double(D<=radius) .* (1-D/radius);

blues = double(D>radius) .* ((D-radius)/max(D-radius));

Color = zeros([sz 3]);

Color(:,:,1) = reshape(reds, sz);

Color(:,:,3) = reshape(blues, sz);

image(xxx, yyy, Color);

set(gca, 'XLim', xxx([1 end]), 'YLim', yyy([1 end]), 'YDir', 'normal');

Suniti
on 9 Jan 2019

The only thing I am concerned about is that how to merge the colour of boundaries of various nodes.

Walter Roberson
on 9 Jan 2019

Merge in what sense?

Suniti
on 9 Jan 2019

as in the picture above, the area covered by sensor nodes is one solid color and we cannot visually see the exact location of sensor nodes as opposed to the following figure

where boundaries are visible for all sensor locations

Sign in to comment.

Answer by KSSV
on 18 Dec 2018

Read about pcolor, surf, imagec

Suniti
on 27 Dec 2018

thanks, but I was not able to solve my problem.

KSSV
on 27 Dec 2018

Share your data.

Suniti
on 3 Jan 2019

I have 100 sensor nodes placed in a 100*100 square metres square field. Each sensor node has a sensing radius of 5 metres as shown in the figure (where small blue circles and magenta diamonds are the sensors and the dotted blue and magenta circles show their sensing ranges). I want to plot a heatmap that shows the areas that are not covered by sensing ranges of any sensors. The areas which are covered by sensors are to be depicted in shades of red and the areas which are far away from the sensing ranges are depicted in shades of blue (The sensing power keeps on decreasing as distance from centre of sensor keeps on increasing with full sensing at centre and zero at the edge of sensing circle)

Now what I have done uptil now for my code is this:

new=[xcord ycord];

xxx=linspace(min(new(:,1)),max(new(:,1)),100);

yyy=linspace(min(new(:,2)),max(new(:,2)),100);

[XXX, YYY] = meshgrid(xxx,yyy);

D = pdist2([xcordt(:) ycordt(:)], [XXX(:) YYY(:)], 'euclidean', 'Smallest', 1);

imagesc(xxx, yyy, reshape (D, size(XXX)));

set(gca, 'XLim', xxx([1 end]), 'YLim', yyy([1 end]), 'YDir', 'normal');

colormap(flip(jet(), 1))

In the above code, xcord and ycord are 100*1 matrices containing x and y coordinates of 100 sensor nodes respectively. xcordt and ycordt are their transpose forms.

This gives me a figure like this at the start of my network when there all all 100 nodes:

However, when certain sensors are drained of their energy and they die out, I again plot the graph with fewer sensors (30 sensors now) and the graph comes out to be like this:

Now in this graph, visually the number of sensors are 30, but the colormap has increased in size giving the wrong impression that the sensing range of sensors has increased. This may be happening because in my code I have been using smallest euclidean distance as a measure to plot the graph.

What I actually want is that the plot should be a heatmap of points (sensor locations) and their sensing ranges (in circular form) should be continuously coloured from red and centre towards blue at edges and also all the non-covered aeas (non-coveredby sensing ranges of sensors) should be blue as in the figure below:

at start of network:

after few nodes die:

after few more nodes die:

Now in all the above three figures, visually everything is correct and the sensing ranges of all sensors are same. I dont know what am I doing wrong. Please help.

Sign in to comment.

Answer by Walter Roberson
on 18 Dec 2018

Walter Roberson
on 3 Jan 2019

N = 100; %100 square meters

subpixels = 4; %each square meter divided into 4 x 4

sensorradius = 5;

gridN = N * subpixels;

NC = 256; %number of entries in colormap

fullmap = true(gridN, gridN);

usemap = bwdistgeodesic(fullmap, round(x(:)*subpixels), round(y(:)*subpixels)); %note it takes C, R which is X, Y

image(uint8(usemap))

colormap(flipud(jet(NC)))

values in usemap start at 0 and go up. 0 is right at a sensor. Any value up to (sensorradius * subpixels) is considered covered and needs to be colored red. Maximum possible value is (sqrt(2) * N * subpixels) for the case where there is one sensor at one corner, with the maximum distance being at the diagonal opposite corner.

The part about subpixels has to do with the fact that if you were to use 1 pixel per meter then a radius of 5 would involve only an 11 x 11 section, which gives a very crude circle, not even remotely as smooth as what you show in your image. To make a smoother image, we divide each square meter into a 4 x 4 area to give a much smoother approximation. Coordinates in x and y would still be given in meters, but the distance map will be constructed 4 times larger to make the appearance much smoother.

Suniti
on 9 Jan 2019

i am still not getting smooth circles by this, Sir.

Walter Roberson
on 9 Jan 2019

Use a higher subpixels value until you are satisfied with the smoothness.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.