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

📄 sips04.cpp

📁 This set of simulation files performs a computational complexity performance comparison of the two m
💻 CPP
字号:
/***************************************************************************
Author: Ian C. Wong and Robert Mullenix   
Copyright (C) 2004   Ian C. Wong

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

You may reach the author at wongic@mail.utexas.edu.
Or visit his website at www.ece.utexas.edu/~iwong
******************************************************************************/

// Source release: For computational complexity comparisons
// SIPS 2004 Paper

#include "SIPS.h"

int main()
{
	// Input variables
	int user;					//  Number of users in system, K
	SNRArray *SNR;				//  KxN array of channel conditions
	IDXArray *IDX;				//  KxN array of channel indices
	float *Proportionality;		//  K array of proportionality constants
	
	// Output variables
	int Shen_Assignment[N];		//  N array indicating user k assigned to subcarrier n
	int Wong_Assignment[N];		//  N array indicating user k assigned to subcarrier n			
	float *Data_rate;			//  K array of data rate for kth user, Rk
	int *Hmin;					//  K array of minimum SNR for user k

	
	// Performance indication variables
	float cnt_shen_challoc, cnt_shen_palloc, cnt_wong_challoc, cnt_wong_palloc;
	register clock_t cnt_old, cnt_new;
	int user_num[NUM_USR];
	float shen_aver_chal[NUM_USR];
	float shen_aver_pall[NUM_USR];
	float wong_aver_chal[NUM_USR];
	float wong_aver_pall[NUM_USR];
	float shen_aver_datarate[NUM_USR];
	float tot_dr;
	
	FILE * pFile;				// File pointer for SNR input
	FILE * oFile;				// File pointer for comparison results
	
	int i,j,k,l;
	int error;
	int sum_err = 0;
	
	// Open file containing the channel SNR values generated from Matlab
  	pFile = fopen("data.txt","r");  	
	
	// Main simulation loop
 	for (i = 1; i <= NUM_USR<<1; i++) {
  		user = 2*i;					// Users in system, incremented by 2 each time

  		cnt_shen_palloc = 0;
  		cnt_shen_challoc = 0;
  		cnt_wong_palloc = 0;
  		cnt_wong_challoc = 0;
  		tot_dr = 0;
  		shen_aver_datarate[i-1]=0;			

		for (j = 0; j < NUM_SAMP; j++) {	
#ifdef _DEBUG
			printf("user %d sample %d \n", user, j);
#endif
			// Allocate memory for system variables
			SNR = (SNRArray *)malloc(user*N*sizeof(float));
			IDX = (IDXArray *)malloc(user*N*sizeof(int));
			Proportionality = (float *)malloc(user*sizeof(float));
			Data_rate = (float *)malloc(user*sizeof(float));
			Hmin = (int *)malloc(user*sizeof(int));
			
			// Initialize system varibles
			Sys_Init(user, SNR, IDX, Proportionality, Shen_Assignment, Wong_Assignment, pFile);
			
			// Run Shen's Subcarrier Allocation Algorithm
			cnt_old = clock();   
			Shen_Challoc(SNR, IDX, user, Shen_Assignment, Proportionality, Hmin);			
			cnt_new = clock();  
			cnt_shen_challoc += cnt_new-cnt_old-CLK_OVHD;
			
			for (k = 0; k < user; k++)
				for (l = 0; l < N; l++)
					IDX[k][l] = l;

			// Run Wong's Subcarrier Allocation Algorithm
			cnt_old = clock();  
			Wong_Challoc(SNR, IDX, user, Wong_Assignment, Proportionality, Hmin);
			cnt_new = clock();  
			cnt_wong_challoc += cnt_new-cnt_old-CLK_OVHD;
			
			// Run Shen's Power Allocation
			cnt_old = clock();   
			error = Shen_Palloc(user, SNR, Shen_Assignment, Data_rate, Proportionality, Hmin);			
			cnt_new = clock();
			cnt_shen_palloc += cnt_new-cnt_old-CLK_OVHD;
			sum_err += error;
			
			// Run Wong's Power Allocation
			cnt_old = clock();   
			error = Wong_Palloc(user, SNR, Wong_Assignment, Data_rate, Hmin);			
			cnt_new = clock();
			cnt_wong_palloc += cnt_new-cnt_old-CLK_OVHD;
			
			//iter += sum;
			tot_dr = 0;
			
			for (k = 0; k < user; k++)
				tot_dr+=Data_rate[k];
			shen_aver_datarate[i-1] += tot_dr / user;

			free(SNR);
			free(IDX);
			free(Proportionality);
			free(Data_rate);
			free(Hmin);
		}
		rewind(pFile);
		
		user_num[i-1] = user;
		shen_aver_chal[i-1] = cnt_shen_challoc/(float)NUM_SAMP;
		shen_aver_pall[i-1] = cnt_shen_palloc/(float)NUM_SAMP;
		wong_aver_chal[i-1] = cnt_wong_challoc/(float)NUM_SAMP;
		wong_aver_pall[i-1] = cnt_wong_palloc/(float)NUM_SAMP;
		//aver_iter[i-1] = iter*.01;
		shen_aver_datarate[i-1] = shen_aver_datarate[i-1]/(float)NUM_SAMP;
	}
	fclose(pFile);
	
// Write results into results.txt	
oFile = fopen("results.txt", "w");
for (i = 0; i < NUM_USR; i++)
	fprintf(oFile, "%d %f %f %f %f\n", user_num[i], shen_aver_chal[i], shen_aver_pall[i], wong_aver_chal[i], wong_aver_pall[i]);
fprintf(oFile, "Total number of errors = %d", error);
fclose(oFile);	
}
void Sys_Init(int user, SNRArray *SNR, IDXArray *IDX, 
			  float *Proportionality, int *Shen_Assignment, int *Wong_Assignment, FILE *pFile) {
	int k, n;
	int rand_num;
	register float tmp;
	float sum_Prop = 0.0f;
	
	for (k = 0; k < user; ++k) {
		rand_num = rand();
		if (rand_num < RAND_MAX_X_PT5) 
			Proportionality[k] = 1.0f;
		else if (rand_num < RAND_MAX_X_PT8)
			Proportionality[k] = 2.0f;
		else 
			Proportionality[k] = 4.0f;
		sum_Prop += Proportionality[k];
  		for (n = 0; n < N; ++n) {
  		  	Shen_Assignment[n] = -1;
			Wong_Assignment[n] = -1;
  		  	IDX[k][n] = n;
	  		fscanf(pFile, "%f ", &tmp);
			SNR[k][n] = tmp*100;
	  	}
	}
	for (k = 0; k < user; ++k) 
		Proportionality[k] /= sum_Prop;
}

⌨️ 快捷键说明

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