I'm saving a matrix of data into a bin file, and SOME TIMES (that's the weird part) it swaps the last row and it appears as the 3rd row, or 4th row. Not consistent. The system runs in real time storing data from an amplifier. The amp send packages of samples, in one package there is 1 sample for each (9) channels, the 9th is special signals.
This is the basic structure:
using (FileStream fs = new FileStream (path, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
while(recording)
{
packages = readData();
foreach(Package package in packages) //packages of 9 floats
{
foreach(float sample in package)
{
binaryWriter.Write(sample)
}
}
}
}
}
And this is how I read the data back from the bin file.
using (FileStream fs = File.OpenRead(path))
{
int numElems = (int)(fs.Length / sizeof(float));
int numChannels = 9;
numElems = numElems / numChannels;
using (BinaryReader br = new BinaryReader(fs))
{
for (int col = 0; col < numElems; ++col)
{
data.Add(new List<float>());
for (float elem = 0; elem < numChannels; ++elem)
{
data[col].Add(br.ReadSingle());
}
}
}
}
And in there I find that sometimes, the special signals ara in chennel num 4 instead of the last 9th channel.
Any clue? Can it be that some missing sample makes the whole thing struggle and shift or ... something?