Please explain the working of this code on mobile user WSN
1 view (last 30 days)
Show older comments
tic clc clear all clf d0=rand; n=4; s=300; w1=0.5;w2=0.5 mrss=-10*n*log10(d0); a=0.5;b=6; d=a+(b-a)*rand(1,s); d=sort(d); for i=1:s RSS_0(i)=mrss-10*n*log10(d(i)/d0)-20; end for i=1:s rss_0(i)=10^(-RSS_0(i)/10); end sum=0; for i=1:s sum=sum+rss_0(i); rss_1(i)=sum; end for i=1:s-1 B(i,1)=-1/2.*(rss_1(i)+rss_1(i+1)); end B(:,2)=1; yn=(rss_0(2:s))'; v=inv(B'*B)*B'*yn; a=v(1); b=v(2); for i=1:s prss_1(i)=rss_1(i); prss_1(i+1)=(rss_0(i)-b/a)*exp(-a*s)+(b/a); prss_0(i)=rss_0(i); prss_0(i+1)=prss_1(i+1)-prss_1(i); end PRSS_0(1:s)=-10*log10(prss_0(2:s+1)); for i=1:s prss_2(i)=w1*rss_0(i)+w2*prss_0(i+1); end PRSS_2=-10*log10(prss_2);
%results and plots fprintf('observed and predicted values of the RSSIs in dB are %g\n') disp([RSS_0' PRSS_0' PRSS_2']) rs_0(1:s)=awgn(RSS_0(1:s),d0-10); prs_0(1:s)=awgn(PRSS_0(1:s),d0-10); prs_2=awgn(PRSS_2,d0-10); plot(d(1:s),rs_0(1:s),d(1:s),prs_0(1:s)) xlabel('distance in meters') ylabel('signal strength in dB') legend('Original RSSI','Predicted RSSI') grid on figure plot(d(1:s),rs_0(1:s),d(1:s),prs_2(1:s)) xlabel('distance in meters') ylabel('signal strength in dB') legend('Original RSSI','Weight Predicted RSSI') grid on toc
Answers (1)
Hari
on 11 Jun 2025
Hi,
I understand that you are simulating and analyzing RSSI (Received Signal Strength Indicator) data over distance, and using a weighted prediction method to estimate RSSI values. You then compare the original and predicted RSSIs using plots.
I assume your main goal is to smooth noisy RSSI values using a linear regression-based approach combined with weighted averaging, and then visualize the improvement via plots.
In order to estimate and compare predicted RSSI values using weighted regression and show them graphically, you can follow the below steps:
Step 1: Initialize parameters and generate random distances
Random values for initial distance d0 and d vector are created.
The reference RSSI mrss is calculated using a log-distance path loss model.
d0 = rand; n = 4; s = 300;
mrss = -10 * n * log10(d0);
d = sort(0.5 + (6 - 0.5) * rand(1, s)); % distances between 0.5 and 6 meters
Step 2: Calculate the original RSSI and convert it to power
RSS_0 is the received signal strength in dB.
Then it's converted from dB to linear scale (power).
for i = 1:s
RSS_0(i) = mrss - 10 * n * log10(d(i)/d0) - 20;
rss_0(i) = 10^(-RSS_0(i)/10);
end
Step 3: Perform linear regression to model cumulative RSSI power
Compute cumulative power (rss_1) and solve linear regression with matrix formulation.
Estimate parameters a and b.
rss_1 = cumsum(rss_0); % cumulative sum
B = [-0.5 * (rss_1(1:end-1) + rss_1(2:end))', ones(s-1, 1)];
yn = rss_0(2:s)';
v = (B' * B) \ (B' * yn);
a = v(1); b = v(2);
Step 4: Predict RSSI power values and apply weighted averaging
Use estimated a and b to compute predicted values (prss_0, prss_1) and convert to dB.
Apply weights w1 and w2 for final smoothing.
for i = 1:s
prss_1(i) = rss_1(i);
prss_1(i+1) = (rss_0(i) - b/a) * exp(-a*s) + (b/a);
prss_0(i+1) = prss_1(i+1) - prss_1(i);
end
PRSS_0 = -10 * log10(prss_0(2:s+1)); % dB conversion
for i = 1:s
prss_2(i) = w1 * rss_0(i) + w2 * prss_0(i+1);
end
PRSS_2 = -10 * log10(prss_2); % final weighted predicted RSSI
Step 5: Add noise and visualize original vs predicted RSSI
Use "awgn" to simulate noisy observations.
Plot original vs predicted RSSIs for comparison.
rs_0 = awgn(RSS_0, d0 - 10);
prs_0 = awgn(PRSS_0, d0 - 10);
prs_2 = awgn(PRSS_2, d0 - 10);
% Plot comparisons
plot(d, rs_0, d, prs_0); grid on;
xlabel('distance in meters'); ylabel('signal strength in dB');
legend('Original RSSI','Predicted RSSI');
figure;
plot(d, rs_0, d, prs_2); grid on;
xlabel('distance in meters'); ylabel('signal strength in dB');
legend('Original RSSI','Weight Predicted RSSI');
References:
https://www.mathworks.com/help/matlab/ref/log10.html
https://www.mathworks.com/help/matlab/ref/awgn.html
https://www.mathworks.com/help/matlab/ref/cumsum.html
https://www.mathworks.com/help/matlab/ref/plot.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!