📄 ogsencdec.c
字号:
else
msg[j] = -1;
scale = scale >> 1;
j++;
}
}
}
return;
}
//
// convert from ASCII to binary
//
static void conv_ASCII_to_binary( char *infile)
{
int len;
char *tmpfile;
FILE *fp;
fp = fopen( infile, "r");
if ( !fp)
{
printf( "Error opening file %s.\n", infile);
exit(-1);
}
else
printf( "Reading file %s.\n", infile);
// getchar();
read_sf1to3( &Nav, fp);
read_sf4( &Nav, fp);
read_sf5( &Nav, fp);
fclose( fp);
encode_navmsg( &Nav, SubFrame);
copy_subframe_to_msg( SubFrame, NavBit);
len = strlen( infile);
tmpfile = conmalloc( len+1);
strcpy( tmpfile, infile);
tmpfile[len-4] = '\0';
// printf( ">%s<\n", tmpfile);
// getchar();
fp = fopen( tmpfile, "wb");
if ( !fp)
{
printf( "Error opening file %s.\n", tmpfile);
exit( -1);
}
fwrite( NavBit, NAVMSGLEN, 1, fp);
fclose( fp);
printf( "Finished writing file '%s'.\n", tmpfile);
return;
}
//
// convert from binary to ASCII
//
static void conv_binary_to_ASCII( char *infile)
{
int nof;
char *tmpfile;
FILE *fp;
tmpfile = infile;
fp = fopen( tmpfile, "rb");
if ( !fp)
{
printf( "Error opening file '%s'.\n", tmpfile);
exit(-1);
}
else
printf( "Reading file '%s'.\n", tmpfile);
nof = fread( NavBit, sizeof( char), NAVMSGLEN, fp);
if ( nof != NAVMSGLEN)
{
printf( "Could only read %d bytes from file %s (expected 1500 bytes).\n",
nof, tmpfile);
exit(-1);
}
fclose( fp);
copy_msg_to_subframe( NavBit, 0, 0, SubFrame);
decode_navmsg( SubFrame, &Nav);
tmpfile = conmalloc( strlen( infile) + 5);
strcpy( tmpfile, infile);
strcat( tmpfile, ".txt");
fp = fopen( tmpfile, "w");
if ( !fp)
{
printf( "Error opening file %s.\n", tmpfile);
exit(-1);
}
write_sf1to3( &Nav, fp);
write_sf4( &Nav, fp);
write_sf5( &Nav, fp);
fclose( fp);
printf( "Finished writing file '%s'.\n", tmpfile);
return;
}
//
// find preamble
//
static void find_preamble( char message[], int *idx, int *invert)
{
int i;
int idxinv, pattern = 0, maxhist, maxhistinv;
short int hist[SUBFRAMELEN];
short int hist_inv[SUBFRAMELEN];
// init to zero
for ( i=0; i<SUBFRAMELEN; i++)
{
hist[i] = 0;
hist_inv[i] = 0;
}
for ( i=0; i<NAVFRAMELEN; i++)
{
pattern = (pattern << 1);
if ( message[i] == 1)
pattern += 1;
pattern &= 0xff;
if ( i >= 7)
{
if ( pattern == 0x8b)
hist[(i-7)%SUBFRAMELEN] += 1;
else if ( pattern == 0x74)
hist_inv[(i-7)%SUBFRAMELEN] += 1;
}
}
*idx = analyze_databit_histogram( &maxhist, hist);
idxinv = analyze_databit_histogram( &maxhistinv, hist_inv);
if ( maxhistinv > maxhist)
{
*idx = idxinv;
*invert = 1;
}
else
*invert = 0;
// printf("find_preamble(): maxhistinv = %d, maxhist = %d\n", maxhistinv, maxhist);
return;
}
//
// copy 1500 bit nav msg to frame buffer sf[][]
// invert = 1: invert msg bits
// invert = 0: don't invert msg bits
//
static void copy_msg_to_subframe( char message[], int ofs, int invert,
unsigned long sf[6][11])
{
int i, j, sfr, word;
unsigned long scale;
// printf( "\n");
j=0;
for ( sfr=1; sfr<=5; sfr++)
{
for ( word=1; word<=10; word++)
{
// scale = 536870912L; // 2^29
scale = 0x1L << 29;
sf[sfr][word] = 0;
for ( i=0; i<30; i++)
{
// printf( " %d", message[j+ofs]);
if ( !invert)
{
if ( message[(j+ofs)%1500] == 1)
sf[sfr][word] += scale;
}
else
{
if ( message[(j+ofs)%1500] != 1)
sf[sfr][word] += scale;
}
scale = scale >> 1;
j++;
}
// printf( " %08x", sf[sfr][word]);
}
// printf( "\n");
}
return;
}
/*******************************************************************************
FUNCTION analyze_databit_histogram()
RETURNS
Start of bit transition (number between 0,...,19)
or -1 (too noise, back to acq)
PARAMETERS
ch : channel number
PURPOSE
Find start of bit transition within 20 x 1 msec segments (bit
synchronization) using histogram method.
REFERENCES
Krunvieda et al., A complete IF software GPS receiver: a tutorial about
the detail, Data Fusion Corporation
WRITTEN BY
G. Beyerle
TO DO
NOTES
*******************************************************************************/
static int analyze_databit_histogram( int *maxhist, short int hist[])
{
int i, maxhistidx;
// unsigned int nofentry = 0;
*maxhist = 0;
for ( i=0; i<300; i++)
{
// nofentry += hist[i];
if ( *maxhist < hist[i])
{
*maxhist = hist[i];
maxhistidx = i;
}
// --- clear array ---
hist[i] = 0;
}
return (maxhistidx);
}
//
// main routine
//
int main( int argc, char *argv[])
{
int j, nof, sgn, len;
long i, nofwritten;
unsigned long cntMSB;
double dt;
char *infile;
char *tmpfile;
FILE *fp;
// printf("argc = %d\n", argc);
// getchar();
if ( argc < 2)
usage();
set_directories( argv[0]);
// process options
getargs( argc, argv);
check_options();
#if 0
printf("argc = %d\n", argc);
printf("optind = %d\n", optind);
printf("argv[0] = %s\n", argv[0]);
printf("argv[1] = %s\n", argv[1]);
printf("argv[2] = %s\n", argv[2]);
getchar();
#endif
if ( argc <= optind)
usage();
infile = argv[optind];
// --- welcome ---
printf("-------------------------------- OpenGPSSim ---------------------------------\n");
printf("ogsencdec version %s\n", Version);
printf("Copyright (C) 2001-2006 G. Beyerle, A. Tabernero, C. Kelley and others. \n");
printf("ogsencdec comes with ABSOLUTELY NO WARRANTY; for details see file 'warranty'.\n");
printf("This is free software, and you are welcome to redistribute it under \n");
printf("certain conditions; see file 'copying' for details. \n");
printf("-----------------------------------------------------------------------------\n");
// data files are located in sub directory '.../data/'
len = strlen( OGSDataDir) + strlen( infile);
tmpfile = conmalloc( len+1);
strcpy( tmpfile, OGSDataDir);
strcat( tmpfile, infile);
len = strlen( tmpfile);
if ( ReadAsyncOutput)
{
// read async files and write as binary and ASCII files
conv_async_to_binaryASCII( tmpfile);
}
else if ( ReadOGSRcvrOutput)
{
// read ogsrcvr files and write as binary and ASCII files
// printf(">%s<", tmpfile + strlen( tmpfile) - 2);
// getchar();
// SatID = atoi( tmpfile + strlen( tmpfile) - 2);
conv_ogsrcvr_to_ASCII( tmpfile);
}
else if ( !strcmp( tmpfile + len-4, ".txt"))
{
// printf(">1:%s<\n", tmpfile);
// getchar();
// convert from ASCII to binary
conv_ASCII_to_binary( tmpfile);
}
else
{
// printf(">2:%s<\n", tmpfile);
// getchar();
// convert from binary to ASCII
conv_binary_to_ASCII( tmpfile);
}
exit( 0);
#if 0
write_sf1to3( &Nav, FpAsc);
write_sf4( &Nav, FpAsc);
write_sf5( &Nav, FpAsc);
fclose( FpAsc);
exit(1);
printf( "Finished writing file.\n");
getchar();
// print_nav_data( &Nav);
char msg[1500];
// encode_navmsg( i4satid, i5satid, &Nav, msg);
//
// decode again
//
find_preamble( msg, &startidx, &invert);
copy_msg_to_subframe( NavBit, startidx, invert, SubFrame);
decode_navmsg( SubFrame, &Nav);
// print_nav_data( &Nav);
//
// compare
//
int k;
for (k=0;k<1500;k+=70)
{
for (j=k;j<k+70;j++)
printf("%d", NavBit[j%1500]);
printf("\n");
for (j=k;j<k+70;j++)
printf("%d", ((msg[j%1500]>0)?1:0));
printf("\n");
getchar();
}
exit(0);
#endif
}
/* ------------------------------ end of file ----------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -