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

📄 rssi_power.c

📁 Atheros AP Test with Agilent N4010A source code
💻 C
📖 第 1 页 / 共 3 页
字号:

	pGoldenCoeffs->pDataPerChannel = (COEFF_DATA_PER_CHANNEL *)malloc(numCh*sizeof(COEFF_DATA_PER_CHANNEL)) ;
	if (NULL == pGoldenCoeffs->pDataPerChannel)
	{
		uiPrintf("unable to allocate golden coeffs datastruct\n");
		A_FREE(pGoldenCoeffs->pChannels);
		exit(0);
	}

	for (ii=0; ii<pGoldenCoeffs->numChannels; ii++)
	{
		pGoldenCoeffs->pDataPerChannel[ii].pCoeff = (double *)malloc(pGoldenCoeffs->numCoeffs*sizeof(double)) ;
		if(NULL == pGoldenCoeffs->pDataPerChannel[ii].pCoeff)
		{
			uiPrintf("unable to allocate coeffs for channel # %d\n", ii);
			break; // will clean up outside the loop
		}
	}

	if (ii != pGoldenCoeffs->numChannels) // allocation loop must've failed
	{
		for (jj=0; jj<ii; jj++)
		{
			if (pGoldenCoeffs->pDataPerChannel[jj].pCoeff != NULL)
				A_FREE(pGoldenCoeffs->pDataPerChannel[jj].pCoeff);
		}
		A_FREE(pGoldenCoeffs->pDataPerChannel);
		A_FREE(pGoldenCoeffs->pChannels);
		uiPrintf("Unable to allocate Golden Coeffs struct. freeing everything allocated so far.\n");
		exit(0);
	}

	ii = 0;
	while(fgets(linebuf, 1020, fp) != NULL)
	{
		extractDoublesFromScientificStr(linebuf, pGoldenCoeffs->pDataPerChannel[ii].pCoeff, pGoldenCoeffs->numCoeffs, &(channel), debug);
		pGoldenCoeffs->pChannels[ii] = (A_UINT16) channel;
		pGoldenCoeffs->pDataPerChannel[ii].channelValue = (A_UINT16) channel;
		ii++;
	}

	if (ii != pGoldenCoeffs->numChannels)
	{
		uiPrintf("actual # channels (%d) differes from declared # channels (%d)\n", ii, pGoldenCoeffs->numChannels);
		exit(0);
	}

	if (debug)
			uiPrintf("SNOOP: done parsing coeffs: \n");


} // parseCoeffTable

void writeIntListToStr(char *deststr, A_UINT32 par1, A_UINT16 *list, A_UINT32 listsize)
{
	char tmpstr[1024];
	A_UINT32 ii;

	strcpy(deststr, "");
	for (ii=0; ii < listsize; ii++)
	{
		sprintf(tmpstr, "%d\t", list[ii]);
		strcat(deststr, tmpstr);
	}
	strcpy(tmpstr, deststr);
	sprintf(deststr, "%d\t%s\n", par1, tmpstr);
} // writeIntListToStr

void writeDoubleListToStr(char *deststr, A_UINT32 par1, double *list, A_UINT32 listsize)
{
	char tmpstr[1024];
	A_UINT32 ii;

	strcpy(deststr, "");

	for (ii=0; ii<listsize; ii++)
	{
		sprintf(tmpstr, "%4.2f\t", list[ii]);
		strcat(deststr, tmpstr);
	}
	strcpy(tmpstr, deststr);
	sprintf(deststr, "%d\t%s\n", par1, tmpstr);
} // writeDoubleListToStr


