我如何从 C 中的数组中挑选出特定范围的数据?
How could I pick out specific range of data from an array in C?
我正在使用带数据流模式的双通道 DAQ。
输出的数据会变成一个数组,但是我只是想从中取出特定范围的数据,然后进行计算分析,否则数据点太多会拖慢系统,导致FIFO溢出。
C
代码有没有类似Matlab的功能array(6000:7000)
?
这是我获取通道 1 和通道 2 数据的代码,我想 select 来自 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;
}
Does C code have a similar function like 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 循环。
我正在使用带数据流模式的双通道 DAQ。
输出的数据会变成一个数组,但是我只是想从中取出特定范围的数据,然后进行计算分析,否则数据点太多会拖慢系统,导致FIFO溢出。
C
代码有没有类似Matlab的功能array(6000:7000)
?
这是我获取通道 1 和通道 2 数据的代码,我想 select 来自 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;
}
Does C code have a similar function like 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 循环。