⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 radio.cpp

📁 传感器网络的可靠路由算法
💻 CPP
字号:

#include "stdafx.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "radio.h"


double randn();

double prrdistanceSimple(double d){

if( d > RANGE_THRESHOLD ) 
return 0;
else
return 1;

}
//begin of prr function which calculates the probability of two nodes connecting with each other. 
double prrdistance(double d)
{

double PATH_LOSS_EXPONENT = 3.0;
double SHADOWING_STANDARD_DEVIATION = 3.8;
double PL_D0 = 52;
double D0 = 1.0;



double MODULATION = 3;
double ENCODING = 3;
double OUTPUT_POWER = -7;
double NOISE_FLOOR = -105.0;
double PREAMBLE_LENGTH = 2;
double FRAME_LENGTH = 50;






double avgDecay = OUTPUT_POWER - PL_D0 - (10*PATH_LOSS_EXPONENT*(log(( double)d/D0)/log((float)10)));
double rssi = avgDecay + randn() * SHADOWING_STANDARD_DEVIATION;
double pe;
double prr;
double snr = (pow((double)10,(double)((rssi - NOISE_FLOOR)/10)))/0.64;  


if (MODULATION == 3) 
  pe = 0.5*exp(-0.5*snr);


double preseq = pow((1-pe),(8*PREAMBLE_LENGTH));

 if (ENCODING == 3)  
          prr = preseq*pow((1-pe),(8*2*(FRAME_LENGTH-PREAMBLE_LENGTH)));
                   else
                     printf("error\n");

return  prr;
}
//end of prr function


/* Generate a normal random variable with mean 0 and standard deviation
   of 1.  To adjust to some other distribution, multiply by the standard
   deviation and add the mean.  Box-Muller method  
   note: rand() is a function that returns a uniformly distributed random
    number from 0 to RAND_MAX
*/

double randn()
   {
   static double V2, fac;
   static int phase = 0;
   double S, Z, U1, U2, V1;

   if (phase)
      Z = V2 * fac;
   else
      {
      do {
         U1 = (double)rand() / RAND_MAX;
         U2 = (double)rand() / RAND_MAX;

         V1 = 2 * U1 - 1;
         V2 = 2 * U2 - 1;
         S = V1 * V1 + V2 * V2;
         } while(S >= 1);

      fac = sqrt (-2 * log(S) / S);
      Z = V1 * fac;
      }

   phase = 1 - phase;

     return Z;
   }
//end of randn


//start Q function
double Q(double z) 
	
	{
		double a1 =  0.127414796;
		double a2 = -0.142248368;
		double a3 =  0.7107068705;
		double a4 = -0.7265760135;
		double a5 =  0.5307027145;
		double B  =  0.231641888;
		double t  = 1/(1 + B*z);
	
		if (z >= 0) {
	        return ( ( a1*t + 
				       a2*pow(t,2) + 
				       a3*pow(t,3) + 
					   a4*pow(t,4) + 
					   a5*pow(t,5) ) * exp( -pow(z,2)/2 ) );
		}
		else {
			printf("Error in Q function: argument Z must be greater equal than 0\n");
			exit(0);
			return -1;
		}
}
//end Q function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -