libxdf  0.92
A static C++ library for loading XDF files
xdf.h
Go to the documentation of this file.
1 //libxdf is a static C++ library to load XDF files
2 //Copyright (C) 2017 Yida Lin
3 
4 //This program is free software: you can redistribute it and/or modify
5 //it under the terms of the GNU General Public License as published by
6 //the Free Software Foundation, either version 3 of the License, or
7 //(at your option) any later version.
8 
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 //GNU General Public License for more details.
13 
14 //You should have received a copy of the GNU General Public License
15 //along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //If you have questions, contact author at ITAL_FAZIOLI@hotmail.com
17 
22 #ifndef XDF_H
23 #define XDF_H
24 
25 #include <string>
26 #include <vector>
27 #include <map>
28 #include <set>
29 
37 class Xdf
38 {
39 public:
41  Xdf();
42 
43  //subclass for single streams
53  struct Stream
54  {
56  std::vector<std::vector<float> > time_series;
57  std::vector<float> time_stamps;
58  std::string streamHeader;
59  std::string streamFooter;
61  struct
62  {
64  double nominal_srate;
65  std::string name;
66  std::string type;
67  std::string channel_format;
69  std::vector<std::map<std::string, std::string> > channels;
71  std::vector<std::pair<double, double> > clock_offsets;
73  double first_timestamp;
74  double last_timestamp;
76  double measured_srate;
78  } info;
80  float last_timestamp{ 0 };
82  std::vector<double> clock_times;
83  std::vector<double> clock_values;
84  };
85 
86  //XDF properties=================================================================================
87 
88  std::vector<Stream> streams;
89  float version;
91  uint64_t totalLen = 0;
94  float minTS = 0;
95  float maxTS = 0;
96  size_t totalCh = 0;
97  int majSR = 0;
98  int maxSR = 0;
99  std::vector<int> streamMap;
107  typedef std::string eventName;
113  typedef float eventTimeStamp;
114 
115  std::vector<std::pair<std::pair<eventName, eventTimeStamp>, int> > eventMap;
117  std::vector<std::string> dictionary;
118  std::vector<uint16_t> eventType;
119  std::vector<std::string> labels;
120  std::set<double> sampleRateMap;
121  std::vector<float> offsets;
123  std::string fileHeader;
124  int userAddedStream { 0 };
127  std::vector<std::pair<std::string, double> > userCreatedEvents;
129  //Public Functions==============================================================================================
130 
142  void adjustTotalLength();
143 
154  void calcTotalLength(int sampleRate);
155 
160  void createLabels();
161 
174  void detrend();
175 
186  void freeUpTimeStamps();
187 
193  int load_xdf(std::string filename);
194 
200  void resample(int userSrate);
201 
208  int writeEventsToXDF(std::string file_path);
209 
210  //Private Functions: Not intended to be used by external programs======================================
211 
212 private:
213 
217  void calcEffectiveSrate();
218 
229  void calcTotalChannel();
230 
251  void findMajSR();
252 
259  void findMinMax();
260 
267  void getHighestSampleRate();
268 
274  void loadDictionary();
275 
281  void loadSampleRateMap();
282 
292  uint64_t readLength(std::ifstream &file);
293 };
294 
295 #endif // XDF_H
std::string eventName
Used as std::vector<std::pair<std::pair<eventName, eventTimeStamp>, int> > in eventMap.
Definition: xdf.h:107
double measured_srate
Definition: xdf.h:76
std::vector< std::string > dictionary
Definition: xdf.h:117
double last_timestamp
Definition: xdf.h:74
std::vector< double > clock_values
Definition: xdf.h:83
void freeUpTimeStamps()
Delete the time stamps vectors when no longer needed to release some memory.
Definition: xdf.cpp:760
std::vector< std::pair< std::string, double > > userCreatedEvents
Definition: xdf.h:127
int userAddedStream
Definition: xdf.h:124
int majSR
Definition: xdf.h:97
size_t totalCh
Definition: xdf.h:96
float minTS
Definition: xdf.h:94
std::string streamFooter
Definition: xdf.h:59
std::vector< uint16_t > eventType
Definition: xdf.h:118
std::vector< double > clock_times
Definition: xdf.h:82
Definition: xdf.h:53
float maxTS
Definition: xdf.h:95
void detrend()
Subtract the entire channel by its mean.
Definition: xdf.cpp:803
std::vector< std::vector< float > > time_series
A 2D vector which stores the time series of a stream. Each row represents a channel.
Definition: xdf.h:56
std::string type
Definition: xdf.h:66
std::vector< Stream > streams
Definition: xdf.h:88
void adjustTotalLength()
Adjust totalLen to avoid possible deviation.
Definition: xdf.cpp:776
std::string name
Definition: xdf.h:65
int writeEventsToXDF(std::string file_path)
writeEventsToXDF
Definition: xdf.cpp:851
std::vector< std::pair< std::pair< eventName, eventTimeStamp >, int > > eventMap
Definition: xdf.h:115
float eventTimeStamp
Used as std::vector<std::pair<std::pair<eventName, eventTimeStamp>, int> > in eventMap.
Definition: xdf.h:113
Definition: xdf.h:37
void createLabels()
Create labels for each channel and store them in labels vector.
Definition: xdf.cpp:940
void resample(int userSrate)
Resample all streams and channel to a chosen sample rate.
Definition: xdf.cpp:537
int channel_count
Definition: xdf.h:63
Xdf()
Default constructor with no parameter.
Definition: xdf.cpp:30
std::vector< std::string > labels
Definition: xdf.h:119
int maxSR
Definition: xdf.h:98
std::string channel_format
Definition: xdf.h:67
double first_timestamp
Definition: xdf.h:73
struct Xdf::Stream::@0 info
std::set< double > sampleRateMap
Definition: xdf.h:120
std::vector< int > streamMap
Definition: xdf.h:99
float sampling_interval
Definition: xdf.h:81
float version
Definition: xdf.h:89
std::vector< float > offsets
Definition: xdf.h:121
double effective_sample_rate
Definition: xdf.h:77
uint64_t totalLen
Definition: xdf.h:91
void calcTotalLength(int sampleRate)
Calculate the globle length (in samples).
Definition: xdf.cpp:755
int load_xdf(std::string filename)
The main function of loading an XDF file.
Definition: xdf.cpp:34
int sample_count
Definition: xdf.h:75
std::string fileHeader
Definition: xdf.h:123
std::vector< std::pair< double, double > > clock_offsets
Definition: xdf.h:71
std::string streamHeader
Definition: xdf.h:58
std::vector< std::map< std::string, std::string > > channels
Definition: xdf.h:69
std::vector< float > time_stamps
Definition: xdf.h:57
double nominal_srate
Definition: xdf.h:64