Variables

Hello guys,
I have a quick question, I have a variable of type double that is changing everytime and it represents signal strength, what i would like to do is to get the differentiated signal strength which is (current signal strength value - previous)

current is SS2
Previous is SS1

but i don't know how to make C++ understand that SS2 and SS1 are different although they are printed using the same variable signal

so the following method is getting the signal strength of the received packets and logging the values

1
2
3
4
5
  void 
Experiment::MonitorSnifferRx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, double signalDbm, double noiseDbm)
{
  
NS_LOG_UNCOND ("A packet has been received with Signal" << signalDbm << " and noise " << noiseDbm << " Rate " << rate<<"and Link Stability"<<LS<<"and Differentiated Signal Strength"<<DSS);


what i would like to do is tell C++ to calculate DSS=SS2-SS1
when i say SS2=signalDbm and SS1 = signalDbm ofcourse i will get DSS =0, how to subtract from the previous value

Best Regards, Sorry for the stupid question , I am just a beginner
Where are SS1 and SS2 in the code?
Is SS2 is current value, you need an SS_previous (or something like that). Just before you calculate and assign an update value to SS2, do this:
1
2
3
SS_previous = SS2;

// calculate new SS2 value. so at this point you'll have both the new value 'SS2', and the previous one 'SS_previous'. 
Last edited on
1
2
3
4
5
6
7
8
9
///
double ss2=0,ss1=0;

while(1){
	cin>> ss2;
	//break;
	cout << "increment = " << ss2-ss1;
	ss1 = ss2;
}
First i initialize the variables before any method like that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private:
  void ReceivePacket (Ptr<Socket> socket);
  void SetPosition (Ptr<Node> node, Vector position);
  Vector GetPosition (Ptr<Node> node);
  void AdvancePosition0 (Ptr<Node> node);
  void AdvancePosition1 (Ptr<Node> node);
  Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
  
  void MonitorSnifferRx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, double signalDbm, double noiseDbm);
  void MonitorSnifferTx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, uint8_t txPower);
  
double stability (double RXThr, double SS1, double SS2, double Thr1, double Thr2, double DSS, double u1, double u2, double LS, double LU);

        // Attributes
         double RXThr;                 	       
         double SS1;    
         double SS2;               		
         //double SS2 = signalDbm;                  		
         double Thr1;           	        
         double Thr2;                     
         double DSS;                           
         double u1;                      
         double u2;                     
         double LS;                    	        
         double LU;    


then the Method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void 
Experiment::MonitorSnifferRx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, double signalDbm, double noiseDbm)
{
  
NS_LOG_UNCOND ("A packet has been received with Signal" << signalDbm << " and noise " << noiseDbm << " Rate " << rate<<"and Link Stability"<<LS<<"and Differentiated Signal Strength"<<DSS);

/////////////////////  
SS2 = signalDbm;
SS1 = signalDbm;

 
stability(RXThr, SS1, SS2, Thr1, Thr2,DSS, u1,u2,LS,LU);

}  


then the method where i calculate DSS is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
double
Experiment::stability(double RXThr, double SS1, double SS2, double Thr1, double Thr2, double DSS, double u1, double u2, double LS, double LU){
        RXThr=10;
        Thr1 = 1.5 * RXThr;
        Thr2 = 1.2 * RXThr;
        u1 = 0.05 * RXThr;
        u2 = 0.03 * RXThr;

        if (SS1>=Thr1)
        {

        LS = 1.0;
        LU = 0.0;
        }
else if (SS1>=Thr2)
        {
        LU=1.0;
        }
else if(SS1>= Thr2 && SS2>=RXThr)
{
	DSS = SS2-SS1;
	LU = 0.0;
	
	if(DSS<= u1)
	{
		LS=1.0;
		LU=0.0;
	}
	else if(DSS>u1 && DSS<=u2)
	{
		LS=(u2-DSS)/(u2-u1);
		LU=0.0;
	}
	else if(DSS>u2)
	{
		LS=0.0;
		LU=0.0;
	}
}


return LS;
@anup30

So in which part of the code i put this:
1
2
3
4
5
6
7
8
9
///
double ss2=0,ss1=0;

while(1){
	cin>> ss2;
	//break;
	cout << "increment = " << ss2-ss1;
	ss1 = ss2;
}

i don't know micro-controllers.

but may help you with programming features.

line 21DSS = SS2-SS1;

after that use SS1 = SS2;

then update SS2;
update DSS;

loop it untill you need.

(did i correctly understand your problem?)

The thing is the function
1
2
3
4
5
void 
Experiment::MonitorSnifferRx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, double signalDbm, double noiseDbm)
{
  
NS_LOG_UNCOND ("A packet has been received with Signal" << signalDbm << " and noise " << noiseDbm << " Rate " << rate<<"and Link Stability"<<LS<<"and Differentiated Signal Strength"<<DSS);


print alot of lines under each other, each with a different value of signalDbm which is SS2,
so i want to subtract the value of the second line from the first and so on,
Its not microcontrollers it is NS-3 a network simulator and this function is sniffing the values of the received signal strength at each packet sent
you need to save the old_SS, so that after the exit of printing function you can retrieve.

so, use a global variable
or send function parameters by reference i.e.
void func(double &old_SS, double &new_SS)
Thanks for answering..
Can you provide the code, In which part of the code above i put it.. I am week @ C++
can you post whole program?
try yo understand this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int dummy;

void func(int data) {
	int increment = data-dummy;
	cout << increment << endl;
	dummy=data;
}

int main()
{
	int arr[]={1,4,8,3,0,4,6,6,4,9};
	dummy=0;

	for(auto x: arr) {
	func(x);
	}
 
return 0;
}
I sent you the code on ur gmail
sorry i don't enough time now to understand that big code.

if you understand my previous example, you can implement yourself in your code.
Topic archived. No new replies allowed.