arrays - 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;
}
解决方案
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 memcpy
is highly optimized, it beats a for loop every time.
推荐阅读
- alexa - Alexa 模拟器“挂起”
- javascript - 动态创建同一组件的多个实例。
- android - Firebase SHA1-Certificate 已被另一个应用程序使用
- jupyter-notebook - 修改 Jupyter 主目录的问题
- uwp - 具有广泛文件系统访问权限的无效 XML 清单
- git - github权限被拒绝推送到我自己的仓库
- javascript - 为什么在我的页面上加载 Google 地图会调整页面上使用 Roboto 字体的所有内容的大小?
- mysql - MySQL从列为空的表中删除
- java - 将透明图像绘制成 BufferedImage
- javascript - (nodejs, javascript) 我将如何使用变量在 JSON 文件中查找对象?