asyncappllayer.cc

来自「基于omnet++开发的Mf框架下的802.11协议仿真。」· CC 代码 · 共 1,090 行 · 第 1/3 页

CC
1,090
字号
    	 	 else
    	 	 {
    	 	 	 int size=temp.size();
    	 	 	 dataQueue.push_back(temp.front());
      		 dataQueue.push_back(size);
      		 temp.clear();
      		 temp.push_back(clusterHead_data[row][col]);
    	 	 }
    	 }
    	}
    	
    	for(int row=former_row;row<behind_row;++row)
    	{
    		for(int col=0;col<former_col;++col)
    		{
    			if(temp.empty()||clusterHead_data[row][col]==temp.front())
    	 	 	temp.push_back(clusterHead_data[row][col]);
    	 	 
    	 	  else
    	 	  {
	    	 	 	 int size=temp.size();
	    	 	 	 dataQueue.push_back(temp.front());
	      		 dataQueue.push_back(size);
	      		 temp.clear();
	      		 temp.push_back(clusterHead_data[row][col]);
    	 	  }
    		}
    	}
    	
    	for(int row=former_row;row<behind_row;++row)
    	{
    		for(int col=former_col;col<behind_col;++col)
    		{
    			if(temp.empty()||clusterHead_data[row][col]==temp.front())
    	 	 	temp.push_back(clusterHead_data[row][col]);
    	 	 
    	 	  else
    	 	  {
	    	 	 	 int size=temp.size();
	    	 	 	 dataQueue.push_back(temp.front());
	      		 dataQueue.push_back(size);
	      		 temp.clear();
	      		 temp.push_back(clusterHead_data[row][col]);
    	 	  }
    		}
    	}
    	
    	former_row=behind_row;
    	behind_row*=2;
    	former_col=behind_col;
    	behind_col*=2;
    }
    std::cout<<"coding!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
    if(temp.size()!=0)
    {
    	int size=temp.size();
  		dataQueue.push_back(temp.front());
  		dataQueue.push_back(size);
  		temp.clear();
    }
    for(int it=0;it<32;it++)
    	  for(int j=0;j<8;j++)
    	     clusterHead_data[it][j]=0.0;
    /***********************end******************************/    int i=dataQueue.size()%8;
    totalCounter=dataQueue.size()/8;
     if(i!=0)
    {totalCounter+=1;}
    std::cout<<"totalCounter:"<<totalCounter<<std::endl;
    //计算压缩比写入文本
    std::ofstream outfile( "yasuobi.txt", std::ios_base::app );
  	outfile<<1.0*36/(totalCounter+4)<<endl;
 
	  outfile.close();
       
    creatNewPkt(totalCounter);
}


/** @brief 簇头编码后,重新打包发送*/
void AsyncApplLayer::creatNewPkt(int sum)
{
  std::cout<<endl;
  std::cout<<"################################################"<<endl;
  std::cout<<"Begin to creating new pkt"<<endl;
  std::cout<<"The sum="<<sum<<endl;
  int counter=1;
   //将小波变换后的数据打包发送
  for(int j=0;j<sum;j++)
  {
	   ApplPkt *pk = new ApplPkt();  
	   pk->setSrcAddr(myApplAddr());
	   pk->setDestAddr(destModuleID);
	   pk->setLength(headerLength);
	   pk->setTimestamp(timeStamp); //?
	   pk->setSeqNum1(sum+4);
	   pk->setSeqNum2(counter);
	   for(int i=0;i<8;i++)
	   {
	     	if(dataQueue.size()!=0)
	     	  {pk->setData(i,dataQueue.front());dataQueue.pop_front();}
	     	else
	     	  pk->setData(i,0);                                                 
	   }
	   //下面为测试输出
	   std::cout<<"the No."<<pk->getSeqNum2()<<"is:"<<endl;
	   for(int ii=0;ii<8;ii++)
	   {
	     std::cout<<pk->getData(ii)<<" ";	
	   }
	   std::cout<<endl;
	  // 	
	   counter++;
	   sendDown(pk);
   
  }  
  //将数据报原地址打包发送
   for(int j=0;j<4;j++)
	 {
	   ApplPkt *pk = new ApplPkt();  
	   pk->setSrcAddr(myApplAddr());
	   pk->setDestAddr(destModuleID);
	   pk->setLength(headerLength);
	   pk->setTimestamp(timeStamp); //?
	   pk->setSeqNum1(sum+4);
	   pk->setSeqNum2(counter);
	   for(int i=0;i<8;i++)
	   {
	     	pk->setData(i,addr[j*8+i]);
	     	addr[j*8+i]=0;	                                              
	   }
	   //下面为测试输出
	   std::cout<<"the No."<<pk->getSeqNum2()<<"is:"<<endl;
	   for(int ii=0;ii<8;ii++)
	   {
	     std::cout<<pk->getData(ii)<<" ";	
	   }
	   std::cout<<endl;
	  // 	
	   counter++;
	   sendDown(pk);
	 }
  std::cout<<"################################################"<<endl;  
}  


   
/**   @brief 簇头数据矩阵小波逆变换*/
void AsyncApplLayer::unDo_clusterWave()
{
 
  std::cout<<endl;
  std::cout<<"unDo_clusterWave....................."<<endl;
  //首先将数据串转换为数组  int former_row=0;
  int behind_row=4;
  int former_col=0;
  int behind_col=1;
  while(former_col<8)
  {
  	for(int row=0;row<former_row;++row)
  	{
  	 for(int col=former_col;col<behind_col;++col)
  	 {
  	 	 sink_data[row][col]=dataQueue1.front();	 
  	 	 dataQueue1.pop_front();
  	 }
  	}
  	
  	for(int row=former_row;row<behind_row;++row)
  	{
  		for(int col=0;col<former_col;++col)
  		{
  			sink_data[row][col]=dataQueue1.front();	 
  	 	  dataQueue1.pop_front();
  	 	}
  	}
  	
  	for(int row=former_row;row<behind_row;++row)
  	{
  		for(int col=former_col;col<behind_col;++col)
  		{
  			sink_data[row][col]=dataQueue1.front();	 
  	 	  dataQueue1.pop_front();
  	 	}
  	}
  	
  	former_row=behind_row;
  	behind_row*=2;
  	former_col=behind_col;
  	behind_col*=2;
  }
       	


	
  //对数组sink_data进行逆变换
  //数组改变,小波逆变换也要修改 5.10 By fangshi   //行操作
   for(int j=0;j<8;j++)
   {
   	  double temp[32];
      int h=1;//三级变换      while(h<=4)
      {
                 
        for(int i=0;i<4*h;i++)
        {
          temp[2*i]=((sink_data[i][j]+sink_data[i+4*h][j])/2);
          temp[2*i+1]=((sink_data[i][j]-sink_data[i+4*h][j])/2);		
        }
        for(int i=0;i<(8*h);i++)
        {
          sink_data[i][j]=temp[i];	
        }
        h=h*2;
      }
   }  
   for(int i=0;i<32;i++)
       for(int j=0;j<8;j++)
       {
         if(abs(sink_data[i][j])<0.001)
             sink_data[i][j]=0.0;
       }
  //列操作
  
   for(int i=0;i<32;i++)
   {
    double temp[8];
    int h=1;
    while(h<=4)
        {
                   
          for(int j=0;j<h;j++)
          {
            temp[2*j]=((sink_data[i][j]+sink_data[i][j+h])/2);
            temp[2*j+1]=((sink_data[i][j]-sink_data[i][j+h])/2);		
          }
          for(int j=0;j<(2*h);j++)
          {
            sink_data[i][j]=temp[j];	
          }
          h=h*2;
        }
   }
   
   //将结果写入文件
  std::cout<<"add result to file****************"<<endl;
  std::ofstream outfile( "result.txt", std::ios_base::app );
 	for(int i=0;i<32;i++)
  {
  if(addr_sink[i]!=0) 
  {for(int j=0;j<8;j++)
     {
       	outfile<<sink_data[i][j]<<" ";
     }
    outfile<<endl;
  }
  else
  	{lostCounter++;}
  }
	outfile.close();
  if(lostCounter!=0)
   { std::ofstream outfile1( "lostpkt.txt", std::ios_base::app ); 
  	//outfile1<<adr<<","<<lostCounter<<endl;
    outfile1<<lostCounter<<endl;
  	outfile1.close();
  	adr=0;
    lostCounter=0;
  }
}
 