double getCorrVal(A_UINT32 channel, double rssi, A_UINT32 debug)
{
	A_UINT16 chIdxL, chIdxR, ch_L, ch_R, ii;
	double	 corr_L = 0.0, corr_R = 0.0;

	iGetLowerUpperValues((A_UINT16) channel, pGoldenCoeffs->pChannels, pGoldenCoeffs->numChannels, &ch_L, &ch_R);

	if (debug)
		uiPrintf("chL=%d, chR=%d\n", ch_L, ch_R);

	chIdxL = 0;
	while (chIdxL < pGoldenCoeffs->numChannels)
	{
		if(ch_L == pGoldenCoeffs->pDataPerChannel[chIdxL].channelValue)
			break;
		chIdxL++;
	}

	if(chIdxL == pGoldenCoeffs->numChannels)
	{
		uiPrintf("desired channel %d is outside the range covered by golden coeffs: %d - %d MHz\n", channel,
			pGoldenCoeffs->pChannels[0], pGoldenCoeffs->pChannels[pGoldenCoeffs->numChannels - 1]);
		exit(0);
	}

	if (ch_R != ch_L)
	{
		if (ch_R != pGoldenCoeffs->pDataPerChannel[chIdxL+1].channelValue)
		{
			uiPrintf("interpolation failed : chL, chR (%d, %d), golden channels: %d, %d at idx %d\n",ch_L, ch_R,
				pGoldenCoeffs->pChannels[chIdxL], pGoldenCoeffs->pChannels[chIdxL+1], chIdxL);
			exit(0);
		}
		chIdxR = chIdxL + 1;
	} else
	{
		chIdxR = chIdxL;
	}


	for(ii=0; ii<pGoldenCoeffs->numCoeffs; ii++)
	{
		corr_L += pGoldenCoeffs->pDataPerChannel[chIdxL].pCoeff[ii] * pow(rssi, ii);
		corr_R += pGoldenCoeffs->pDataPerChannel[chIdxR].pCoeff[ii] * pow(rssi, ii);
	}

	if (ch_L == ch_R)
	{
		return(corr_L);
	} else
	{
		return(dGetInterpolatedValue(channel, ch_L, ch_R, corr_L, corr_R));
	}
} // getCorrVal

void openRawRefLogFile(char *filename, A_UINT32 expectedType, A_UINT32 *numCh, A_UINT32 *numDataPoints, A_UINT32 debug)
{
	FILE		*fp;
	char		linebuf[1024];
	A_BOOL		line1 = FALSE; // version #
	A_BOOL		line2 = FALSE; // fileType: 0->coeffs, 1->ref, 2->raw
	A_BOOL		line3 = FALSE; // numChannels
	A_BOOL		line4 = FALSE; // numCoeffs
	A_UINT32	dummy[NUM_COEFFS];
	A_UINT32	filetype;
	double		version;
	A_UINT16	ii;



	uiPrintf("\nReading from file %s\n", filename);
	if( (fp = fopen( filename, "r")) == NULL ) {
		uiPrintf("Failed to open %s to read raw/ref data\n", filename);
		exit(0);
	}

	ii=0;

	while(fgets(linebuf, 1020, fp) != NULL)
	{
		if (!line1)
		{
			if (!sscanf(linebuf, "%lf", &(version)))
			{
				uiPrintf("Could not read version from file %s, line: %s\n", filename, linebuf);
				exit(0);
			} else
			{
				line1 = TRUE;
				if (debug)
					uiPrintf("Coeff file version: %f\n", version);
				continue;
			}
		}

		if (!line2)
		{
			if (!sscanf(linebuf, "%d", &(filetype)))
			{
				uiPrintf("Could not read the file type from file %s, line: %s\n", filename, linebuf);
				exit(0);
			} else
			{
				line2 = TRUE;
				if (filetype != expectedType)
				{
					uiPrintf("File %s is type (%d), not of the expected type (%d).\n", filename, filetype, expectedType);
					exit(0);
				}
				if (debug)
					uiPrintf("Filetype is : %d\n", filetype);
				continue;
			}
		}

		if (!line3)
		{
			if (!sscanf(linebuf, "%d", numCh))
			{
				uiPrintf("Could not read number of channels from file %s, line: %s\n", filename, linebuf);
				exit(0);
			} else
			{
				line3 = TRUE;
				if (debug)
					uiPrintf("Number of channels : %d\n", *numCh);
				continue;
			}
		}

		if (!line4)
		{
			extractIntsFromStr(linebuf, dummy, sizeOfRawPcdacs, numDataPoints, debug);
			line4 = TRUE;
			if (*numDataPoints != sizeOfRawPcdacs)
			{
				uiPrintf("Number of datapoints read (%d) form file (%s) is different from expected (%d)\n", *numDataPoints, filename, sizeOfRawPcdacs);
				exit(0);
			}
			if (debug)
				uiPrintf("Number of datapoints : %d\n", *numDataPoints);
			break;
		}
	} // done reading first 4 lines

	if (filetype == REF_FILETYPE)
	{
		refGU = fp;
	} else
	{
		rawGU = fp;
	}

} // openRawRefLogFile


void extractGoldenCalCoeffs(A_UINT32 mode, A_UINT32 debug)
{
	FILE		*logCal;
	A_UINT32	ii, jj, kk;
	A_UINT32	channel1, channel2;
	char		linebuf1[1024], linebuf2[1024];
	char		filenameRef[64], filenameRaw[64], filenameCoeff[64];
	float		*xdata, *ydata;
	float		coeffs[NUM_COEFFS] ;

	A_UINT32 numDataPoints1, numDataPoints2, numChannels1, numChannels2;

	sprintf(filenameRaw, "%s_%s", modeName[mode], CAL_GU_FILENAME);
	sprintf(filenameRef, "%s_%s", modeName[mode], CAL_PM_FILENAME);
	sprintf(filenameCoeff, "%s_%s", modeName[mode], COEFF_GU_FILENAME);

	openRawRefLogFile(filenameRef, REF_FILETYPE,  &(numChannels1), &(numDataPoints1), debug);
	openRawRefLogFile(filenameRaw, RAW_FILETYPE,  &(numChannels2), &(numDataPoints2), debug);

	if ( (numChannels1 != numChannels2) || (numDataPoints1 != numDataPoints2) )
	{
		uiPrintf("the raw and ref files do not match.\n");
		exit(0);
	}

	xdata = (float *) malloc(numDataPoints1*sizeof(float));
	ydata = (float *) malloc(numDataPoints1*sizeof(float));

	if ( (xdata == NULL) || (ydata == NULL) )
	{
		uiPrintf("Could not allocated for xdata, ydata.\n");
		exit(0);
	}

	uiPrintf("\nLogging Golden Coefficients in File %s\n", filenameCoeff);
	if( (logCal = fopen( filenameCoeff, "w")) == NULL ) {
		uiPrintf("Failed to open %s to write golden cal coeffs\n", filenameCoeff);
		exit(0);
	}
	fprintf(logCal, "%f\n", FAST_CAL_FILES_VERSION);
	fprintf(logCal, "%d\n", COEFF_FILETYPE);
	fprintf(logCal, "%d\n", numChannels1);
	fprintf(logCal, "%d\t", NUM_COEFFS);
	for (ii=0; ii<NUM_COEFFS; ii++)
		fprintf(logCal, "%d\t", ii);
	fprintf(logCal, "\n");
	fflush(logCal);

	uiPrintf("Curve fit results ...\n");
	ii = 0;

	while(fgets(linebuf1, 1020, refGU) != NULL)
	{
		if (fgets(linebuf2, 1020, rawGU) == NULL)
		{
			uiPrintf("Problem reading the raw data file for ch %d.\n", ii);
			exit(0);
		}

		if (debug)
		{
			uiPrintf("line_ref: %s\nline_raw: %s\n", linebuf1, linebuf2);
		}

//		extractDoublesFromStr(linebuf1, ydata, numDataPoints1, &(channel1), debug);
//		extractDoublesFromStr(linebuf2, xdata, numDataPoints1, &(channel2), debug);
		extractFloatsFromStr(linebuf1, ydata, numDataPoints1, &(channel1), debug);
		extractFloatsFromStr(linebuf2, xdata, numDataPoints1, &(channel2), debug);

		if (debug) uiPrintf("ch1=%d, ch2=%d\n", channel1, channel2);

		if (channel1 != channel2)
		{
			uiPrintf("Sequence of channels is different between ref (%d) and raw (%d) files.\n", channel1, channel2);
			exit(0);
		}

		for (jj=0; jj<numDataPoints1; jj++)
		{
			ydata[jj] = xdata[jj] - ydata[jj];
			//uiPrintf("x=%f, y=%f\n", xdata[jj], ydata[jj]);
		}

		curveFit(xdata, ydata, numDataPoints1, NUM_COEFFS, coeffs);

		fprintf(logCal, "%d\t", channel1);
		uiPrintf("%d\t", channel1);
		for (kk=0; kk<NUM_COEFFS; kk++)
		{
			//fprintf(logCal, "%lf\t", coeffs[kk]);
			fprintf(logCal, "%e\t", coeffs[kk]);
			uiPrintf("%e\t", coeffs[kk]);
		}
		fprintf(logCal, "\n");
		uiPrintf("\n");

		ii++;
	}

	if (ii > numChannels1)
	{
		uiPrintf("data found for more channels than declared. Pl. fix the ref/raw files.\n");
		exit(0);
	}

	fclose(logCal);

} // extractGoldenCalCoeffs


void calibrateGoldenForFastCal_GU(A_UINT32 devNum, A_UINT32 mode, A_UINT32 debug)
{


	waitForSync(debug);
	sendAck(devNum, "Ready for ref data collection", mode, CAL_PM_PWR, 0, debug);
	sendCalDataFromGU(devNum, mode, CAL_PM_PWR, debug);

	waitForAck(debug);
	sendAck(devNum, "Ready for raw data collection", mode, CAL_GU_PWR, 0, debug);
	sendCalDataFromGU(devNum, mode, CAL_GU_PWR, debug);

	extractGoldenCalCoeffs(mode, debug);

	closeComms();

} // calibrateGoldenForFastCal_GU

void calibrateGoldenForFastCal_DUT(A_UINT32 devNum, A_UINT32 mode, A_UINT32 debug)
{

//	sendAck(devNum, "Prepare for ref data collection", mode, CAL_PM_PWR, 0, debug);

	sendSync(devNum, CalSetup.goldenIPAddr, debug);
	waitForAck(debug);
	Sleep(500);
	getCalDataUsingGU(devNum, mode, CAL_PM_PWR, debug);

	sendAck(devNum, "Prepare for raw data collection", mode, CAL_GU_PWR, 0, debug);
	waitForAck(debug);
	Sleep(500);
	getCalDataUsingGU(devNum, mode, CAL_GU_PWR, debug);

	closeComms();

} // calibrateGoldenForFastCal_DUT


void fastCalMenu_GU(A_UINT32 devNum)
{
	A_BOOL exitLoop = FALSE;

//	gpibSendIFC(0); // clashes with attenuator !!?!
//	gpibSIC(0); // not necessary
//	gpibRSC(0,1); // request system controller (1)

//** the following remaked by ccshiang
	/***
	uiPrintf("\nSetting up Attenuator");
	guDevAtt = attInit(CalSetup.attGPIBaddr, ATT_11713A);
//	gpibClear(guDevAtt); // not necessary
//	gpibONL(guDevAtt, 1); // not necessary
	attSet(guDevAtt, MAX_ATTENUATOR_RANGE);

	uiPrintf("\nSetting up Power Meter");
	guDevPM = pmInit(CalSetup.pmGPIBaddr, PM_E4416A);
	***/
//** the above remaked by ccshiang


	while(exitLoop == FALSE) {
		printf("\n");
		printf("=============================================\n");
		printf("| Fastcal for mode on GU:(select before DUT)|\n");
		printf("|   a - GU Fastcal Cal for 802.11(a)        |\n");
		printf("|   b - GU Fastcal Cal for 802.11(b)        |\n");
		printf("|   g - GU Fastcal Cal for 802.11(g)        |\n");
		printf("|   q - (Q)uit                              |\n");
		printf("=============================================\n");
		switch(toupper(getch())) {
		case 'A':
			calibrateGoldenForFastCal_GU(devNum, MODE_11a, CalSetup.customerDebug);
			break;
		case 'B':
			calibrateGoldenForFastCal_GU(devNum, MODE_11b, CalSetup.customerDebug);
			break;
		case 'G':
			calibrateGoldenForFastCal_GU(devNum, MODE_11g, CalSetup.customerDebug);
			break;
		case 0x1b:
		case 'Q':
			exitLoop = TRUE;
			uiPrintf("exiting\n");
			break;
		default:
			uiPrintf("Unknown command\n");
			break;
		}
	}
} // fastCalMenu_GU

void fastCalMenu_DUT(A_UINT32 devNum)
{
	A_BOOL exitLoop = FALSE;


	while(exitLoop == FALSE) {
		printf("\n");
		printf("=============================================\n");
		printf("| Fastcal for mode on DUT:(first select GU) |\n");
		printf("|   a - DUT Fastcal Cal for 802.11(a)       |\n");
		printf("|   b - DUT Fastcal Cal for 802.11(b)       |\n");
		printf("|   g - DUT Fastcal Cal for 802.11(g)       |\n");
		printf("|   q - (Q)uit                              |\n");
		printf("=============================================\n");
		switch(toupper(getch())) {
		case 'A':
			calibrateGoldenForFastCal_DUT(devNum, MODE_11a, CalSetup.customerDebug);
			break;
		case 'B':
			calibrateGoldenForFastCal_DUT(devNum, MODE_11b, CalSetup.customerDebug);
			break;
		case 'G':
			calibrateGoldenForFastCal_DUT(devNum, MODE_11g, CalSetup.customerDebug);
			break;
		case 0x1b:
		case 'Q':
			exitLoop = TRUE;
			uiPrintf("exiting\n");
			break;
		default:
			uiPrintf("Unknown command\n");
			break;
		}
	}
} // fastCalMenu_DUT

⌨️ 快捷键说明

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