Skip to content

Commit

Permalink
Clean up getTimeStampedData(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianFranzen committed Nov 15, 2015
1 parent 9fcedab commit e97fa36
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
64 changes: 34 additions & 30 deletions src/cerebustraceprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ bool CerebusTracesProvider::init() {

// Allocate live spike event buffer
mLiveClusterTime = new UINT32*[this->nbChannels];
mLiveClusterID = new UINT16*[this->nbChannels];
mLiveClusterID = new UINT8*[this->nbChannels];
mLiveClusterPosition = new size_t*[this->nbChannels];

for(int i = 0; i < this->nbChannels; i++) {
mLiveClusterTime[i] = new UINT32[mEventCapacity];
memset(mLiveClusterTime[i], 0, mEventCapacity * sizeof(UINT32));
mLiveClusterID[i] = new UINT16[mEventCapacity];
memset(mLiveClusterID[i], 0, mEventCapacity * sizeof(UINT16));
mLiveClusterID[i] = new UINT8[mEventCapacity];
memset(mLiveClusterID[i], 0, mEventCapacity * sizeof(UINT8));
mLiveClusterPosition[i] = new size_t(0);
}

Expand Down Expand Up @@ -559,14 +559,14 @@ void CerebusTracesProvider::slotPagingStopped() {
mLiveTracePosition = new size_t(0);

mLiveClusterTime = new UINT32*[this->nbChannels];
mLiveClusterID = new UINT16*[this->nbChannels];
mLiveClusterID = new UINT8*[this->nbChannels];
mLiveClusterPosition = new size_t*[this->nbChannels];

for(int i = 0; i < this->nbChannels; i++) {
mLiveClusterTime[i] = new UINT32[mEventCapacity];
memset(mLiveClusterTime[i], 0, mEventCapacity * sizeof(UINT32));
mLiveClusterID[i] = new UINT16[mEventCapacity];
memset(mLiveClusterID[i], 0, mEventCapacity * sizeof(UINT16));
mLiveClusterID[i] = new UINT8[mEventCapacity];
memset(mLiveClusterID[i], 0, mEventCapacity * sizeof(UINT8));
mLiveClusterPosition[i] = new size_t(0);
}

Expand Down Expand Up @@ -673,11 +673,11 @@ QList<ClustersProvider*> CerebusTracesProvider::getClusterProviders() {
}

Array<dataType>* CerebusTracesProvider::getClusterData(unsigned int channel, long start, long end) {
return getTimeStampedData(mViewClusterTime[channel],
mViewClusterID[channel],
mViewClusterPosition[channel],
start,
end);
return getTimeStampedData<UINT8>(mViewClusterTime[channel],
mViewClusterID[channel],
mViewClusterPosition[channel],
start,
end);
}

EventsProvider* CerebusTracesProvider::getEventProvider() {
Expand All @@ -688,16 +688,17 @@ EventsProvider* CerebusTracesProvider::getEventProvider() {
}

Array<dataType>* CerebusTracesProvider::getEventData(long start, long end) {
return getTimeStampedData(mViewEventTime,
mViewEventID,
mViewEventPosition,
start,
end);
return getTimeStampedData<UINT16>(mViewEventTime,
mViewEventID,
mViewEventPosition,
start,
end);
}

Array<dataType>* CerebusTracesProvider::getTimeStampedData(UINT32* time,
UINT16* id,
size_t* position,
template <typename T>
Array<dataType>* CerebusTracesProvider::getTimeStampedData(UINT32* timeBuffer,
T* dataBuffer,
size_t* bufferPosition,
long start,
long end) {
// The arrays assignment operator is broken, so returning a pointer is a quick fix.
Expand Down Expand Up @@ -730,21 +731,24 @@ Array<dataType>* CerebusTracesProvider::getTimeStampedData(UINT32* time,
// In the beginning or on clock overflow make sure we stay positive.
if (startInRecordingUnits < 0)
startInRecordingUnits = 0;
if (endInRecordingUnits < 0)
endInRecordingUnits = 0;

size_t endIndex = (*position);
// Linear search for start and end index.
size_t endIndex = (*bufferPosition);
do {
if(endIndex == 0) endIndex = mEventCapacity;
endIndex--;
} while(endIndex != (*position) &&
time[endIndex] > endInRecordingUnits);
} while(endIndex != (*bufferPosition) &&
timeBuffer[endIndex] > endInRecordingUnits);
endIndex++;

size_t startIndex = endIndex;
do {
if(startIndex == 0) startIndex = mEventCapacity;
startIndex--;
} while(startIndex != (*position) &&
time[startIndex] > startInRecordingUnits);
} while(startIndex != (*bufferPosition) &&
timeBuffer[startIndex] >= startInRecordingUnits);
startIndex++;

// Count spike event and wrapp around if needed.
Expand All @@ -765,26 +769,26 @@ Array<dataType>* CerebusTracesProvider::getTimeStampedData(UINT32* time,
if(startIndex <= endIndex) {
// Adjust and copy timestamps
for(size_t i = startIndex; i < endIndex; i++) {
(*result)[dataIndex++] = (time[i] - startInRecordingUnits) / clockToSampleRatio;
(*result)[dataIndex++] = (timeBuffer[i] - startInRecordingUnits) / clockToSampleRatio;
}
// Copy cluster ids
for(size_t i = startIndex; i < endIndex; i++) {
(*result)[dataIndex++] = id[i];
(*result)[dataIndex++] = dataBuffer[i];
}
} else {
// Adjust and copy timestamps
for(size_t i = startIndex; i < mEventCapacity; i++) {
(*result)[dataIndex++] = (time[i] - startInRecordingUnits) / clockToSampleRatio;
(*result)[dataIndex++] = (timeBuffer[i] - startInRecordingUnits) / clockToSampleRatio;
}
for(size_t i = 0; i < endIndex; i++) {
(*result)[dataIndex++] = (time[i] - startInRecordingUnits) / clockToSampleRatio;
(*result)[dataIndex++] = (timeBuffer[i] - startInRecordingUnits) / clockToSampleRatio;
}
// Copy cluster ids
for(size_t i = startIndex; i < mEventCapacity; i++) {
(*result)[dataIndex++] = id[i];
(*result)[dataIndex++] = dataBuffer[i];
}
for(size_t i = 0; i < endIndex; i++) {
(*result)[dataIndex++] = id[i];
(*result)[dataIndex++] = dataBuffer[i];
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/cerebustraceprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ class CerebusTracesProvider : public TracesProvider {

// Spike event data storage
UINT32** mLiveClusterTime;
UINT16** mLiveClusterID; //UINT8
UINT8** mLiveClusterID;
size_t** mLiveClusterPosition;
UINT32** mViewClusterTime;
UINT16** mViewClusterID; //UINT8
UINT8** mViewClusterID;
size_t** mViewClusterPosition;

// Digital and serial event data storage
Expand All @@ -241,9 +241,10 @@ class CerebusTracesProvider : public TracesProvider {
virtual void computeRecordingLength();

/** Helper function that searches timestamps in buffer*/
Array<dataType>* getTimeStampedData(UINT32* time,
UINT16* id,
size_t* position,
template <typename T>
Array<dataType>* getTimeStampedData(UINT32* timeBuffer,
T* dataBuffer,
size_t* bufferPosition,
long start,
long end);
};
Expand Down

0 comments on commit e97fa36

Please sign in to comment.