/** @brief 簇头解码*/
void AsyncApplLayer::unDo_code()
{
	unDo_clusterWave();
}


void AsyncApplLayer::finish()
{/**
	tempIterator = timeVector.begin();
	std::ofstream outfile( "msg.out", std::ios_base::app );
    	if ( ! outfile ) { // 打开失败    		std::cerr << "cannot open \"copy.out\" for output\n";
    		//exit( -1 );
    		EV<<"error"<<endl;
    	}	
	while( tempIterator != timeVector.end() )
	{
		for(int i=0;i<8;i++)
		    outfile<<(*tempIterator)->getData(i)<<"  ";
		outfile<<endl;
		++tempIterator;
	}
	outfile.close();*/
	m_iterator it=dmap.begin();
	m_iterator end=dmap.end();
	for(;it!=end;++it)
	{
		std::cout<<"cluster head's address is: "<<(*it).first<<", the received packet number is: "<<(*it).second.size()<<std::endl;
	}
}

/**@brief 簇头单数据报逆小波变换 */

void AsyncApplLayer::addToFile(ApplPkt *msg)
{
	std::cout<<"add to file****************"<<endl;
	std::ofstream outfile( "sink.out", std::ios_base::app );
  if ( ! outfile ) { // 打开失败     std::cerr << "cannot open \"copy.out\" for output\n";
     EV<<"error"<<endl;
  }
  outfile<<(msg->getSeqNum1()+1)<<":";
  for(int i=0; i<8; ++i)
  {
  	outfile<<msg->getData(i)<<",";
  }	
  outfile<<endl;
	outfile.close();
}

/** 计算数据误差*/
void AsyncApplLayer::test()
{
  double test1[32][8];//小波变换、量化后数据
  for(int i=0;i<32;i++)
     for(int j=0;j<8;j++)
       test1[i][j]=clusterHead_data[i][j];
  
  for(int j=0;j<8;j++)
  {
   	double temp[32];
    int h=1;//三级变换    while(h<=4)
    {                   
       for(int i=0;i<4*h;i++)
       {
         temp[2*i]=((test1[i][j]+test1[i+4*h][j])/2);
         temp[2*i+1]=((test1[i][j]-test1[i+4*h][j])/2);		
       }
       for(int i=0;i<(8*h);i++)
       {
          test1[i][j]=temp[i];	
       }
       h=h*2;
    }
  } 
  double sum=0;
  for(int i=0;i<32;i++)
    for(int j=0;j<8;j++) 
     	sum+=pow((test_data[i][j]-test1[i][j]),2);
  double mse=1.0*sum/256;
  std::cout<<"mse="<<mse<<endl;
  std::ofstream outfile( "mse.txt", std::ios_base::app );

  outfile<<mse<<endl; 
	outfile.close();
}

⌨️ 快捷键说明

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