简体   繁体   English

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

[英]Access values in an externally generated array

I am taking voltage readings and converting them into pressure readings. 我正在获取电压读数并将其转换为压力读数。 This is my first real experience with C so my code is messy but so far has worked fine. 这是我第一次真正的C经验,所以我的代码很混乱,但到目前为止效果很好。 The problem I am facing is making the program count the number of readings (stored in the array data[i]) that fall between a min and max desired value. 我面临的问题是使程序对介于最小和最大期望值之间的读数(存储在数组data [i]中)进行计数。

Here is the code, Sum2 and count are the areas giving me trouble. 这是代码,Sum2和count是给我麻烦的地方。 Sum2 is adding 999 values instead of filtering and count is always resulting in 998 when it should be closer to 500 Sum2会添加999个值而不是进行过滤,并且count总是会导致998,而应该接近500

EDIT: 编辑:

the readings in data[i] are voltage and I am working with pressure. data [i]中的读数是电压,我正在压力下工作。 My calibration curve is P=(V-2.9674)/.404 我的校准曲线是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 is zero based index so you have been skipping the first value every time, which may or may not give a bad value. C是从零开始的索引,因此您每次都跳过第一个值,这可能会或可能不会产生错误的值。 The extra brackets caused you to miss setting up the conditional correctly. 多余的括号使您无法正确设置条件。 I marked the bad ; 我标出了不好; with a comment so you can see it. 与评论,以便您可以看到它。

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++;
    }
}

You have an erroneous ; 你有一个错误; after the following lines 在以下几行之后

if (minV < data[i] && data[i] < maxV);

and

if (minV < data[i] && data[i] < maxV);

These cause the following code block to be always executed, since the ; 由于; ,这些将始终执行以下代码块; creates a "do-nothing" code block. 创建一个“不做任何事情”的代码块。

EDIT: are you also doing the wrong calculation for minV and maxV ? 编辑:你是不是也做了错误的计算minVmaxV What do they have to do with pressure? 他们与压力有什么关系?

minV = (minP2*.404) + 2.9674;
maxV = (maxP2*.404) + 2.9674;

Perhaps that puts the data out of their range, since the loop fails to catch any values. 也许这使数据超出了它们的范围,因为循环无法捕获任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问结构数组中的值 - How to access values in a struct array 指针访问和数组访问之间的值差异 - Difference in values between pointer access and array access 在if语句中初始化int数组并在外部使用 - Initialising an int array in an if statement and using externally 来自数组指针的正确语法访问值? - proper syntax access values from an array pointer? C - scanf 无需直接访问即可更改数组的值 - C - scanf changes the values ​of an array without direct access 如何使用Cgo访问MATLAB数组中的值? - How do I access the values inside a MATLAB array using Cgo? 如何在同一过程中共享对文件的共享读/写访问权限,但在外部禁止共享? - How can I share share read/write access to a file within the same process, but forbid it externally? 我想访问指针数组的值并对其求和,但我得到了错误的值 - I want to access and sum the values of a pointer array, but I get the wrong values 如果结构值位于 c 中另一个结构数组的结构数组中,您将如何访问它们? - How would you access struct values if they're in a struct array in another struct array in c? 如何访问存储在 C 结构内的数组中的值,该结构通过 ctypes 返回到 Python? - How do I access values stored in an array inside a C struct returned to Python via ctypes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM