Created
November 13, 2020 21:13
-
-
Save sixlettervariables/39a8742f808cd599b44149c62430ac8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Quick and Dirty way to read ELI 280 XML Files | |
// [email protected] | |
// | |
// .NET 5.0 / C# 8.0 | |
// | |
using System; | |
using System.IO; | |
using System.Xml.Linq; | |
namespace Eli280EcgReader | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
XDocument xdoc = XDocument.Load(args[0]); | |
XElement xroot = xdoc.Root; | |
ReadTypicalCycles(xroot); | |
ReadChannels(xroot); | |
} | |
private static void ReadTypicalCycles(XElement xroot) | |
{ | |
using StreamWriter writer = File.CreateText("typicalCycles.csv"); | |
writer.WriteLine("Channel\tSample\tTime\tUnits\tMV"); | |
XElement typicalCycles = xroot.Element("TYPICAL_CYCLE"); | |
if (typicalCycles != null) | |
{ | |
Console.WriteLine("Reading typical cycles..."); | |
double unitsPerMV = (int)typicalCycles.Attribute("UNITS_PER_MV"); | |
int samples = (int)typicalCycles.Attribute("DURATION"); | |
double sampleFreq = (int)typicalCycles.Attribute("SAMPLE_FREQ"); | |
foreach (XElement typicalCycle in typicalCycles.Elements("TYPICAL_CYCLE_CHANNEL")) | |
{ | |
string name = (string)typicalCycle.Attribute("NAME"); | |
byte[] data = Convert.FromBase64String((string)typicalCycle.Attribute("DATA")); | |
Console.WriteLine($"\tReading Channel '{name}'..."); | |
using BinaryReader reader = new BinaryReader(new MemoryStream(data)); | |
for (int ss = 0; ss < samples; ++ss) | |
{ | |
short sample = reader.ReadInt16(); | |
writer.WriteLine($"{name}\t{ss}\t{ss/sampleFreq}\t{sample}\t{sample/unitsPerMV}"); | |
} | |
} | |
} | |
} | |
private static void ReadChannels(XElement xroot) | |
{ | |
using StreamWriter writer = File.CreateText("channels.csv"); | |
writer.WriteLine("Channel\tSample\tTime\tUnits\tMV"); | |
Console.WriteLine("Reading channels..."); | |
foreach (XElement channel in xroot.Elements("CHANNEL")) | |
{ | |
double unitsPerMV = (int)channel.Attribute("UNITS_PER_MV"); | |
int samples = (int)channel.Attribute("DURATION"); | |
double sampleFreq = (int)channel.Attribute("SAMPLE_FREQ"); | |
string name = (string)channel.Attribute("NAME"); | |
byte[] data = Convert.FromBase64String((string)channel.Attribute("DATA")); | |
Console.WriteLine($"\tReading Channel '{name}'..."); | |
using BinaryReader reader = new BinaryReader(new MemoryStream(data)); | |
for (int ss = 0; ss < samples; ++ss) | |
{ | |
short sample = reader.ReadInt16(); | |
writer.WriteLine($"{name}\t{ss}\t{ss/sampleFreq}\t{sample}\t{sample/unitsPerMV}"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment