-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSxHeader.cpp
54 lines (43 loc) · 2.18 KB
/
NSxHeader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "NSxHeader.h"
NSxHeader::NSxHeader(std::ifstream& file) {
// All NSx files start with the magic word "NEURALCD".
// Check to make sure this one does too
std::streampos start= file.tellg();
try {
std::string typeString = "NEURALCD";
for(auto i = 0U; i<typeString.size(); i++) {
char tmp;
file >> tmp;
if(tmp != typeString[i]) {
throw(std::runtime_error("File is not a 'NEURALCD' file; expecting a Ripple .ns5 or similar"));
}
}
// See page 8 of the Trellis NEV spec for sizes
file.read(reinterpret_cast<char*>(&majorVersion), sizeof(majorVersion));
file.read(reinterpret_cast<char*>(&minorVersion), sizeof(minorVersion));
file.read(reinterpret_cast<char*>(&offset), sizeof(offset));
file.read(reinterpret_cast<char*>(&label), sizeof(char)*16);
file.read(reinterpret_cast<char*>(&comment), sizeof(char)*256);
file.read(reinterpret_cast<char*>(&samplingPeriod), sizeof(samplingPeriod));
file.read(reinterpret_cast<char*>(&timeResolution), sizeof(timeResolution));
file.read(reinterpret_cast<char*>(&time), sizeof(time));
file.read(reinterpret_cast<char*>(&channelCount), sizeof(channelCount));
} catch (...) {
file.seekg(start); //Rewind the file, as if this never happened
throw;
}
/* We explicitly DO NOT CLOSE the file here, because the rest of the class
is going to continue reading from the file */
}
std::ostream& operator<<(std::ostream& out, const NSxHeader& h) // output
{
out << "NSx Header (file format " << int(h.majorVersion) << '.' << int(h.minorVersion) << ')' << std::endl;
out << "Data collection began at " << h.time << std::endl;
out << "Contents: " << h.label << ", beginning at offset " << int(h.offset) << std::endl;
out << h.channelCount << " channels, sampled at " << h.getSamplingFreq() << "Hz. Time resolution " << h.timeResolution << " Hz." << std::endl;
out << "Comments: " << h.comment << std::endl;
return out;
}
double NSxHeader::getSamplingFreq() const {
return double(timeResolution)/double(samplingPeriod);
}