【问题标题】:How could I pick out specific range of data from an array in C?如何从 C 中的数组中挑选出特定范围的数据?
【发布时间】:2020-12-17 17:27:49
【问题描述】:

我正在使用具有数据流模式的双通道 DAQ。

输出的数据会变成一个数组,但是我只想要一个特定范围的数据并进行计算和分析,否则太多的数据点会延迟系统并导致FIFO溢出。

C代码是否有类似Matlabarray(6000:7000)的功能?

这是我获取通道 1 和通道 2 数据的代码,我想从 ch1Buffer[i]ch2Buffer[i] 中选择特定范围的数据

uInt32 i, n;
uInt8* pu8Buffer = NULL;
int16* pi16Buffer = NULL;
int64 i64Sum = 0;
float max1 = 0;
float max2 = 0;
double Corrected = 0;
double AUC = 0;
int16* ch1Buffer = NULL;
int16* ch2Buffer = NULL;
double SumBufferData( void* pBuffer, uInt32 u32Size, uInt32 u32SampleBits )
{
    // In this routine we sum up all the samples in the buffer. This function 
    // should be replaced with the user's analysys function
    if ( 8 == u32SampleBits )
    {
        pu8Buffer = (uInt8 *)pBuffer;
        for (i = 0; i < u32Size; i++)
        {
            i64Sum += pu8Buffer[i];
        }
    }
    else
    {
        pi16Buffer = (int16 *)pBuffer;
        fftw_complex(hilbertedch2[N]);

        ch1Buffer       = (int16*)calloc(u32Size/2, sizeof(int16));
        ch2Buffer       = (int16*)calloc(u32Size/2, sizeof(int16));

        // Divide ch1 and ch2 data from pi16Buffer
        for (i = 0; i < u32Size/2; i++)
        {
            ch1Buffer[i] += pi16Buffer[i*2];
            ch2Buffer[i] += pi16Buffer[i*2 + 1];
        }
        // Here hilbert on the whole ch2
        hilbert(ch2Buffer, hilbertedch2);

        //Find max value in each segs of ch1 and ch2
        for (i = 0; i < u32Size/2; i++)
        {
            if (ch1Buffer[i] > max1)
                max1 = ch1Buffer[i];

            if (abs(hilbertedch2[i][IMAG])> max2)
                max2 = abs(hilbertedch2[i][IMAG]);
        }
        Corrected = (max2 / max1); // Calculate the signal correction
    }
    free(ch1Buffer);
    free(ch2Buffer);
    return Corrected;
}

【问题讨论】:

  • 您可以使用memcpy,您可以在其中提及您的源地址作为要复制数据的起始地址,而不是范围,您可以提及需要从中复制的字节数起始地址,您需要提供目标地址来存储此数据。并查看手册页以查看限制(源和目标不应重叠)
  • 谢谢,但它看起来像 Linux。我用的是 C 合适吗?
  • for(i = 6000; i &lt; 7000; i++) { do_whatever_with array[i] } 呢?
  • 这是一个C 函数
  • @Davide 看起来最简单直接的,我试试看谢谢!

标签: arrays c sorting date-range


【解决方案1】:

C代码有没有类似Matlab array(6000:7000)的函数?

是的,它叫做memcpy。但是作为 C 语言,它更难使用,并且不知道数据的结构和大小。

int source_array[500]; 
// assume array gets filled somehow

int dest_array[50];
// dest_array = source_array[100:150] (not C code)
memcpy(dest_array, &source_array[100], 50*sizeof(source_array[0]);

由于memcpy 是高度优化的,它每次都击败一个 for 循环。

【讨论】:

  • 嗨,如果我理解清楚,我将构建一个名为 ch2newBuffer 的新数组,以获取我感兴趣的值,从 ch2Buffermemcpy 写作 6000 到 7000 范围格式会变成memcpy(ch2newBuffer, &amp;ch2Buffer[6000], 1000*sizeof(ch2Buffer[0])) 对吗?
  • @kevin,是的。除非我完全误解了 MatLab 语法的作用。
  • 谢谢!顺便说一句,我在打印答案时遇到了问题。我写道:for (i = 0; i &lt; 2; i++) { printf("a[%d] = %f\n", i, *ch2newBuffer); ch2newBuffer++; 我只从ch2Buffer 中挑选出两个值,想检查它们是否是我想要的。如何修改此打印输出功能?
  • Exception thrown at 0x00007FFF4C2A1399 (vcruntime140d.dll) in Examplefordebug.exe: 0xC0000005: Access violation writing location 0x0000000000000000.
  • 问题解决了,但我还是给你功劳,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2015-01-07
  • 1970-01-01
相关资源
最近更新 更多