📄 sim_util.c
字号:
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include "simulator.h"
#include "sim_util.h"
#include "matrix_tools.h"
extern double **RxPos;
//******************************************************************************
//** Function: PrintHeader
//** Input: None
//** Tasks: Function blanks screen and prints program header
//**
//******************************************************************************
void PrintHeader( void )
{
// clear screen
system ("CLS");
// print header
printf(" *****************************************\n");
printf(" * *\n");
printf(" * L1 C/A 码 *\n");
printf(" * 中频GPS信号模拟器 *\n");
printf(" * *\n");
printf(" * Copywrite 2005 *\n");
printf(" * *\n");
printf(" * 北京一朴科技有限公司 *\n");
printf(" * *\n");
printf(" *****************************************\n");
printf("\n");
}
//******************************************************************************
//** Function: GetOptions
//** Input: no
//** Tasks: Read option file name and get options
//**
//******************************************************************************
int GetOptions( OPTION *pOption) // pointer to option structure
{
int count=0; // generic counter
char filename[100]; // character string containing option filename
FILE *optfile = NULL; // pointer to option file
int err_code = FAILURE;
while(optfile==NULL && count<10)
{
if(count==0) // first prompt
printf("Please enter the name of the option file: ");
// optfile=fopen("sim.txt","r");
else
printf("Error opening option file. Please re-enter option file filename.\n >> ");
scanf("%s", filename);
optfile=fopen(filename,"r");
count++;
// test for valid input
if(optfile==NULL && count==10)
{
printf("Invalid input...Program over!\n");
return err_code;
}
// read the option file
err_code = ReadOptions(pOption,optfile);
// close the input file
fclose(optfile);
// return to calling function
return err_code;
}
return err_code;
}
//******************************************************************************
//** Function: ReadOptions
//** Input: no
//** Tasks: analyse option file and extract option information
//**
//******************************************************************************
int ReadOptions( OPTION *pOption, // option structure to be filled
FILE *input ) // input file
{
char buffer[BUFSIZE]; // input buffer
char optionname[BUFSIZE];
char ctemp[BUFSIZE]; // temporary character string
int itemp1; // temporary integer variables
int result; // stores result of scanning
int err_code = SUCCESS; // flag to indicate validity of parameters
double dtemp; // temporary double variable
// clear the screen
system("CLS");
optionname[56]='\0';
while(!feof(input))
{
fgets(buffer,BUFSIZE,input);
memcpy(optionname,buffer,sizeof(char)*56);
if(strcmp(optionname," Intermediate Frequency (MHz) :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // Intermediate Frequency Used
if (result == 1)
pOption->IF_Frequency = dtemp*1e6;
else
{
err_code = FAILURE;
printf("Invalid Intermediate Frequency. \n");
}
}
else if(strcmp(optionname," Sampling Frequency (MHz) :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // Intermediate Frequency Used
if (result == 1)
pOption->Sampling_Frequency = dtemp*1e6;
else
{
err_code = FAILURE;
printf("Invalid Sampling Frequency. \n");
}
}
else if(strcmp(optionname," C/N0 (dB-Hz) :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // C/N0 for all SV's Used
if (result == 1)
pOption->CN0 = dtemp;
else
{
err_code = FAILURE;
printf("Invalid C/N0 . \n");
}
}
else if(strcmp(optionname," Front End Band Width (MHz) :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // Front end Bandwidth Used
if (result == 1)
pOption->Filter_BandWidth = dtemp *1e6;
else
{
err_code = FAILURE;
printf("Invalid Sampling Frequency. \n");
}
}
else if(strcmp(optionname," Quantization Bits :")==0)
{
result=sscanf(buffer+56,"%d",&itemp1); // Quantization Bit Used
if( (result==1) && ( itemp1 ==1 || itemp1 == 2 || itemp1 == 3 || itemp1 == 4
|| itemp1 == 5 || itemp1 == 6) )
pOption->Quantization_Bit = itemp1;
else
{
err_code = FAILURE;
printf("Invalid Number of Quantization Bit.\n");
}
}
else if(strcmp(optionname," I and Q components :")==0)
{
// Determine if both I and Q components need to be simulated
//
pOption->IQ_Switch = 0; // Only I component will be simulated
result=sscanf(buffer+56,"%s",&ctemp); // Orbit error simulated
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->IQ_Switch = 1; // "YES" Simulate both I and Q components
}
else if(strcmp(optionname," Satellite Orbit Error Simulation (Yes/No) :")==0)
{
pOption->SatObitErr_Switch = 0; // Orbit error not simulated
result=sscanf(buffer+56,"%s",&ctemp); // Orbit error simulated
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->SatObitErr_Switch = 1; // "YES" Simulated
}
else if(strcmp(optionname," Ionospheric Error Simulation (Yes/No) :")==0)
{
pOption->Iono_Switch = 0; // Ionospheric error not simulated
result=sscanf(buffer+56,"%s",&ctemp); // Ionospheric error simulated
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->Iono_Switch = 1; // "YES" Simulated
}
else if(strcmp(optionname," Troposphere Error Simulation (Yes/No) :")==0)
{
pOption->Tropo_Switch = 0; // Tropospheric error not simulated
result=sscanf(buffer+56,"%s",&ctemp);
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->Tropo_Switch = 1; // "YES" Simulated
}
else if(strcmp(optionname," Satellite Clock Error Simulation (Yes/No) :")==0)
{
pOption->SatClkErr_Switch = 0; // Satellite Clock error not simulated
result=sscanf(buffer+56,"%s",&ctemp); // atellite Clock error simulated
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->SatClkErr_Switch = 1; // "YES" Simulated
}
else if(strcmp(optionname," User Clock Error Simulation (Yes/No) :")==0)
{
pOption->RxClkErr_Switch = 0; // User Clock error not simulated
result=sscanf(buffer+56,"%s",&ctemp); // User Clock error simulated
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->RxClkErr_Switch = 1; // "YES" Simulated
}
else if(strcmp(optionname," Multipath Error Simulation (Yes/No) :")==0)
{
pOption->Multipath_Switch = 0; // Multipath error not simulated
result=sscanf(buffer+56,"%s",&ctemp); // Multipath error simulated
if(result==1)
if(strcmpi("YES",ctemp)==0)
pOption->Multipath_Switch = 1; // "YES" Simulated
}
else if(strcmp(optionname," Trajectory Filename :")==0)
{
result=sscanf(buffer+56,"%s",&ctemp); // Trajectory file
if(result==1 && IsFileExist(ctemp))
strcpy(pOption->Traj_filename,ctemp);
else
{
err_code = FAILURE;
printf("Trajectory file does not exist.\n");
}
}
else if(strcmp(optionname," Ephemeris Filename :")==0)
{
result=sscanf(buffer+56,"%s",&ctemp); // Ephemeris file
if(result==1 && IsFileExist(ctemp))
strcpy(pOption->Eph_filename,ctemp);
else
{
err_code = FAILURE;
printf("Ephemeris file does not exist.\n");
}
}
else if(strcmp(optionname," Output Sample Filename :")==0)
{
result=sscanf(buffer+56,"%s",&ctemp); // Output file
if(result==1 )
{
strcpy(pOption->Output_filename_I,"I_");
strcat(pOption->Output_filename_I,ctemp);
strcpy(pOption->Output_filename_Q,"Q_");
strcat(pOption->Output_filename_Q,ctemp);
}
else
{
err_code = FAILURE;
printf("Output file name is nor correct .\n");
}
}
else if(strcmp(optionname," Average Temperature (deg) :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // Front end Bandwidth Used
if (result == 1)
pOption->temperature = dtemp;
else
{
err_code = FAILURE;
printf("Invalid temperature. \n");
}
}
else if(strcmp(optionname," Relative Humidity :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // Front end Bandwidth Used
if (result == 1)
pOption->relativehumidity = dtemp;
else
{
err_code = FAILURE;
printf("Invalid relative humidity. \n");
}
}
else if(strcmp(optionname," Pressure :")==0)
{
result=sscanf(buffer+56,"%lf",&dtemp); // Front end Bandwidth Used
if (result == 1)
pOption->pressure = dtemp;
else
{
err_code = FAILURE;
printf("Invalid pressure. \n");
}
}
}
if(err_code==FAILURE)
return err_code;
else
{
if (pOption->Iono_Switch)
{
ReadIonoPara(pOption);
}
return err_code;
}
}
//******************************************************************************
//** Function: ConfirmOptions
//** Input: Option data structure
//** Tasks: Display option data and let user double check the data
//**
//******************************************************************************
int ConfirmOptions( OPTION *pOption ) // structure with options to be confirmed
{
int err_code;
int key;
// clear the screen
system("CLS");
// print the box and the parameter names
printf(" ******************************************************************************\n");
printf(" *\n");
printf(" * Option Parameters \n");
printf(" *\n");
printf(" *\n");
printf(" * Intermediate Frequency : %15.5f (MHz) \n", pOption->IF_Frequency/1e6);
printf(" * Sampling Frequency : %15.5f (MHz) \n", pOption->Sampling_Frequency/1e6);
printf(" * CN0 : %15.5f (dB-Hz) \n", pOption->CN0);
printf(" * Front End BandWidth : %15.5f (MHz) \n", pOption->Filter_BandWidth/1e6);
printf(" * Quantization Bits : %15d (Bits) \n", pOption->Quantization_Bit);
printf(" * Both I and Q Components : %15s \n", (pOption->IQ_Switch)?"YES":"NO" );
printf(" *\n");
printf(" *\n");
printf(" * Error Simulation (Yes/No)\n");
printf(" *\n");
printf(" * Satellite OrbitError : %15s\n", (pOption->SatObitErr_Switch)?"YES":"NO" );
printf(" * Ionospheric Error : %15s\n", (pOption->Iono_Switch)?"YES":"NO" );
printf(" * Tropospheric Error : %15s \n", (pOption->Tropo_Switch)?"YES":"NO" );
printf(" * Satellite Clock Error : %15s \n", (pOption->SatClkErr_Switch)?"YES":"NO" );
printf(" * User Clock Error : %15s \n", (pOption->RxClkErr_Switch)?"YES":"NO");
printf(" * Multipath Error : %15s \n", (pOption->Multipath_Switch)?"YES":"NO" );
printf(" *\n");
printf(" *\n");
printf(" * Input/Output Files\n");
printf(" *\n");
printf(" * Trajectory File : %20s\n", pOption->Traj_filename);
printf(" * Ephemeris File : %20s\n", pOption->Eph_filename);
if (pOption->IQ_Switch)
{
printf(" * Output Sample File (I) : %20s\n", pOption->Output_filename_I);
printf(" * Output Sample File (Q) : %20s\n", pOption->Output_filename_Q);
}
else
printf(" * Output Sample File : %s\n", pOption->Output_filename_I);
printf(" *\n");
printf(" *\n");
printf(" ******************************************************************************\n");
printf(" Press 'Q' if these parameters are incorrect or any other key to continue...");
key = getch();
if(key=='Q' || key=='q') // if the user wants to quit, print message and stop
{
printf("Parameters not correct...Program over!\n");
err_code = FAILURE;
}
else
err_code = SUCCESS;
return err_code;
}
//******************************************************************************
//** Function: IsFileExist
//** Input: a file name
//** Tasks: Determine if a file exists
//**
//******************************************************************************
int IsFileExist( char *filename)
{
FILE *ftest;
ftest = fopen(filename, "r");
if (ftest == NULL)
return 0;
else
{
fclose (ftest);
return 1;
}
}
//******************************************************************************
//** Function: Load_Trajectory
//** Input: trajectory file name included in option data
//** Tasks: compute user trajectory from the input trajectory file
//**
//******************************************************************************
int Load_Trajectory( OPTION *pOption, long int *pRx_Num )
{
char buffer[BUFSIZE]; // input buffer
char bufstr1[BUFSIZE]; // input buffer
char bufstr2[BUFSIZE]; // input buffer
char bufstr3[BUFSIZE]; // input buffer
int i;
double dtemp;
double Start_time;
double enu_origin[3];
double ini_pos[3] = {0,0,0};
double ini_vel[3];
long int Rx_Pos_Index = 0;
FILE *fpTraj;
fpTraj = fopen(pOption->Traj_filename, "r");
if (fpTraj == NULL)
return FAILURE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -