首页 > 解决方案 > How could I pick out specific range of data from an array in C?

问题描述

I am using a dual channel DAQ with data streaming mode.

The output data will become an array, however, I just want a specific range of the data from it and do the calculation and analysis, otherwise too much data points will retard the system and cause the FIFO overflow.

Does C code have a similar function like Matlab array(6000:7000)?

Here is my code to get the channel 1 and channel 2 data and I want to select a specific range of data from the ch1Buffer[i] and 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;
}

标签: arrayscsortingdate-range

解决方案


Does C code have a similar function like Matlab array(6000:7000)?

Yes, it's called memcpy. But being C, it is harder to use and it is unaware of the structure and size of your data.

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]);

And as memcpyis highly optimized, it beats a for loop every time.


推荐阅读