0

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?

dbc
  • 104,963
  • 20
  • 228
  • 340
javirs
  • 1,049
  • 26
  • 52
  • There's not enough information for us to answer. If you're recording in a different thread, maybe you have a threading bug and the `Package` isn't fully populated? Your write method doesn't check for bad data and throw an exception or log an error, so you have no way of finding out. I suggest switching to a file format that is more self-documenting in which array sizes are *stored* not hardcoded in the reader. E.g. store sequential `float` arrays as root objects using `BinaryFormatter` as follows: http://mikehadlow.blogspot.com/2007/07/serializing-lots-of-different-objects.html – dbc Oct 05 '15 at 20:19
  • There is no sample missing in the bad dataset. Just rows swapped. The file cannot be any fancier because it hast to be opened from MATLAB afterwards. – javirs Oct 06 '15 at 06:33
  • If the the columns are sometimes swapped in a given row (values from columns 4 and 9 swapped) then that must be some sort of bug in how the `Package` was filled in. Your `BinaryWriter` code should correctly write the floats in the order returned by your `Package`. – dbc Oct 06 '15 at 06:47

0 Answers0