?? asyncappllayer.cc
字號:
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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -