gpt4 book ai didi

c - 访问外部生成的数组中的值

转载 作者:行者123 更新时间:2023-11-30 17:01:57 25 4
gpt4 key购买 nike

我正在获取电压读数并将其转换为压力读数。这是我第一次真正使用 C 语言,所以我的代码很困惑,但到目前为止运行良好。我面临的问题是让程序计算介于最小和最大期望值之间的读数数量(存储在数组 data[i] 中)。

这是代码,Sum2 和 count 是给我带来麻烦的地方。 Sum2 添加 999 个值而不是过滤,并且 count 始终结果为 998,而它应该接近 500

编辑:

数据[i]中的读数是电压,我正在使用压力。我的校准曲线是 P=(V-2.9674)/.404

    //Voltage readings for NI USB-6009 built from the ground up

#include "stdafx.h"
#include "stdio.h"
#include "NIDAQmx.h"
#include "math.h"
#include "tdmwriter.h"
#include "fundtypes.h"
#include "platdefines.h"



#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void)
{
int32 error = 0;
TaskHandle taskHandle = 0;
int32 read;
float64 data[1000], Sum=0, Average, Variance, Deviation=0, std_dev, min=0, max=0, num=0, avg=0, minP=0, maxP=0, avgP=0, avgP2=0, minP2=0, maxP2=0, minV=0, maxV=0, avgV=0, Sum2 = 0;
char errBuff[2048] = { '\0' };
int i, count = 0;

/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("Pressure Voltage\n", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", 100.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000));

/*********************************************/
// DAQmx TDMS Configure Code
/*********************************************/
DAQmxErrChk(DAQmxConfigureLogging(taskHandle, "C:\\TestData\\LogFile.tdms", DAQmx_Val_LogAndRead, "ECS Test Data", DAQmx_Val_OpenOrCreate));
//DAQmxWriteAnalogF64(taskHandle,1000, 1 /*autoStart*/, -1 /*timeout*/, DAQmx_Val_GroupByScanNumber, data, 1000, NULL);

/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk(DAQmxStartTask(taskHandle));
printf("Voltage due to Pressure:\n");
printf("\n");
printf("Recording Data...\n");
DAQmxErrChk(DAQmxWaitUntilTaskDone(taskHandle, 10.0));


/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 1000, &read, NULL));

printf("Acquired %d points\n", (int)read);


/*********************************************/
//Display Values
/*********************************************/
printf("\n");
printf("values:\n");
printf("Voltage 1= %f\n",data[10]);
printf("Voltage 2= %f\n", data[100]);
printf("Voltage 3= %f\n", data[200]);
printf("Voltage 4= %f\n", data[300]);
printf("Voltage 5= %f\n", data[400]);
printf("Voltage 6= %f\n", data[500]);
printf("Voltage 7= %f\n", data[600]);
printf("Voltage 8= %f\n", data[700]);
printf("Voltage 9= %f\n", data[800]);
printf("Voltage 10= %f\n", data[900]);
printf("\n");

//for (int i = 1; i < 1000; i++) {
// printf("Voltage %i= %f\n",i, data[i]);
//}
//printf("\n");

/*********************************************/
//Average Values
/*********************************************/
for (i = 1; i < 999; ++i) {
Sum = Sum + data[i];
}

Average = (Sum / 999);
printf("Average= %f\n", Average);


/*********************************************/
//Standard Deviation
/*********************************************/
for (i = 1; i < 999; ++i) {
Deviation = Deviation + pow((Average - data[i]), 2);
}

Variance = Deviation / 999;
printf("Variance= %f\n", Variance);

std_dev = sqrt(Variance);
printf("Standard Deviation= %f\n", std_dev);

printf("\n");


/*********************************************/
//Min and Max Values
/*********************************************/

{
max = fmax(data[2], data[999]);
}
{
min = fmin(data[2], data[999]);
}

printf("Min: %f\n", min);
printf("Max: %f\n", max);
printf("Log File located in C:\\TestData. Please rename LogFile.tdms after testing\n");
printf("\n");


/*********************************************/
//Convert to Pressure Readings
/*********************************************/
printf("Pressure Readings (inches H2O):\n");
{
minP = (min - 2.9674) / .404;
maxP = (max - 2.9674) / .404;
avgP = (Average - 2.9674) / .404;
}
printf("Min Pressure: %f\n", minP);
printf("Max Pressure: %f\n", maxP);
printf("Average Pressure: %f\n", avgP);
printf("\n");


/*********************************************/
//New Voltage and Pressure Averages
/*********************************************/
{//target min and max pressure
minP2 = avgP - (avgP / 10);
maxP2 = avgP + (avgP / 10);
}
{//target min and max voltage
minV = (minP2*.404) + 2.9674;
maxV = (maxP2*.404) + 2.9674;
}
{//Sum of values in desired range
for (i = 1; i < 999; ++i) {
if (minV < data[i] && data[i] < maxV); { Sum2 = Sum2 + data[i]; }
}
}
{//Number of values in desired range
for (i = 1; i < 999; i++)
{
if (minV < data[i] && data[i] < maxV);
{
count++;
}
}
}
{//New average voltage
avgV = Sum2 / count;
}
{//New average pressure
avgP2 = ((avgV - 2.9674) / .404);
}
printf("Adjusted Values:\n");
printf("Min P2= %f\n", minP2);
printf("Max P2= %f\n", maxP2);
printf("Min Voltage= %f\n", minV);
printf("Max Voltage= %f\n", maxV);
printf("Sum Voltage= %f\n", Sum2);
printf("Count= %d\n", count);
printf("AvgV= %f\n", avgV);
printf("AvgP= %f\n", avgP2);


Error:
if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0)
{
/*********************************************/
// DAQmx Stop Code
/*********************************************/

DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}

if (DAQmxFailed(error))
printf("DAQmx Error: %s\n", errBuff);
printf("End of program, press Enter key to quit...\n");
getchar();
return 0;
}

最佳答案

C 是基于零的索引,因此您每次都会跳过第一个值,这可能会也可能不会给出错误的值。额外的括号导致您无法正确设置条件。我标记了不好的;带有评论,以便您可以看到它。

sum2 = 0; // not needed since initialized above
count = 0; // not needed since initialized above
for (i = 0; i < 999; ++i)
{
if ((minV < data[i]) && (data[i] < maxV)) // ; messed up and forced code to execute
// Extra parens are just for readability
{
//Sum of values in desired range
Sum2 += data[i];
//Number of values in desired range
count++;
}
}

关于c - 访问外部生成的数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com