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

📄 strength.c

📁 802.11仿真源码 对分析RSSI很有用的
💻 C
字号:
/*strength.cBase code written by: Dylan Syme, for Tele4363.Edited and addapted by: Peter TzannesCompilegcc -o <object file> strength.cRun<object file> <A number which corresponds to the number of inputs>INPUT          ->    OUTPUTstrength'i'    ->    strength'i'.txt  This program takes as input all strength'i' files (where 'i' indicates a number)  The program then proceeds to analyse each strength'i' file and produces a   form'i'.txt file in the format PER, room, mp, number of excess packets, the   number of total pacekts and then the average RSSI value.   The main principle of this function is to scan in a line from the 'strength'    file. Then using a while loop as a comparison loop, data is compared/altered/   set or extracted on a charcter by character basis. If statememnts are then    used as error detection, and if errors are found, they are skip over    as described below.Note: - A double is used instead of a float for an increase in accuracy of results- Flagging errors:  A flag counter has been used to detect when less than 10 packets have been   received at a single room. These measurements are dropped, due to the RSSI   needing at least 10 packets for a correct mesurement.   An ignore counter has also been used to detect when packets are less than   10, but are within a 30-second collection phase, this line will also be   ignored for RSSI calculations.  The confidence measurement printed to the output file   is an integer counter that counts the number of lines within a 30-second   period where the number of packets received is greater than 101. - The output file does not include any measurement points that received no   packets. - An END line identifies the last line of the experiment.A bug in Moors program with the first line of all MP. The first line of all MP has a large number of received packets, because the number of packets after on the previous line is the same as the number of packets before on the current line. Due to this bug, it makes it extremely difficult in determining the number of packets > 101 for this line.To remedy this, the first line of all MP is excluded. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <math.h>#include <stdio.h>#define MAX_LINE_SIZE 65  //maximum length of a line in the input file#define MAX_FIELD_SIZE 8  //maximum size of the field, ie.max digits between comma'sint empty(char *tmp){  int j;  for(j = 0 ; j<= MAX_FIELD_SIZE; j++)     tmp[j] = 0;    return(0);}intweather(FILE *in, FILE *out, int seconds){  //int mint = 60;  int hourt = 3600;  int dayt = 86400;  int thir_1_day = 2678400;  int thir_ty_day = 2592000;  int months = 3, day = 1, hour = 0;  //min = 0;  FILE *humidity;  const char *final = "weather";  char line[MAX_LINE_SIZE];  char created[20];  char first[20], second[10];  int found = 0;  seconds = seconds - 1111104000;   //seconds till start of March  if(seconds >= thir_1_day) //March;  {    seconds = seconds - thir_1_day;    months++;  }  if(seconds >= thir_ty_day) //April;  {    seconds = seconds - thir_ty_day;    months++;  }  if(seconds >= thir_1_day) //May;  {    seconds = seconds - thir_1_day;    months++;  }  if(seconds >= thir_ty_day) //June;  {    seconds = seconds - thir_ty_day;    months++;  }  while(seconds >=dayt)  {    seconds = seconds - dayt;    day++;  }  while(seconds >=hourt)  {    seconds = seconds - hourt;    hour++;  }  if((months < 10) && (day < 10) && (hour < 10))    sprintf(created, "20050%d0%d:0%d0000\n",months, day, hour);  else if ((months < 10) && (day < 10))    sprintf(created, "20050%d0%d:%d0000\n",months, day, hour);  else if ((months < 10) && (hour < 10))    sprintf(created, "20050%d%d:0%d0000\n",months, day, hour);  else if ((day < 10) && (hour < 10))    sprintf(created, "2005%d0%d:0%d0000\n",months, day, hour);  else if (months < 10)    sprintf(created, "20050%d%d:%d0000\n",months, day, hour);  else    sprintf(created, "2005%d%d:%d0000\n",months, day, hour);  empty(line);  sprintf(line,"%s.txt",final);  humidity = fopen(line, "r");    if ((humidity) == NULL)    {      printf("fopen failed filename: weather.txt\n");      exit(1);    }  empty(line);    while(!feof(humidity))  {    fgets(line, MAX_LINE_SIZE, humidity);    sscanf(line, "%s %s", first,second);	    first[15] = '\0';    if((first[4] == created[4]) && (first[5] == created[5]) && (first[6] == created[6]) && (first[7] == created[7]) && (first[9] == created[9]) && (first[10] == created[10])) // strcmp doesn't work    {      found++;      fprintf(out,"%s\n",second);            return(0);    }  }  if(found == 0)  {    printf(" Empty Strength File ");    fprintf(out,"-9\n"); //empty strength files  }  return(0);}int extract_data(FILE *in, FILE *out) {  unsigned int length = MAX_LINE_SIZE;  char *line;  int i = 0, j;  int flag = 0, ignore = 0, flag_pkt = 0;  unsigned int room = 1;  // we assume there's no room 0  unsigned int room_last = 0;  unsigned int mp = 0;  unsigned int mp_last = 0;  unsigned int pkt_recv = 0;  unsigned int pkt_lost = 0;  unsigned int qual = 0;  unsigned int pkt_before = 0;  unsigned int pkt_after = 0;  unsigned int pkt_interval = 0;  unsigned int pkt_total = 0;  unsigned int pkt_line = 0;  unsigned int linecnt = 0;  unsigned int pkt_over;   int count = 1;  int sig_lvl = 0, noise_lvl = 0;  float sig_lvl_avg, noise_lvl_avg, fer;  double snr;  //double qual_avg;  line = (char *)malloc(length);  while(!feof(in)) //checks for the EOF character  {        //printf("beginning of file\n");    fgets(line, MAX_LINE_SIZE, in);    char *tmp;           //use a re-usable temporary variable to save memory                      tmp = (char *)malloc(MAX_LINE_SIZE); /*no field should exceed 8 digits,					          this pointer is used to store all accesses variables*/    //printf("nine = %s\n", nine);    if(strcmp(line,"EMPTY") == 0)      {	fprintf(out,"EMPTY");	return(1);      }    flag = 0; // used for errors on a line    empty(tmp);    j=0;	    while(line[i] != ' ')       i++;    i++;    while(line[i] != ',')       {	tmp[j] = line[i];	j++;	i++;      }       //printf("%s\n",tmp);     room = atoi(tmp);    //printf("%d ", room);    empty(tmp);  // We need to re-initialize tmp every time we read data     j=0;       while(line[i] != ' ')      i++;    i++;    while(line[i] != ',')       {	tmp[j] = line[i];	j++;	i++;      }    mp = atoi(tmp);    //printf("%d ", mp);                if((mp != mp_last) || (room != room_last)) //measurements are in a different room -> reinitialise everything      {	if(count != 0)	{	  sig_lvl_avg = ((double) sig_lvl/count);	  noise_lvl_avg = (noise_lvl/count);	}	if(pkt_recv != 0)	{	  fer = ((double)pkt_lost/(pkt_recv + pkt_lost));	}	  	snr = (sig_lvl_avg - noise_lvl_avg);	if(!isnan(fer) && (pkt_total != 0))	 	  {	    fprintf(out,"%g,%d,%d,%d,%d,%f\n",fer, room_last, mp_last, pkt_over, pkt_total, sig_lvl_avg);//no space between the last \n and digit	  }	pkt_recv = 0;	pkt_lost = 0;	qual = 0;	sig_lvl = 0;	noise_lvl = 0;	count = 0;	ignore = 0;	flag_pkt = 0;	pkt_total = 0;	pkt_line = 0;	pkt_interval = 0;	linecnt = 0;	pkt_over = 0;      }    empty(tmp);    j=0;    while(line[i] != ' ')       i++;    i++;    while(line[i] != ',')       {	tmp[j] = line[i];	j++;	i++;      }    if((atoi(tmp) < 10) && (count > 0)) //less than 10 packets received and less than two line of data      flag ++;    else if ((atoi(tmp) < 10) && (count == 0)) //ignore data altotgether, as < 10 packets and on a single line as has       ignore++;    else    {      pkt_line = atoi(tmp); //packets received on a line      pkt_recv = pkt_recv + pkt_line;    }       empty(tmp);    j=0;	    while(line[i] != ' ')      i++;         i++;    while(line[i] != ',')      {	tmp[j] = line[i];	j++;	i++;      }    if(flag == 0)      pkt_lost = (pkt_lost + atoi(tmp));	    empty(tmp);	    j=0;	    while(line[i] != ' ')      i++;	    i++;    while(line[i] != ',')      {	tmp[j] = line[i];	j++;	i++;      }    pkt_before = atoi(tmp);;    empty(tmp);    j=0;    while(line[i] != ' ')       i++;    i++;      while(line[i] != ',')      {	tmp[j] = line[i];	j++;	i++;      }    pkt_after = atoi(tmp);    pkt_interval = pkt_before - pkt_after - pkt_line;    //Don't count the number of packets received at a measurment points when:    //- the packet after field is not 0 (occurs for the first MP of all experiments)    //- flag counter has been set (described above)    //- the current room and mp differ to the next room and mp, that is skip the     //- it is the first line of a MP.     if((pkt_after != 0) && (flag == 0) && (mp == mp_last) && (room == room_last))    {      linecnt++; //lines that are included      pkt_over = pkt_over + pkt_interval; //number of packets that are over the limit      pkt_total = pkt_total + pkt_line; //total number of packets included     }    empty(tmp);    j=0;    while(line[i] != ' ')       i++;    i++;    while(line[i] != ',')       {	tmp[j] = line[i];	j++;	i++;      }    qual = (qual + atoi(tmp));    empty(tmp);    j=0;        while(line[i] != ' ')       i++;    i++;    while(line[i] != ',')       {	tmp[j] = line[i];	j++;	i++;      }    if ((flag == 0) && (linecnt > 0))      sig_lvl = (sig_lvl + (atoi(tmp))); //sub 256 to put it in dBm    empty(tmp);    j=0;	    while(line[i] != ' ')       i++;    i++;        while(line[i] != '\n')       {	tmp[j] = line[i];	j++;	i++;      }    if ((flag == 0) && (linecnt > 0))      noise_lvl = (noise_lvl + (atoi(tmp) - 256)); //sub 256 to put it in dBm        empty(tmp);    j = 0;        i = 0;    pkt_line = 0;    if ((flag == 0) && (linecnt > 0))      count++;    room_last = room;    mp_last = mp;     pkt_before = 0;    pkt_after = 0;  }  // all data has been found  fprintf(out,"END");  return(0);}int main(int argc, char *argv[]) {  FILE *in;  FILE *out;  char file[6], line[MAX_LINE_SIZE], time[MAX_LINE_SIZE];   int seconds;   const char *final = "strength";  const char *strength = "strength";  int max, i;  if(argc != 2)    printf("Need correct arguments: ./a.out <number of forms that which to be opened>");  else    {      max = atoi(argv[1]); //integer representing maximum number of forms      printf("Number of files to analyse is: %d\n", max);      for(i = 1; i<=max ; i++) //step through all forms which are in the arguments      {	  	sprintf(file,"%s%d",final,i);	printf("%s  ", file);		in = fopen(file, "r");  	if ((in) == NULL)	  {	    printf("fopen failed filename: %s\n",file);	    exit(1);	  }      	sprintf(file,"%s%d.txt",strength,i);	out = fopen(file, "w");  	if ((out) == NULL)	  {	    printf("fopen failed filename: %s\n",file);	    exit(1);	  }	rewind(in);	fgets(line, MAX_LINE_SIZE, in);	sscanf(line, "%s", time);			time[10] = '\0';	seconds = atoi(time);	weather(in, out, seconds);	rewind(in);	extract_data(in, out);	printf("Complete\n");		fclose(in);	fclose(out);      }	printf("\n\n  %d forms analysed and created...", --i);    }  return(0);}

⌨️ 快捷键说明

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