Skip to content

Commit

Permalink
<# bug fixes in variant adapatation #>
Browse files Browse the repository at this point in the history
  • Loading branch information
melissakey committed Nov 8, 2024
1 parent 963d53c commit 49d379c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 70 deletions.
133 changes: 80 additions & 53 deletions xdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,41 +178,91 @@ int Xdf::load_xdf(std::string filename)
//read [NumSampleBytes], [NumSamples]
uint64_t numSamp = readLength(file);

//if the time series is empty
if (streams[index].time_series.empty())
streams[index].time_series.resize(streams[index].info.channel_count);

auto tsBytes = readBin<uint8_t>(file);
double lastTimestamp = streams[index].stream.last_timestamp;

for (size_t i = 0; i < numSamp; i++) {
double ts = readTimestamp(file, lastTimestamp, streams[index].stream.sampling_interval);
streams[index].time_stamps.emplace_back(ts);

// Based on the channel format, read and store the data
if(streams[index].info.channel_format.compare("float32")) {
readData<float>(file, streams[index].stream.time_series, streams[index].stream.info.channel_count);
} else if(streams[index].info.channel_format.compare("double64")) {
readData<double>(file, streams[index].stream.time_series, streams[index].stream.info.channel_count);
} else if(streams[index].info.channel_format.compare("int8")) {
readData<int8_t>(file, streams[index].stream.time_series, streams[index].stream.info.channel_count);
} else if(streams[index].info.channel_format.compare("int16")) {
readData<int16_t>(file, streams[index].stream.time_series, streams[index].stream.info.channel_count);
} else if(streams[index].info.channel_format.compare("int32")) {
readData<int32_t>(file, streams[index].stream.time_series, streams[index].stream.info.channel_count);
} else if(streams[index].info.channel_format.compare("int64")) {
readData<int64_t>(file, streams[index].stream.time_series, streams[index].stream.info.channel_count);
} else if(streams[index].info.channel_format.compare("string")) {
auto length = Xdf::readLength(file);
//if the time series is empty
if (streams[index].time_series.empty())
streams[index].time_series.resize(streams[index].info.channel_count);

//for each sample
for (size_t i = 0; i < numSamp; i++)
{
//read or deduce time stamp
auto tsBytes = readBin<uint8_t>(file);

double ts; //temporary time stamp

if (tsBytes == 8)
{
Xdf::readBin(file, &ts);
streams[index].time_stamps.emplace_back(ts);
}
else
{
ts = streams[index].last_timestamp + streams[index].sampling_interval;
streams[index].time_stamps.emplace_back(ts);
}

streams[index].last_timestamp = ts;

if(streams[index].info.channel_format.compare("string") == 0) {

std::vector<std::variant<int, float, double, int64_t, std::string>> data;

for(int v = 0; v < streams[index].info.channel_count; ++v) {
auto length = Xdf::readLength(file);
char* buffer = new char[length + 1];
file.read(buffer, length);
buffer[length] = '\0';

streams[index].time_series[v].emplace_back(buffer);
delete[] buffer;
}
}
}
} else {
//read the data
if(streams[index].info.channel_format.compare("float32") == 0) {
for (int v = 0; v < streams[index].info.channel_count; ++v)
{
float data;
Xdf::readBin(file, &data);
streams[index].time_series[v].emplace_back(data);
}
} else if(streams[index].info.channel_format.compare("double64") == 0) {
for (int v = 0; v < streams[index].info.channel_count; ++v)
{
double data;
Xdf::readBin(file, &data);
streams[index].time_series[v].emplace_back(data);
}
} else if(streams[index].info.channel_format.compare("int8_t") == 0) {
for (int v = 0; v < streams[index].info.channel_count; ++v)
{
int8_t data;
Xdf::readBin(file, &data);
streams[index].time_series[v].emplace_back(data);
}
} else if(streams[index].info.channel_format.compare("int16_t") == 0) {
for (int v = 0; v < streams[index].info.channel_count; ++v)
{
int16_t data;
Xdf::readBin(file, &data);
streams[index].time_series[v].emplace_back(data);
}
} else if(streams[index].info.channel_format.compare("int32_t") == 0) {
for (int v = 0; v < streams[index].info.channel_count; ++v)
{
int data;
Xdf::readBin(file, &data);
streams[index].time_series[v].emplace_back(data);
}
} else if(streams[index].info.channel_format.compare("int64_t") == 0) {
for (int v = 0; v < streams[index].info.channel_count; ++v)
{
int64_t data;
Xdf::readBin(file, &data);
streams[index].time_series[v].emplace_back(data);
}
}
}
}
}
break;
case 4: //read [ClockOffset] chunk
{
Expand Down Expand Up @@ -423,6 +473,7 @@ void Xdf::resample(int userSrate)
for (auto &stream : streams)
{
if (!stream.time_series.empty() &&
!stream.info.channel_format.compare("string") &&
stream.info.nominal_srate != userSrate &&
stream.info.nominal_srate != 0)
{
Expand Down Expand Up @@ -909,27 +960,3 @@ template<typename T> T Xdf::readBin(std::istream& is, T* obj) {
is.read(reinterpret_cast<char*>(obj), sizeof(T));
return *obj;
}

// Generic function to read data of any type
template<typename T>
void readData(std::ifstream &file, std::vector<std::vector<T>> &time_series, int channel_count) {
for (int v = 0; v < channel_count; ++v) {
T data;
Xdf::readBin(file, &data);
time_series[v].emplace_back(data);
}
}

double readTimestamp(std::ifstream &file, double &lastTimestamp, double samplingInterval) {
auto tsBytes = readBin<uint8_t>(file);
double ts;

if (tsBytes == 8) {
Xdf::readBin(file, &ts);
} else {
ts = lastTimestamp + samplingInterval;
}

lastTimestamp = ts;
return ts;
}
18 changes: 1 addition & 17 deletions xdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,7 @@ class Xdf
* \return the read data
*/
template<typename T> T readBin(std::istream& is, T* obj = nullptr);

/*
* \brief Template function to read data of any type and store it in the time_series container.
* \param file The input file stream to read the data from.
* \param time_series A reference to a 2D vector storing the time series data for each channel.
* \param channel_count The number of channels to read data for.
*/
template<typename T> void readData(std::ifstream &file, std::vector<std::vector<T>> &time_series, int channel_count);

/*!
* \brief Utility function to handle time stamp reading or deduction.
* \param file The input file stream from which the time stamp is read.
* \param lastTimestamp The last timestamp value to calculate the new timestamp if needed.
* \param samplingInterval The interval between samples, used for calculating the timestamp when necessary.
* \return The calculated or read timestamp.
*/
double readTimestamp(std::ifstream &file, double &lastTimestamp, double samplingInterval)

};

#endif // XDF_H

0 comments on commit 49d379c

Please sign in to comment.