예제 #1
0
        private static int _WriteSignal(ref byte[] data, Signals sigs, double[] avm)
        {
            if (sigs == null)
            {
                return(-1);
            }

            int rhythmStart, rhythmEnd;

            sigs.CalculateStartAndEnd(out rhythmStart, out rhythmEnd);

            int nrLeads   = sigs.NrLeads,
                sigs_size = rhythmEnd - rhythmStart,
                buff_size = sigs_size * nrLeads * SHORT_SIZE;

            if ((sigs_size <= 0) ||
                (avm == null) ||
                (avm.Length != nrLeads))
            {
                return(-1);
            }

            if ((data == null) ||
                (data.Length < buff_size))
            {
                data = new byte[buff_size];
            }

            for (int pos = 0; pos < buff_size; pos += SHORT_SIZE)
            {
                int pos2   = pos / SHORT_SIZE,
                    lead   = pos2 % nrLeads,
                    sample = (pos2 / nrLeads) + rhythmStart;

                short curr = LEAD_FAULT_VALUE;

                if ((sample >= sigs[lead].RhythmStart) &&
                    (sample < sigs[lead].RhythmEnd))
                {
                    sample -= sigs[lead].RhythmStart;

                    if ((sample >= 0) &&
                        (sample < sigs[lead].Rhythm.Length))
                    {
                        curr = sigs[lead].Rhythm[sample];
                    }
                }

                if (curr != LEAD_FAULT_VALUE)
                {
                    curr = (short)((curr * sigs.RhythmAVM) / avm[lead]);
                }

                BytesTool.writeBytes(curr, data, pos, SHORT_SIZE, true);
            }

            return(buff_size);
        }
예제 #2
0
        public int setSignals(Signals sigs)
        {
            if ((sigs != null) &&
                (sigs.NrLeads > 0) &&
                (sigs.NrLeads <= ISHNEHeader.MAX_NR_LEADS) &&
                (sigs.RhythmAVM > 0.0) &&
                (sigs.RhythmSamplesPerSecond > 0))
            {
                _Header.ECGNrLeads    = sigs.NrLeads;
                _Header.ECGSampleRate = (Int16)sigs.RhythmSamplesPerSecond;

                for (int i = 0; i < sigs.NrLeads; i++)
                {
                    _Header.SetLeadType(i, sigs[i].Type);
                    _Header.ECGLeadQuality[i]    = 0;
                    _Header.ECGLeadResolution[i] = (Int16)(sigs.RhythmAVM * ISHNEHeader.UV_TO_AMPLITUDE);
                }

                if (sigs.IsBuffered)
                {
                    BufferedSignals bs = sigs.AsBufferedSignals;

                    _Header.ECGNrSamples = (bs.RealRhythmEnd - bs.RealRhythmStart) * _Header.ECGNrLeads;
                }
                else
                {
                    int rhythmStart, rhythmEnd;

                    sigs.CalculateStartAndEnd(out rhythmStart, out rhythmEnd);

                    _Header.ECGNrSamples = (rhythmEnd - rhythmStart) * _Header.ECGNrLeads;
                }

                _Signals = sigs;

                return(0);
            }

            return(1);
        }
예제 #3
0
        public override int Write(Stream output)
        {
            try
            {
                if ((output != null) &&
                    output.CanWrite)
                {
                    if (Works())
                    {
                        _Header.VarBlockOffset = _Header.Size() + BYTES_BEFORE_HEADER;
                        _Header.VarBlockSize   = (_HeaderAndVarBlock != null) && (_HeaderAndVarBlock.Length < _Header.VarBlockOffset) ? _HeaderAndVarBlock.Length - _Header.VarBlockOffset : 0;
                        _Header.ECGOffset      = _Header.VarBlockOffset + _Header.VarBlockSize;

                        if ((_HeaderAndVarBlock == null) ||
                            (_HeaderAndVarBlock.Length < _Header.ECGOffset))
                        {
                            _HeaderAndVarBlock = new byte[_Header.ECGOffset];
                        }

                        // Begin: code that handles overriding of AVM
                        double   avmOverride = _AVMOverride;
                        int      nrLeads     = _Signals.NrLeads;
                        double[] avm         = new double[nrLeads];

                        if (avmOverride <= 0.0)
                        {
                            for (int i = 0; i < nrLeads; i++)
                            {
                                avm[i] = _Header.GetLeadAmplitude(i);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < nrLeads; i++)
                            {
                                _Header.ECGLeadResolution[i] = (Int16)(avmOverride * ISHNEHeader.UV_TO_AMPLITUDE);
                                avm[i] = avmOverride;
                            }
                        }
                        // End: code that handles overriding of AVM

                        BytesTool.writeString(MAGIC_NUMBER, _HeaderAndVarBlock, 0, MAGIC_NUMBER.Length);

                        if (_Header.Write(_HeaderAndVarBlock, BYTES_BEFORE_HEADER, _Header.Size()) != 0)
                        {
                            return(0x4);
                        }

                        CRCTool tool = new CRCTool();
                        tool.Init(CRCTool.CRCCode.CRC_CCITT);

                        ushort crc = tool.CalcCRCITT(_HeaderAndVarBlock, BYTES_BEFORE_HEADER, _HeaderAndVarBlock.Length - BYTES_BEFORE_HEADER);

                        BytesTool.writeBytes(crc, _HeaderAndVarBlock, MAGIC_NUMBER.Length, SHORT_SIZE, true);

                        output.Write(_HeaderAndVarBlock, 0, _Header.ECGOffset);

                        if (_Signals.IsBuffered)
                        {
                            BufferedSignals bs = _Signals.AsBufferedSignals;

                            int rhythmCur = bs.RealRhythmStart,
                                rhythmEnd = bs.RealRhythmEnd,
                                stepSize  = 60 * bs.RhythmSamplesPerSecond;

                            byte[] data = null;

                            for (; rhythmCur < rhythmEnd; rhythmCur += stepSize)
                            {
                                if (!bs.LoadSignal(rhythmCur, rhythmCur + stepSize))
                                {
                                    return(0x8);
                                }

                                int buff_size = _WriteSignal(ref data, _Signals, avm);

                                if (buff_size < 0)
                                {
                                    return(0x10);
                                }

                                output.Write(data, 0, buff_size);
                            }
                        }
                        else
                        {
                            int rhythmStart, rhythmEnd;

                            _Signals.CalculateStartAndEnd(out rhythmStart, out rhythmEnd);

                            byte[] data = null;

                            int buff_size = _WriteSignal(ref data, _Signals, avm);

                            if (buff_size < 0)
                            {
                                return(0x10);
                            }

                            output.Write(data, 0, buff_size);
                        }

                        return(0x0);
                    }

                    return(0x2);
                }
            }
            catch
            {
                return(0x10);
            }

            return(0x1);
        }