Beispiel #1
0
        /// <summary>
        /// Function to make a twelve leads signals object.
        /// </summary>
        /// <returns>returns twelve leads signals object or null</returns>
        public Signals CalculateTwelveLeads()
        {
            LeadType[] lt = new LeadType[] { LeadType.I, LeadType.II, LeadType.III,
                                             LeadType.aVR, LeadType.aVL, LeadType.aVF,
                                             LeadType.V1, LeadType.V2, LeadType.V3,
                                             LeadType.V4, LeadType.V5, LeadType.V6 };

            int nrSim = NrSimultaneosly();

            if (nrSim != _Lead.Length)
            {
                return(null);
            }

            Signal[] leads = null;

            if (nrSim == 12)
            {
                ArrayList pos_list = new ArrayList(lt);

                int       check_one = 0;
                ArrayList check_two = new ArrayList(lt);
                Signal[]  pos       = new Signal[12];

                for (int i = 0; i < nrSim; i++)
                {
                    if (_Lead[i].Type == lt[i])
                    {
                        check_one++;
                    }

                    int temp = check_two.IndexOf(_Lead[i].Type);
                    if (temp < 0)
                    {
                        return(null);
                    }

                    check_two.RemoveAt(temp);

                    pos[pos_list.IndexOf(_Lead[i].Type)] = _Lead[i];
                }

                if (check_one == 12)
                {
                    return(this);
                }

                if (check_two.Count == 0)
                {
                    for (int i = 0; i < pos.Length; i++)
                    {
                        if (pos[i] != null)
                        {
                            pos[i] = pos[i].Clone();
                        }
                    }

                    leads = pos;
                }
            }
            else
            {
                short[][]
                tempRhythm = null,
                tempMedian = null;

                Signal[] pos = new Signal[12];

                if (nrSim == 8)
                {
                    ArrayList pos_list = new ArrayList(lt);

                    ArrayList check = new ArrayList(
                        new LeadType[] { LeadType.I, LeadType.II,
                                         LeadType.V1, LeadType.V2, LeadType.V3,
                                         LeadType.V4, LeadType.V5, LeadType.V6 });

                    for (int i = 0; i < nrSim; i++)
                    {
                        int temp = check.IndexOf(_Lead[i].Type);
                        if (temp < 0)
                        {
                            return(null);
                        }

                        check.RemoveAt(temp);

                        pos[pos_list.IndexOf(_Lead[i].Type)] = _Lead[i];
                    }

                    if (check.Count == 0)
                    {
                        for (int i = 0; i < pos.Length; i++)
                        {
                            if (pos[i] != null)
                            {
                                pos[i] = pos[i].Clone();
                            }
                        }

                        tempRhythm    = new short[2][];
                        tempRhythm[0] = pos[0].Rhythm;
                        tempRhythm[1] = pos[1].Rhythm;

                        tempMedian    = new short[2][];
                        tempMedian[0] = pos[0].Median;
                        tempMedian[1] = pos[1].Median;
                    }
                }
                else if (nrSim == 9)
                {
                    ArrayList pos_list = new ArrayList(lt);

                    ArrayList check = new ArrayList(
                        new LeadType[] { LeadType.I, LeadType.II, LeadType.III,
                                         LeadType.V1, LeadType.V2, LeadType.V3,
                                         LeadType.V4, LeadType.V5, LeadType.V6 });

                    for (int i = 0; i < nrSim; i++)
                    {
                        int temp = check.IndexOf(_Lead[i].Type);
                        if (temp < 0)
                        {
                            return(null);
                        }

                        check.RemoveAt(temp);

                        pos[pos_list.IndexOf(_Lead[i].Type)] = _Lead[i];
                    }

                    if (check.Count == 0)
                    {
                        for (int i = 0; i < pos.Length; i++)
                        {
                            if (pos[i] != null)
                            {
                                pos[i] = pos[i].Clone();
                            }
                        }

                        tempRhythm    = new short[3][];
                        tempRhythm[0] = pos[0].Rhythm;
                        tempRhythm[1] = pos[1].Rhythm;
                        tempRhythm[2] = pos[2].Rhythm;

                        tempMedian    = new short[3][];
                        tempMedian[0] = pos[0].Median;
                        tempMedian[1] = pos[1].Median;
                        tempMedian[2] = pos[2].Median;
                    }
                }

                if ((tempRhythm != null) ||
                    (tempMedian != null))
                {
                    short[][] calcLeads;

                    if ((tempRhythm != null) &&
                        (tempRhythm[0] != null) &&
                        ECGTool.CalculateLeads(tempRhythm, tempRhythm[0].Length, out calcLeads) == 0)
                    {
                        for (int i = 0; i < calcLeads.Length; i++)
                        {
                            Signal sig = new Signal();
                            sig.Type        = lt[i + tempRhythm.Length];
                            sig.RhythmStart = pos[0].RhythmStart;
                            sig.RhythmEnd   = pos[0].RhythmEnd;
                            sig.Rhythm      = calcLeads[i];

                            pos[i + tempRhythm.Length] = sig;
                        }

                        if ((tempMedian != null) &&
                            (tempMedian[0] != null) &&
                            (ECGTool.CalculateLeads(tempMedian, tempMedian[0].Length, out calcLeads) == 0))
                        {
                            for (int i = 0; i < calcLeads.Length; i++)
                            {
                                pos[i + tempRhythm.Length].Median = calcLeads[i];
                            }
                        }

                        leads = pos;
                    }
                }
            }

            if (leads != null)
            {
                Signals sigs = this.Clone();

                sigs.NrLeads = (byte)leads.Length;

                for (int i = 0; i < leads.Length; i++)
                {
                    sigs._Lead[i] = leads[i];
                }

                return(sigs);
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Function to make a fifteen leads signals object.
        /// </summary>
        /// <returns>returns fifteen leads signals object or null</returns>
        public Signals CalculateFifteenLeads()
        {
            LeadType[] lt1 = new LeadType[] { LeadType.I, LeadType.II, LeadType.III,
                                              LeadType.aVR, LeadType.aVL, LeadType.aVF,
                                              LeadType.V1, LeadType.V2, LeadType.V3,
                                              LeadType.V4, LeadType.V5, LeadType.V6,
                                              LeadType.V3R, LeadType.V4R, LeadType.V7 };
            LeadType[] lt2 = new LeadType[] { LeadType.I, LeadType.II, LeadType.III,
                                              LeadType.aVR, LeadType.aVL, LeadType.aVF,
                                              LeadType.V1, LeadType.V2, LeadType.V3,
                                              LeadType.V4, LeadType.V5, LeadType.V6,
                                              LeadType.V7, LeadType.V8, LeadType.V9 };

            int nrSim = NrSimultaneosly();

            if (nrSim != _Lead.Length)
            {
                return(null);
            }

            Signal[] leads = null;

            if (nrSim == lt1.Length)
            {
                ArrayList pos_list1 = new ArrayList(lt1);
                ArrayList pos_list2 = new ArrayList(lt2);

                int       check_one1 = 0;
                int       check_one2 = 0;
                ArrayList check_two1 = new ArrayList(lt1);
                ArrayList check_two2 = new ArrayList(lt2);
                Signal[]  pos1       = new Signal[lt1.Length];
                Signal[]  pos2       = new Signal[lt2.Length];

                for (int i = 0; i < nrSim; i++)
                {
                    if (_Lead[i].Type == lt1[i])
                    {
                        check_one1++;
                    }

                    if (_Lead[i].Type == lt2[i])
                    {
                        check_one2++;
                    }

                    int temp = check_two1.IndexOf(_Lead[i].Type);
                    if (temp >= 0)
                    {
                        check_two1.RemoveAt(temp);

                        pos1[pos_list1.IndexOf(_Lead[i].Type)] = _Lead[i];
                    }
                    temp = check_two2.IndexOf(_Lead[i].Type);
                    if (temp >= 0)
                    {
                        check_two2.RemoveAt(temp);

                        pos2[pos_list2.IndexOf(_Lead[i].Type)] = _Lead[i];
                    }
                }

                if (check_one1 == lt1.Length)
                {
                    return(this);
                }
                if (check_one2 == lt2.Length)
                {
                    return(this);
                }

                if (check_two1.Count == 0)
                {
                    for (int i = 0; i < pos1.Length; i++)
                    {
                        if (pos1[i] != null)
                        {
                            pos1[i] = pos1[i].Clone();
                        }
                    }

                    leads = pos1;
                }
                else if (check_two2.Count == 0)
                {
                    for (int i = 0; i < pos2.Length; i++)
                    {
                        if (pos2[i] != null)
                        {
                            pos2[i] = pos2[i].Clone();
                        }
                    }

                    leads = pos2;
                }
            }
            else
            {
                LeadType[] lt = null;

                short[][]
                tempRhythm = null,
                tempMedian = null;

                Signal[] pos = null;

                if (nrSim == 11)
                {
                    Signal[] pos1 = new Signal[lt1.Length];
                    Signal[] pos2 = new Signal[lt2.Length];

                    ArrayList pos_list1 = new ArrayList(lt1);
                    ArrayList pos_list2 = new ArrayList(lt2);

                    ArrayList check1 = new ArrayList(
                        new LeadType[] { LeadType.I, LeadType.II,
                                         LeadType.V1, LeadType.V2, LeadType.V3,
                                         LeadType.V4, LeadType.V5, LeadType.V6,
                                         LeadType.V7, LeadType.V3R, LeadType.V4R });

                    ArrayList check2 = new ArrayList(
                        new LeadType[] { LeadType.I, LeadType.II,
                                         LeadType.V1, LeadType.V2, LeadType.V3,
                                         LeadType.V4, LeadType.V5, LeadType.V6,
                                         LeadType.V7, LeadType.V8, LeadType.V9 });

                    for (int i = 0; i < nrSim; i++)
                    {
                        int temp = check1.IndexOf(_Lead[i].Type);
                        if (temp >= 0)
                        {
                            check1.RemoveAt(temp);

                            pos1[pos_list1.IndexOf(_Lead[i].Type)] = _Lead[i];
                        }
                        temp = check2.IndexOf(_Lead[i].Type);
                        if (temp >= 0)
                        {
                            check2.RemoveAt(temp);

                            pos2[pos_list2.IndexOf(_Lead[i].Type)] = _Lead[i];
                        }
                    }

                    if (check1.Count == 0)
                    {
                        pos = pos1;
                        lt  = lt1;
                    }
                    else if (check2.Count == 0)
                    {
                        pos = pos2;
                        lt  = lt2;
                    }

                    if (pos != null)
                    {
                        for (int i = 0; i < pos.Length; i++)
                        {
                            if (pos[i] != null)
                            {
                                pos[i] = pos[i].Clone();
                            }
                        }

                        tempRhythm    = new short[2][];
                        tempRhythm[0] = pos1[0].Rhythm;
                        tempRhythm[1] = pos1[1].Rhythm;

                        tempMedian    = new short[2][];
                        tempMedian[0] = pos1[0].Median;
                        tempMedian[1] = pos1[1].Median;
                    }
                }
                else if (nrSim == 12)
                {
                    Signal[] pos1 = new Signal[lt1.Length];
                    Signal[] pos2 = new Signal[lt2.Length];

                    ArrayList pos_list1 = new ArrayList(lt1);
                    ArrayList pos_list2 = new ArrayList(lt2);

                    ArrayList check1 = new ArrayList(
                        new LeadType[] { LeadType.I, LeadType.II, LeadType.III,
                                         LeadType.V1, LeadType.V2, LeadType.V3,
                                         LeadType.V4, LeadType.V5, LeadType.V6,
                                         LeadType.V7, LeadType.V3R, LeadType.V4R });

                    ArrayList check2 = new ArrayList(
                        new LeadType[] { LeadType.I, LeadType.II, LeadType.III,
                                         LeadType.V1, LeadType.V2, LeadType.V3,
                                         LeadType.V4, LeadType.V5, LeadType.V6,
                                         LeadType.V7, LeadType.V8, LeadType.V9 });

                    for (int i = 0; i < nrSim; i++)
                    {
                        int temp = check1.IndexOf(_Lead[i].Type);
                        if (temp >= 0)
                        {
                            check1.RemoveAt(temp);

                            pos1[pos_list1.IndexOf(_Lead[i].Type)] = _Lead[i];
                        }
                        temp = check2.IndexOf(_Lead[i].Type);
                        if (temp >= 0)
                        {
                            check2.RemoveAt(temp);

                            pos2[pos_list2.IndexOf(_Lead[i].Type)] = _Lead[i];
                        }
                    }

                    if (check1.Count == 0)
                    {
                        pos = pos1;
                        lt  = lt1;
                    }
                    else if (check2.Count == 0)
                    {
                        pos = pos2;
                        lt  = lt2;
                    }

                    if (pos != null)
                    {
                        for (int i = 0; i < pos.Length; i++)
                        {
                            if (pos[i] != null)
                            {
                                pos[i] = pos[i].Clone();
                            }
                        }

                        tempRhythm    = new short[3][];
                        tempRhythm[0] = pos[0].Rhythm;
                        tempRhythm[1] = pos[1].Rhythm;
                        tempRhythm[2] = pos[2].Rhythm;

                        tempMedian    = new short[3][];
                        tempMedian[0] = pos[0].Median;
                        tempMedian[1] = pos[1].Median;
                        tempMedian[2] = pos[2].Median;
                    }
                }

                if ((tempRhythm != null) ||
                    (tempMedian != null))
                {
                    short[][] calcLeads;

                    if ((tempRhythm != null) &&
                        (tempRhythm[0] != null) &&
                        ECGTool.CalculateLeads(tempRhythm, tempRhythm[0].Length, out calcLeads) == 0)
                    {
                        for (int i = 0; i < calcLeads.Length; i++)
                        {
                            Signal sig = new Signal();
                            sig.Type        = lt[i + tempRhythm.Length];
                            sig.RhythmStart = pos[0].RhythmStart;
                            sig.RhythmEnd   = pos[0].RhythmEnd;
                            sig.Rhythm      = calcLeads[i];

                            pos[i + tempRhythm.Length] = sig;
                        }

                        if ((tempMedian != null) &&
                            (tempMedian[0] != null) &&
                            (ECGTool.CalculateLeads(tempMedian, tempMedian[0].Length, out calcLeads) == 0))
                        {
                            for (int i = 0; i < calcLeads.Length; i++)
                            {
                                pos[i + tempRhythm.Length].Median = calcLeads[i];
                            }
                        }

                        leads = pos;
                    }
                }
            }

            if (leads != null)
            {
                Signals sigs = this.Clone();

                sigs.NrLeads = (byte)leads.Length;

                for (int i = 0; i < leads.Length; i++)
                {
                    sigs._Lead[i] = leads[i];
                }

                return(sigs);
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Apply highpass filter to Signal object
        /// </summary>
        /// <param name="cutoff">top frequency of bandpass filter</param>
        /// <param name="numSections">nr of sections to use in filter (default: 2)</param>
        /// <param name="filters">provide filters that where used</param>
        /// <returns>a filtered copy of the signal object</returns>
        public Signals ApplyHighpassFilter(double cutoff, int numSections, ref DSP.IFilter[] filters)
        {
            Signals sigs = new Signals();

            sigs.RhythmAVM = this.RhythmAVM;
            sigs.RhythmSamplesPerSecond = this.RhythmSamplesPerSecond;

            sigs.MedianAVM              = this.MedianAVM;
            sigs.MedianLength           = this.MedianLength;
            sigs.MedianSamplesPerSecond = this.MedianSamplesPerSecond;
            sigs.MedianFiducialPoint    = this.MedianFiducialPoint;

            if (this.QRSZone != null)
            {
                sigs.QRSZone = new QRSZone[this.QRSZone.Length];

                for (int i = 0; i < sigs.QRSZone.Length; i++)
                {
                    sigs.QRSZone[i] = this.QRSZone[i].Clone();
                }
            }

            if (this._Lead != null)
            {
                sigs.NrLeads = this.NrLeads;

                if (filters == null ||
                    filters.Length != this.NrLeads)
                {
                    filters = new DSP.IFilter[this.NrLeads];
                }

                for (int i = 0; i < sigs._Lead.Length; i++)
                {
                    DSP.IFilter
                        rhythmFilter = null,
                        medianFilter = null;

                    if ((_Lead[i].Rhythm != null) &&
                        (this.RhythmSamplesPerSecond > 0))
                    {
                        if ((filters[i] == null) ||
                            !(filters[i] is DSP.HighpassFilterButterworthImplementation))
                        {
                            filters[i] = new DSP.HighpassFilterButterworthImplementation(cutoff, numSections, sigs.RhythmSamplesPerSecond);
                        }

                        rhythmFilter = filters[i];
                    }

                    if ((_Lead[i].Median != null) &&
                        (this.MedianSamplesPerSecond > 0))
                    {
                        medianFilter = new DSP.HighpassFilterButterworthImplementation(cutoff, numSections, sigs.MedianSamplesPerSecond);
                    }

                    sigs._Lead[i] = this._Lead[i].ApplyFilter(rhythmFilter, medianFilter);

                    if (sigs._Lead[i] == null)
                    {
                        return(null);
                    }
                }
            }

            return(sigs);
        }
Beispiel #4
0
        public void Run()
        {
            try
            {
                if (_NoArgs)
                {
                    Help();
                }
                else if (_BadArgs)
                {
                    Error();
                    Help();
                }
                else
                {
                    if (_InType != null)
                    {
                        converter.waitForLoadingAllPlugins();
                    }

                    IECGReader reader = _InType == null ? new UnknownECGReader() : converter.getReader(_InType);

                    if (reader == null)
                    {
                        Console.Error.WriteLine("Error: no reader provided by input type!");

                        return;
                    }

                    ECGConfig config1 = ECGConverter.Instance.getConfig(_InType);

                    if (config1 != null)
                    {
                        for (int i = 0; i < _Config.Count; i++)
                        {
                            config1[(string)_Config.GetKey(i)] = (string)_Config.GetByIndex(i);
                        }
                    }

                    IECGFormat src = reader.Read(_InFile, _InFileOffset, config1);

                    if (src == null ||
                        !src.Works())
                    {
                        Console.Error.WriteLine("Error: {0}", reader.getErrorMessage());

                        return;
                    }

                    if ((_BufferedSecondsToLoad > 0) ||
                        (_BufferedOffsetToLoad > 0))
                    {
                        ECGConversion.ECGSignals.Signals sigs = null;

                        if ((src.Signals.getSignals(out sigs) == 0) &&
                            sigs.IsBuffered)
                        {
                            ECGConversion.ECGSignals.BufferedSignals bs = (ECGConversion.ECGSignals.BufferedSignals)sigs;

                            int start = 0,
                                end   = 0;

                            start = bs.RealRhythmStart + (_BufferedOffsetToLoad * bs.RhythmSamplesPerSecond);
                            end   = (_BufferedSecondsToLoad == int.MaxValue) ? _BufferedSecondsToLoad : start + (_BufferedSecondsToLoad * bs.RhythmSamplesPerSecond);

                            if (start > bs.RealRhythmEnd)
                            {
                                start = bs.RealRhythmEnd;
                            }

                            if (end > bs.RealRhythmEnd)
                            {
                                end = bs.RealRhythmEnd;
                            }

                            if (start < end)
                            {
                                bs.LoadSignal(start, end);

                                src.Signals.setSignals(bs);
                            }
                        }
                    }

                    IECGManagementSystem manSys = _OutFile == null ? null : converter.getECGManagementSystem(_OutType);

                    config1 = ECGConverter.Instance.getConfig(manSys == null ? _OutType : manSys.FormatName);

                    ECGConfig config2 = manSys == null ? null : manSys.Config;

                    for (int i = 0; i < _Config.Count; i++)
                    {
                        if (config1 != null)
                        {
                            config1[(string)_Config.GetKey(i)] = (string)_Config.GetByIndex(i);
                        }

                        if (config2 != null)
                        {
                            config2[(string)_Config.GetKey(i)] = (string)_Config.GetByIndex(i);
                        }
                    }

                    if ((config1 != null) &&
                        !config1.ConfigurationWorks())
                    {
                        Console.Error.WriteLine("Error: Bad Configuration for ECG Format!");

                        return;
                    }

                    if ((config2 != null) &&
                        !config2.ConfigurationWorks())
                    {
                        Console.Error.WriteLine("Error: Bad Configuration for ECG Management System!");

                        return;
                    }

                    if (manSys == null)
                    {
                        IECGFormat dst = src.GetType() == ECGConverter.Instance.getType(_OutType) ? src : null;

                        if (dst == null)
                        {
                            if ((src.Demographics != null) &&
                                (src.Demographics.PatientID == null))
                            {
                                src.Demographics.PatientID = _PatientId;
                            }

                            ECGConverter.Instance.Convert(src, _OutType, config1, out dst);
                        }

                        if ((dst == null) ||
                            !dst.Works())
                        {
                            Console.Error.WriteLine("Error: Conversion Failed!");

                            return;
                        }

                        if (_Anonymize)
                        {
                            dst.Anonymous();
                        }

                        if ((_PatientId != null) &&
                            (dst.Demographics != null))
                        {
                            dst.Demographics.PatientID = _PatientId;
                        }

                        ECGWriter.Write(dst, _OutFile, true);

                        if (ECGWriter.getLastError() != 0)
                        {
                            Console.Error.WriteLine("Error: {0}", ECGWriter.getLastErrorMessage());
                        }
                    }
                    else
                    {
                        if (manSys.ConfiguredToSave())
                        {
                            if (_Anonymize)
                            {
                                src.Anonymous();
                            }

                            manSys.SaveECG(src, _PatientId, config1);
                        }
                        else
                        {
                            Console.Error.WriteLine("Error: Not configured to store!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error: {0}", ex.ToString());
            }
        }