Esempio n. 1
0
        /// <summary>
        /// Function to encode data in this section.
        /// </summary>
        /// <param name="data">Rhythm data to encode</param>
        /// <param name="tables">Huffman table to use during enconding</param>
        /// <param name="medianLength">contains length of median data in msec</param>
        /// <param name="difference">difference to use durring decoding</param>
        /// <returns>0 on succes</returns>
        public int EncodeData(short[][] data, SCPSection2 tables, ushort medianLength, byte difference)
        {
            if ((tables != null) &&
                (data != null))
            {
                ushort nrleads = (ushort)data.Length;
                _Data       = new byte[nrleads][];
                _DataLength = new ushort[nrleads];
                for (int loper = 0; loper < nrleads; loper++)
                {
                    if (data[loper] == null)
                    {
                        return(2);
                    }

                    _Difference  = difference;
                    _Data[loper] = tables.Encode(data[loper], medianLength, 0, _Difference);
                    if (_Data[loper] == null)
                    {
                        _Data       = null;
                        _DataLength = null;
                        return(4);
                    }
                    _DataLength[loper] = (ushort)_Data[loper].Length;
                    if ((_DataLength[loper] & 0x1) == 0x1)
                    {
                        _DataLength[loper]++;
                    }
                }
                return(0);
            }
            return(1);
        }
Esempio n. 2
0
        /// <summary>
        /// Function to encode data in this section.
        /// </summary>
        /// <param name="data">Rhythm data to encode</param>
        /// <param name="tables">Huffman table to use during enconding</param>
        /// <param name="leadDefinition">Lead Definitions to use for encoding</param>
        /// <param name="difference">difference to use durring decoding</param>
        /// <returns>0 on succes</returns>
        public int EncodeData(short[][] data, SCPSection2 tables, SCPSection3 leadDefinition, SCPSection4 qrsLocations, int medianFreq, byte difference)
        {
            int localFreq = getSamplesPerSecond();

            if ((data != null) &&
                (tables != null) &&
                (leadDefinition != null) &&
                (localFreq > 0))
            {
                if ((medianFreq <= 0) ||
                    (medianFreq == localFreq))
                {
                    medianFreq = 1;
                    localFreq  = 1;
                }

                if ((_Bimodal == 0x1) &&
                    (qrsLocations == null))
                {
                    return(2);
                }

                ushort nrleads = leadDefinition.getNrLeads();
                _Data       = new byte[nrleads][];
                _DataLength = new ushort[nrleads];
                for (int loper = 0; loper < nrleads; loper++)
                {
                    if (data[loper] == null)
                    {
                        return(4);
                    }

                    short[] temp = data[loper];

                    int time = (leadDefinition.getLeadLength(loper) * localFreq) / medianFreq;
                    if (localFreq != medianFreq)
                    {
                        int rate = (medianFreq / localFreq);
                        // Bimodal part might be buggy unable to test.
                        if ((_Bimodal == 0x1) &&
                            ((medianFreq % localFreq) == 0) &&
                            (rate > 0) &&
                            (rate < 5))
                        {
                            // Calculate nr of samples stored in section.
                            time = 0;

                            int nrzones = qrsLocations.getNrProtectedZones();
                            for (int zone = 0; zone < nrzones; zone++)
                            {
                                int begin = (qrsLocations.getProtectedStart(zone) >= leadDefinition.getLeadStart(loper) ? qrsLocations.getProtectedStart(zone) : leadDefinition.getLeadStart(loper));
                                int end   = (qrsLocations.getProtectedEnd(zone) <= leadDefinition.getLeadEnd(loper) ? qrsLocations.getProtectedEnd(zone) : leadDefinition.getLeadEnd(loper));

                                begin = (end > begin ? end - begin + 1 : 0);

                                time += begin + (rate - (begin % rate));
                            }

                            time += ((leadDefinition.getLeadLength(loper) - time) * localFreq) / medianFreq;

                            int leadLength = leadDefinition.getLeadLength(loper);

                            time += ((leadLength - time) * localFreq) / medianFreq;

                            // Restructure bimodal data to length set in Section3.
                            temp = new short[time];

                            int time2Offset = leadDefinition.getLeadStart(loper);
                            int time1       = 0;
                            int time2       = 0;

                            while ((time1 < temp.Length) &&
                                   (time2 <= leadLength) &&
                                   (time2 < data[loper].Length))
                            {
                                int zone = 0;
                                int end  = qrsLocations.getNrProtectedZones();
                                for (; zone < end; zone++)
                                {
                                    if ((qrsLocations.getProtectedLength(zone) > 0) &&
                                        (time2 + time2Offset >= qrsLocations.getProtectedStart(zone)) &&
                                        (time2 + time2Offset <= qrsLocations.getProtectedEnd(zone)))
                                    {
                                        break;
                                    }
                                }

                                if (zone < end)
                                {
                                    temp[time1] = data[loper][time2++];
                                }
                                else
                                {
                                    int Sum = 0;
                                    for (int sumLoper = 0; sumLoper < rate; sumLoper++)
                                    {
                                        Sum += data[loper][time2 + sumLoper];
                                    }
                                    temp[time1] = (short)(Sum / rate);
                                    time2      += rate;
                                }

                                time1++;
                            }
                        }
                        else
                        {
                            _Bimodal = 0;
                            ECGTool.ResampleLead(temp, medianFreq, localFreq, out temp);
                        }
                    }

                    _Difference  = difference;
                    _Data[loper] = tables.Encode(temp, time, 0, _Difference);
                    if (_Data[loper] == null)
                    {
                        _Data       = null;
                        _DataLength = null;
                        return(8);
                    }
                    _DataLength[loper] = (ushort)_Data[loper].Length;
                    if ((_DataLength[loper] & 0x1) == 0x1)
                    {
                        _DataLength[loper]++;
                    }
                }
                return(0);
            }
            return(1);
        }