I am trying to read some magnetic fields using Matlab. To do so, I am using Windows XP OS and I am sending the command to continuos reading from the device using Putty. While Putty its running and saving the log file I am reading this log file on Matlab. The Device is a HMR2300.
When I am reading this using ASCII format I am not facing any problems at all. However, I need to collect this data in binary and this is the problem.
The output string is 7 bytes long "Xh|Xl|Yh|Yl|Zh|Zl|" Xh = Signed Byte, X axis - Xl= Low byte, X axis.
Since I have four devices, I am collecting all data inside of a while loop with this code:
%*****% Reading from Device 01
[D1, N] = fread(FidMag1, 7);
while (true)
MagData1 = [MagData1, D1'];
CRLF1 = find(MagData1 == 13);
CRLF1 = [0, CRLF1]';
if (CRLF1(end) ~= 7)
MagData1(1:CRLF1(end))=[];
[D1, N] = fread(FidMag1, 7);
else
break;
end
end
ttt = clock();
TimeStamp1(Count) = ttt(6) + ttt(5)*60 + ttt(4)*3600;
NoCalMagX1(Count) = int16(typecast(uint8(MagData1(1)), 'int8')) * int16(256) + (MagData1(2));
NoCalMagY1(Count) = int16(typecast(uint8(MagData1(3)), 'int8')) * int16(256) + (MagData1(4));
NoCalMagZ1(Count) = int16(typecast(uint8(MagData1(5)), 'int8')) * int16(256) + (MagData1(6));
MagData1(1:CRLF1(end))=[];
So, after that I am reading in a row devices 2, 3, and 4.
This is my result:
Image Link
You can see roughly that before and after 200 sec I am having a delay to collect the data and this is what I need to solve. Usually when my field starts to change, I start to receive a delay and I don't know how to solve it.
I've tried to do a different approach with my code however this is just worst then the first one:
%*****% Reading from Device 01
if (SYNC_1)
[D1, countB] = fread(FidMag1, 7-BIB_1);
if (countB ~= 7)
for a=1:countB
temp(BIB_1+a) = D1(a);
end
end
if (flag_temp)
flag_temp = 0;
else if(find(D1 == 13) == 7)
for a=1:countB
temp(a) = D1(a);
end
end
end
BIB_1 = BIB_1 + countB;
if(BIB_1 == 7)
if (find(temp == 13) == 7)
ttt = clock();
TimeStamp1(Count_1) = ttt(6) + ttt(5)*60 + ttt(4)*3600;
NoCalMagX1(Count_1) = int16(typecast(uint8(temp(1)), 'int8')) * int16(256) + (temp(2));
NoCalMagY1(Count_1) = int16(typecast(uint8(temp(3)), 'int8')) * int16(256) + (temp(4));
NoCalMagZ1(Count_1) = int16(typecast(uint8(temp(5)), 'int8')) * int16(256) + (temp(6));
fprintf('SENSOR B\nX:%.0f Y:%.0f Z:%.0f\n\n', NoCalMagX1(Count_1), NoCalMagY1(Count_1), NoCalMagZ1(Count_1));
Count_1 = Count_1 + 1;
else
SYNC_1 = 0;
end
BIB_1 = 0;
end
end
if (~SYNC_1)
[D1, countB] = fread(FidMag1, 7-BIB_1);
BIB_1 = BIB_1 + countB;
if (BIB_1 > 0)
index = find (D1 == 13);
if(index)
BIB_1 = 7 - index(end);
if(BIB_1 == 0)
flag_temp = 1;
for a=(BIB_1+1):7
temp(a-BIB_1) = D1(a);
end
else
for a=(index+1):countB
temp(aux) = D1(a);
aux = aux + 1;
end
end
aux = 1; %temp index
SYNC_1 = 1;
else
SYNC_1 = 0;
BIB_1 = 0;
end
end
end
Results:
Image Link
Do you guys have any idea how can I solve this? To collect data using ascii my approach its similar and I don't have any problems. Also when I am reading from just one device using the first code I don't have any problems too. I am just lost in how to solve that.
I am trying to implement a wireless sensor network simulator on matlab and I need your help.
This is exactly what I want to do:
Deploy nodes randomly in a 2D plane.
Model a group leader election algorithm using two conditions:
a) Energy: generate random energy values associated with each of the sensors, the sensor node with the maximum energy has higher probability of being selected as leader.
b) Proximity: the sensor node that is mostly surrounded by neighboring nodes has higher probability of being selected.
So, for a random node that has maximum energy and more neighbors can be selected as leader and plotted with the rest of the nodes in a different color.
I have been working on this, trying to develop my code all to no avail. I am not really good with matlab coding, I am still learning.
Please guys, I need any help I can get on this, my deadline is imminent.
Thanks,
Ike
First of all building a wireless networks simulator using matlab is by far not the best choice to do. its better to use specialized simulators such as NS2 or Omnet++. however, I have developed once a simple m-file code to randomly deploy points in a square are and try to link between these points according to the distance between these points. the points are assumed to be sensor nodes.
try to modify on it to get what you need. please vote up if this answer was useful to you:
W=0;X=0;Y=0;Z=0;
nodedistance = zeros();
maxx = 400; maxy=400; maxn = 50;
q = zeros(maxn);
e = rand(maxn,1)*100;
nodeloc = rand(maxn, 2)* maxx;
node(maxn) = struct('NodeNum',zeros(maxn),'nEnergy',zeros(maxn),'Loc',[zeros(maxn,1), zeros(maxn,2)]);
rss(maxn,maxn) = struct('NodeNumber',zeros(maxn),'NodeDistance',zeros(maxn));
for a = 1:100,
m = 2;
node = struct();
rss = struct();
nodedistance = zeros();
maxx = 400; maxy=400; maxn = 50;
q = zeros(maxn);
e = rand(maxn,1)*100;
nodeloc = rand(maxn, 2)* maxx;
for i = 1: maxn,
node(i)=struct('NodeNum',i,'nEnergy',e(i),'Loc',[nodeloc(i, 1), nodeloc(i, 2)]);
end
for i = 1:maxn,
for j = 1:maxn,
rss(i,j) = struct('NodeNumber',i,'NodeDistance',sqrt((node(i).Loc(1)- node(j).Loc(1))^2+(node(i).Loc(2)-node(j).Loc(2))^2));
end
end
for i = 1:maxn,
for j = 1:maxn,
nodedistance(i,j)=rss(i,j).NodeDistance;
end
end
for i = 1:maxn,
for j = 1:maxn,
if (node(i).nEnergy > 0) && (node (j).nEnergy > 0) && (0 < nodedistance(i,j) && nodedistance(i,j) <= 30)
if (node(i).nEnergy < node (j).nEnergy) %|| ( (node(i).nEnergy == node (j).nEnergy )&& (0 < nodedistance(i,j) && nodedistance(i,j)) < 30) %|| (0 < nodedistance(i,j) && nodedistance(i,j) <= 30)
q(i,j) = 1;
elseif node(i).nEnergy == node (j).nEnergy && 0 < nodedistance(i,j) && nodedistance(i,j) < 30
q(i,j) = 1;
else if 0 < nodedistance(i,j) && nodedistance(i,j) <= 30
q(i,j) = 1;
else
q(i,j) = 0;
end
end
end
end
end
end
for i = 1:maxn,
for j = 1:maxn,
if q(i,j) == 1
q(j,i) = 1;
end
end
end
colordef white,
figure(1);
axis equal
for i = 1:maxn,
hold on
box on;
plot(nodeloc(i, 1), nodeloc(i, 2), 'k.', 'MarkerSize', 5);
lscatter(nodeloc(i, 1),nodeloc(i, 2),i);
grid on;
end
gplot(q,nodeloc,'r-');
c = q;
b = 0; zeros(maxn);
check = 1;
while (check)
for m = 2:30
p = size(b)
b = zeros(maxn,maxn);
for i = 1:maxn,
for j = 1:maxn,
if i ~= j
if c(i,j) == 0
for k = 1:maxn,
if c(i,k) >0 && q(k,j) >0
b(i,j) = m;
break
else b(i,j) = 0;
end
end
end
end
end
end
f = size(b)
c = c + b;
if b ==0
check = 0;
else b = 0;% while loop here the condition.
m = m + 1;
end
end
k = 0;
for i = 1:maxn,
for j = 1:maxn,
k = k + c(i,j);
end
end
average_hop = k/(maxn*(maxn-1));
o = 0;
for i = 1:maxn,
for j = 1:maxn,
if c(i,j) ~= 0
o = o + 1;
end
end
end
c(~c) = nan;
r=max(max(c));
t=min(min(c));
clear
end
formatSpec1 = 'The number of hops from node %d to node %d is %d \n';
formatSpec2 = 'The average number of hops is %.4f, the maximum hop-count is %d, the minimum hop-count is %d \n';
formatSpec3 = 'The number of created paths is %d \n';
fileID = fopen('Paths11.txt','w');
fprintf(fileID,formatSpec1,i,j,c(i,j));
fprintf(fileID,formatSpec2,average_hop,r,t);
fprintf(fileID,formatSpec3,o);
clear;
X = X+average_hop;
Y = Y+r;
z = Z+t;
W = W+o;
end;
formatSpec1 = 'The number of hops from node %d to node %d is %d \n';
formatSpec2 = 'The average number of hops is %.4f, the average maximum hop-count is %.2f, the minimum hop-count is %d \n';
formatSpec3 = 'The average number of created paths is %.2f \n';
fileID = fopen('Energy-50-AODV.txt','w');
fprintf(fileID,formatSpec1,i,j,c(i,j));
fprintf(fileID,formatSpec2,X/a,Y/a,t);
fprintf(fileID,formatSpec3,W/a);
clear;
This is not a get-your-homework-done-for-free site; you'll have to do most of the work yourself. Here are my tips:
Don't leave your assignments to the last minute.
Study the MATLAB code of other people working in your field, and refer to the MATLAB help files (hit F1) when you get stuck. Here's a fruitful search query to point you in the right direction: wsn OR "sensor network" clustering matlab code site:edu
I am Trying to convert a MATLAB code to C++ using MATLAB coder but this error apears:
Error indenting generated C code
The error points to the name of the function itself and has no more explanations in it. can someone tell me what is this error?
here is the function i want to conver:
function [Report_Clustered,ClusterCounter_new]=InitClusterGenerator_test(Report_In,~,FreqEpsilon,DegreeEpsilon,~,ClusterCounter_old, BlockCount, Report_old)
Report_M = zeros(size(Report_In,1),size(Report_In,2),4);
for i=1:size(Report_In,1)
for j=1:size(Report_In,2)
Report_M(i,j,1)=Report_In(i,j,1);
Report_M(i,j,2)=Report_In(i,j,2);
Report_M(i,j,3)=0; % Cluster number that the point belongs to.
Report_M(i,j,4)=0;
Report_In{i,j}
end
end
ClusterCounter = 0;
for i=1:size(Report_M,1)
for j=1:size(Report_M,2)
if (Report_M(i,j,3) == 0)
ClusterCounter = ClusterCounter + 1;
Report_M(i,j,3) = ClusterCounter;
for ii=1:size(Report_M,1)
for jj=1:size(Report_M,2)
if (Report_M(ii,jj,3) == 0)
if (abs(Report_M(i,j,1)-Report_M(ii,jj,1))<FreqEpsilon &&...
(abs(Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(-360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon ||...
abs(360 + Report_M(i,j,2)-Report_M(ii,jj,2)) <DegreeEpsilon))
Report_M(ii,jj,3) = ClusterCounter;
end
end
end
end
end
end
end
if (BlockCount> 20 && ClusterCounter<4)
warning = 1;
end
ClusterCounter_new = ClusterCounter;
%clear Report_new;
flag = 0;
Report_new = zeros(ClusterCounter,size (Report_M, 2),4);
index = zeros(1, ClusterCounter_new);
for i = 1: size (Report_M, 1)
for j = 1: size (Report_M, 2)
for k = 1: ClusterCounter_new
if (Report_M(i,j,3) == k)
index(1,k) = index(1,k) + 1;
Report_new(k,index(1,k), 1:3) = Report_M(i,j,1:3);
flag = flag + 1;
end
end
end
end
for j = 1: size (Report_new, 2)
for i = 1: size (Report_new, 1)
if (Report_new(i,j,1) == 0)
Report_new(i,j,1:3) = Report_new(i,1,1:3);
end
end
end
%Report_new = Report;
MedoidF_old = zeros(1, size(Report_old,1));
MedoidA_old = zeros(1, size(Report_old,1));
for i=1:size(Report_old,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_old,2)
SumF = SumF + Report_old(i,j,1);
SumA = SumA + Report_old(i,j,2);
if Report_old(i,j,2) > MaxAngle
MaxAngle = Report_old(i,j,2);
elseif Report_old(i,j,2) < MinAngle
MinAngle = Report_old(i,j,2);
end
end
MedoidF_old(1, i) = SumF/size(Report_old,2);
if (MaxAngle - MinAngle) > 350
MedoidA_old(1, i) = 0;
else
MedoidA_old(1, i) = SumA/size(Report_old,2);
end
end
MedoidF_new = zeros(1, size(Report_new,1));
MedoidA_new = zeros(1, size(Report_new,1));
for i=1:size(Report_new,1)
SumF = 0;
SumA = 0;
MinAngle = 361;
MaxAngle = -1;
for j=1:size(Report_new,2)
SumF = SumF + Report_new(i,j,1);
SumA = SumA + Report_new(i,j,2);
if Report_new(i,j,2) > MaxAngle
MaxAngle = Report_new(i,j,2);
elseif Report_new(i,j,2) < MinAngle
MinAngle = Report_new(i,j,2);
end
end
MedoidF_new(1, i) = SumF/size(Report_new,2);
if (MaxAngle - MinAngle) > 350
MedoidA_new(1, i) = 0;
else
MedoidA_new(1, i) = SumA/size(Report_new,2);
end
end
TempCluster = zeros(1, size(Report_new, 1));
CurrentCluster = ClusterCounter_old;
for i = 1: 1: size(Report_new,1)
for j = 1: 1: size(Report_old,1)
if (abs(MedoidF_old(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_old(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_old(j,1,3);
end
end
%%this part is for seperating the clusters which where in the collision state in the past time
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%first search if the new cluster is a part of a newly found cluster found before this one
for j = 1: 1: i-1
if (abs(MedoidF_new(1,j)-MedoidF_new(1,i))<FreqEpsilon &&...
(abs(MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon ||...
abs(-360 + MedoidA_new(1,j)-MedoidA_new(1,i))<DegreeEpsilon)) %%if the new cluster is the rest of an old cluster use the old one's index for it
TempCluster(1,i) = Report_new(j,1,3);
end
end
end
if (TempCluster(1,i)>0) %%if the new cluster is one of the old ones the index should be set
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1;% Alive
end
else %%new cluster is just began so it needs a new index
CurrentCluster = CurrentCluster + 1;
ClusterCounter_new = CurrentCluster;
TempCluster(1,i) = CurrentCluster;
for j = 1:1:size(Report_new, 2)
Report_new(i,j,3) = TempCluster(1,i);
Report_new(i,j,4) = 1; % Alive
end
end
end
NewClusters = zeros(1, size (Report_new, 1));
for i = 1: size(Report_new, 1)
NewClusters (1,i) = Report_new(i,1,3);
end
OldClusters = zeros(1, size (Report_old, 1));
OldClustersLine = zeros(1, size (Report_old, 1));
for i = 1: size(Report_old, 1)
OldClusters (1,i) = Report_old(i,1,3);
OldClustersLine (1, i) = i;
end
NumberOfDead = 0;
%clear AddDead;
AddDead = zeros (16,size(Report_new, 2),4);
if (BlockCount>10)
for i = 1: size (OldClusters, 2)
IsDead = 1;
for j = 1: size (NewClusters, 2)
if OldClusters(1, i) == NewClusters(1,j)
IsDead = 0;
end
end
if (IsDead == 1)
NumberOfDead = NumberOfDead + 1;
%clear TempLine;
TempLine = zeros(1, size(Report_old,2), 4);
TempLine(1,:,1:3) = Report_old(OldClustersLine(1, i),:,1:3);
for k= 1: size(TempLine, 2)
TempLine(1,k,4) = 0; % Dead
end
TempSize = size(TempLine, 2);
Thresh = size(Report_new, 2);
if (TempSize >= Thresh)
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
else
for l = 1: Thresh-TempSize
TempLine(1, TempSize+l, 1:4) = TempLine(1, TempSize, 1:4);
end
AddDead (NumberOfDead, 1:Thresh, 1:4) = TempLine(1,1:Thresh, 1:4);
end
end
end
xR = size (Report_new,1);
if (NumberOfDead == 0)
Report_Clustered = zeros (size(Report_new,1),size(Report_new,2),size(Report_new,3));
else
Report_Clustered = zeros (size(Report_new,1) + NumberOfDead,size(Report_new,2),size(Report_new,3));
end
Report_Clustered (1:size(Report_new,1), :, :) = Report_new(:,:,:);
for i = 1: NumberOfDead
Report_Clustered(xR + i, :) = AddDead(i, :);
end
end
and I'm using matlab 2012a
Tnx.
From what you've said in the comments, it appears that you simply need to call
clear functions
from the command line before recompiling the function to allow Matlab to overwrite the files. See this Matlab forum or the documentation for clear for more detail.
I have a vector. I want to remove outliers. I got bin and no of values in that bin. I want to remove all points based on the number of elements in each bin.
Data:
d1 =[
360.471912914169
505.084636471948
514.39429429184
505.285068055647
536.321181755858
503.025854206322
534.304229816684
393.387035881967
396.497969729985
520.592172434431
421.284713703215
420.401106087984
537.05330275495
396.715779872694
514.39429429184
404.442344469518
476.846474245118
599.020867750031
429.163139144079
514.941744277933
445.426761656729
531.013596812737
374.977332648255
364.660115724218
538.306752697753
519.042387479096
1412.54699036882
405.571202133485
516.606049132218
2289.49623498271
378.228766753667
504.730621222846
358.715764917016
462.339366699398
512.429858614816
394.778786157514
366
498.760463549388
366.552861126468
355.37022947906
358.308526273099
376.745272034036
366.934599077274
536.0901883079
483.01740134285
508.975480745389
365.629593988233
536.368800360349
557.024236456548
366.776498701866
501.007025898839
330.686029339009
508.395475983019
429.563732174866
2224.68806802212
534.655786464525
518.711297351426
534.304229816684
514.941744277933
420.32368479542
367.129404978681
525.626188464768
388.329756778952
1251.30895065927
525.626188464768
412.313764019587
513.697381733643
506.675438520558
1517.71183364959
550.276294237722
543.359917550053
500.639590923451
395.129864728041];
Histogram computation:
[nelements,centers] = hist(d1);
nelements=55 13 0 0 1 1 1 0 0 2
I want to remove all points apearing less than 5 (in nelements). It means only first 2 elements in nelements( 55, 13 ) remains.
Is there any function in matlab.
You can do it along these lines:
threshold = 5;
bin_halfwidth = (centers(2)-centers(1))/2;
keep = ~any(abs(bsxfun(#minus, d1, centers(nelements<threshold))) < bin_halfwidth , 2);
d1_keep = d1(keep);
Does this do what you want?
binwidth = centers(2)-centers(1);
centersOfRemainingBins = centers(nelements>5);
remainingvals = false(length(d1),1);
for ii = 1:length(centersOfRemainingBins )
remainingvals = remainingvals | (d1>centersOfRemainingBins (ii)-binwidth/2 & d1<centersOfRemainingBins (ii)+binwidth/2);
end
d_out = d1(remainingvals);
I don't know Matlab function for this problem, but I think, that function with follow code is what are you looking for:
sizeData = size(data);
function filter_hist = filter_hist(data, binCountRemove)
if or(max(sizeData) == 0, binCountRemove < 1)
disp('Error input!');
filter_hist = [];
return;
end
[n, c] = hist(data);
sizeN = size(n);
intervalSize = c(2) - c(1);
if sizeData(1) > sizeData(2)
temp = transpose(data);
else
temp = data;
end
for i = 1:1:max(sizeN)
if n(i) < binCountRemove
a = c(i) - intervalSize / 2;
b = c(i) + intervalSize / 2;
sizeTemp = size(temp);
removeInds = [];
k = 0;
for j = 1:1:max(sizeTemp)
if and(temp(j) > a, less_equal(temp(j), b) == 1)
k = k + 1;
removeInds(k) = j;
end
end
temp(removeInds) = [];
end
end
filter_hist = transpose(temp);
%Determines when 'a' less or equal to 'b' by accuracy
function less_equal = less_equal(a, b)
delta = 10^-6; %Accuracy
if a < b
less_equal = 1;
return;
end
if abs(b - a) < delta
less_equal = 1;
return;
end
less_equal = 0;
You can do something like this
nelements=nelements((nelements >5))
I am trying to generate a pn sequence and it works. However, when I try I call the function with different inputs in a for-loop, it gives me the same results each time. As if it is not affected by using the for loop. Why?
This is my code:
%e.g. noof flip flops 4 ==>
function[op_seq]=pnseq(a,b,c)
a = 7;
%generator polynomial x4+x+1 ==>
b = [1 0 0 1 1 0 1 ]
%initial state [1 0 0 0] ==>
c = [1 0 0 0 1 0 1 ]
%refere figure to set a relation between tap function and initial state
%
for j= 1:50,
x = a;
tap_ff =b;
int_stat= c;
for i = 1:1: length(int_stat)
old_stat(i) = int_stat(i);
gen_pol(i) = tap_ff(i);
end
len = (2 ^x)-1;
gen_pol(i+1)= 1;
gen_l = length(gen_pol);
old_l = length(old_stat);
for i1 = 1: 1:len
% feed back input genration
t = 1;
for i2 = 1:old_l
if gen_pol(i2)==1
stat_str(t) = old_stat(gen_l - i2);
i2 = i2+1;
t = t+1;
else
i2 = i2+1;
end
end
stat_l = length(stat_str);
feed_ip = stat_str(1);
for i3 = 1: stat_l-1
feed_ip = bitxor(feed_ip,stat_str(i3 + 1));
feed_ipmag(i1) = feed_ip;
i3 = i3+1;
end
% shifting elements
new_stat = feed_ip;
for i4 = 1:1:old_l
new_stat(i4+1) = old_stat(i4);
old_stat(i4)= new_stat(i4);
end
op_seq(i1) = new_stat(old_l +1);
end
%op_seq;
end
I assume you're doing something like:
for n = 1:10
...
% set a,b,c for this n
...
op_seq =pnseq(a,b,c)
...
end
and that you see the same op_seq output for each case. This is because you have a,b,c as inputs, but you overwrite them at the start of your function. If I remove, or comment out the following lines in your function:
a = 7;
b = [1 0 0 1 1 0 1 ]
c = [1 0 0 0 1 0 1 ]
Then I get different results for calling the function with different a,b,c. There is nothing random in your function, so the same inputs give the same outputs.