ecgtool.cs
来自「ecg tool kit for medical image retrieval」· CS 代码 · 共 829 行 · 第 1/2 页
CS
829 行
/***************************************************************************
Copyright 2004,2008, Thoraxcentrum, Erasmus MC, Rotterdam, The Netherlands
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Written by Maarten JB van Ettinger.
****************************************************************************/
using System;
using System.Collections;
namespace ECGConversion
{
/// <summary>
/// a tool for calculating some leads.
/// </summary>
public class ECGTool
{
/// <summary>
/// Interval to use for Resampling with polynomial in msec.
/// </summary>
public static int ResampleInterval = 20;
public static double[][] Fast = null;
/// <summary>
/// Calculate four leads from lead I, II and (optionaly) III.
/// </summary>
/// <returns>error:
/// 0x00) succes
/// 0x01) given leads are wrong
/// 0x02) error during caculation lead III
/// 0x04) error during caculation lead aVR
/// 0x08) error during caculation lead aVL
/// 0x10) error during caculation lead aVF</returns>
public static int CalculateLeads(short[][] in_leads, int TotalLength, out short[][] out_leads)
{
out_leads = null;
if (in_leads != null)
{
if (in_leads.Length == 2)
{
return CalculateLeads(in_leads[0], in_leads[1], TotalLength, out out_leads);
}
else if (in_leads.Length == 3)
{
return CalculateLeads(in_leads[0], in_leads[1], in_leads[2], TotalLength, out out_leads);
}
}
return 1;
}
/// <summary>
/// Calculate four leads from lead I and II.
/// </summary>
/// <returns>error:
/// 0x00) succes
/// 0x01) given leads are wrong
/// 0x02) error during caculation lead III
/// 0x04) error during caculation lead aVR
/// 0x08) error during caculation lead aVL
/// 0x10) error during caculation lead aVF</returns>
public static int CalculateLeads(short[] leadI, short[] leadII, int TotalLength, out short[][] leads)
{
return CalculateLeads(leadI, 0, leadI.Length, leadII, 0, leadII.Length, TotalLength, out leads);
}
/// <summary>
/// Calculate four leads from lead I, II and III.
/// </summary>
/// <returns>error:
/// 0x00) succes
/// 0x01) given leads are wrong
/// 0x04) error during caculation lead aVR
/// 0x08) error during caculation lead aVL
/// 0x10) error during caculation lead aVF</returns>
public static int CalculateLeads(short[] leadI, short[] leadII, short[] leadIII, int TotalLength, out short[][] leads)
{
return CalculateLeads(leadI, 0, leadI.Length, leadII, 0, leadII.Length, leadIII, 0, leadIII.Length, TotalLength, out leads);
}
/// <summary>
/// Calculate four leads from lead I and II.
/// </summary>
/// <returns>error:
/// 0x00) succes
/// 0x01) given leads are wrong
/// 0x02) error during caculation lead III
/// 0x04) error during caculation lead aVR
/// 0x08) error during caculation lead aVL
/// 0x10) error during caculation lead aVF</returns>
public static int CalculateLeads(short[] leadI, int beginI, int lengthI, short[] leadII, int beginII, int lengthII, int totalLength, out short[][] leads)
{
leads = null;
if ((leadI != null)
&& (leadII != null)
&& (beginI >= 0)
&& (beginII >= 0)
&& (lengthI > 0)
&& (lengthII > 0)
&& (lengthI <= leadI.Length)
&& (lengthII <= leadII.Length)
&& ((beginI + lengthI) <= totalLength)
&& ((beginII + lengthII) <= totalLength))
{
leads = new short[4][];
leads[0] = _CalculateLeadIII(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength);
if (leads[0] == null)
{
return 0x2;
}
leads[1] = _CalculateLeadaVR(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength);
if (leads[1] == null)
{
return 0x4;
}
leads[2] = _CalculateLeadaVL(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength, false);
if (leads[2] == null)
{
return 0x8;
}
leads[3] = _CalculateLeadaVF(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength, false);
if (leads[3] == null)
{
return 0x10;
}
return 0x0;
}
return 0x1;
}
/// <summary>
/// Calculate four leads from lead I and II.
/// </summary>
/// <returns>error:
/// 0x00) succes
/// 0x01) given leads are wrong
/// 0x02) error during caculation lead III
/// 0x04) error during caculation lead aVR
/// 0x08) error during caculation lead aVL
/// 0x10) error during caculation lead aVF</returns>
public static int CalculateLeads(short[] leadI, int beginI, int lengthI, short[] leadII, int beginII, int lengthII, short[] leadIII, int beginIII, int lengthIII, int totalLength, out short[][] leads)
{
leads = null;
if ((leadI != null)
&& (leadII != null)
&& (beginI >= 0)
&& (beginII >= 0)
&& (beginIII >= 0)
&& (lengthI > 0)
&& (lengthII > 0)
&& (lengthIII > 0)
&& (lengthI <= leadI.Length)
&& (lengthII <= leadII.Length)
&& (lengthIII <= leadIII.Length)
&& ((beginI + lengthI) <= totalLength)
&& ((beginII + lengthII) <= totalLength)
&& ((beginIII + lengthIII) <= totalLength))
{
leads = new short[3][];
leads[0] = _CalculateLeadaVR(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength);
if (leads[0] == null)
{
return 0x4;
}
leads[1] = _CalculateLeadaVL(leadI, beginI, lengthI, leadIII, beginIII, lengthIII, totalLength, true);
if (leads[1] == null)
{
return 0x8;
}
leads[2] = _CalculateLeadaVF(leadIII, beginIII, lengthIII, leadII, beginII, lengthII, totalLength, true);
if (leads[2] == null)
{
return 0x10;
}
return 0x0;
}
return 0x1;
}
/// <summary>
/// Calculate lead III from lead I and II.
/// </summary>
public static short[] CalculateLeadIII(short[] leadI, int beginI, int lengthI, short[] leadII, int beginII, int lengthII, int totalLength)
{
if ((leadI != null)
&& (leadII != null)
&& (beginI >= 0)
&& (beginII >= 0)
&& (lengthI > 0)
&& (lengthII > 0)
&& (lengthI <= leadI.Length)
&& (lengthII <= leadII.Length)
&& ((beginI + lengthI) <= totalLength)
&& ((beginII + lengthII) <= totalLength))
{
return _CalculateLeadIII(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength);
}
return null;
}
/// <summary>
/// Hidden function to calculate III (input must be checked before using this function).
/// </summary>
private static short[] _CalculateLeadIII(short[] leadI, int beginI, int lengthI, short[] leadII, int beginII, int lengthII, int totalLength)
{
short[] ret = new short[totalLength];
for (int loper=0;loper < totalLength;loper++)
{
short dataI = ((loper >= beginI) && (loper < (beginI + lengthI)) ? leadI[loper - beginI] : (short)0);
short dataII = ((loper >= beginII) && (loper < (beginII + lengthII)) ? leadII[loper - beginII] : (short)0);
ret[loper] = (short) (dataII - dataI);
}
return ret;
}
/// <summary>
/// Calculate lead aVR from lead I and II.
/// </summary>
public static short[] CalculateLeadaVR(short[] leadI, int beginI, int lengthI, short[] leadII, int beginII, int lengthII, int totalLength)
{
if ((leadI != null)
&& (leadII != null)
&& (beginI >= 0)
&& (beginII >= 0)
&& (lengthI > 0)
&& (lengthII > 0)
&& (lengthI <= leadI.Length)
&& (lengthII <= leadII.Length)
&& ((beginI + lengthI) <= totalLength)
&& ((beginII + lengthII) <= totalLength))
{
return _CalculateLeadaVR(leadI, beginI, lengthI, leadII, beginII, lengthII, totalLength);
}
return null;
}
/// <summary>
/// Hidden function to calculate aVR (input must be checked before using this function).
/// </summary>
private static short[] _CalculateLeadaVR(short[] leadI, int beginI, int lengthI, short[] leadII, int beginII, int lengthII, int totalLength)
{
short[] ret = new short[totalLength];
for (int loper=0;loper < totalLength;loper++)
{
short dataI = ((loper >= beginI) && (loper < (beginI + lengthI)) ? leadI[loper - beginI] : (short)0);
short dataII = ((loper >= beginII) && (loper < (beginII + lengthII)) ? leadII[loper - beginII] : (short)0);
ret[loper] = (short) -((dataI + dataII) >> 1);
}
return ret;
}
/// <summary>
/// Calculate lead aVL from lead I and II/III.
/// </summary>
public static short[] CalculateLeadaVL(short[] leadI, int beginI, int lengthI, short[] leadX, int beginX, int lengthX, int totalLength, bool threeLeads)
{
if ((leadI != null)
&& (leadX != null)
&& (beginI >= 0)
&& (beginX >= 0)
&& (lengthI > 0)
&& (lengthX > 0)
&& (lengthI <= leadI.Length)
&& (lengthX <= leadX.Length)
&& ((beginI + lengthI) <= totalLength)
&& ((beginX + lengthX) <= totalLength))
{
return _CalculateLeadaVF(leadI, beginI, lengthI, leadX, beginX, lengthX, totalLength, threeLeads);
}
return null;
}
/// <summary>
/// Hidden function to calculate aVL (input must be checked before using this function).
/// </summary>
private static short[] _CalculateLeadaVL(short[] leadI, int beginI, int lengthI, short[] leadX, int beginX, int lengthX, int totalLength, bool threeLead)
{
short[] ret = new short[totalLength];
if (threeLead)
{
for (int loper=0;loper < totalLength;loper++)
{
short dataI = ((loper >= beginI) && (loper < (beginI + lengthI)) ? leadI[loper - beginI] : (short)0);
short dataIII = ((loper >= beginX) && (loper < (beginX + lengthX)) ? leadX[loper - beginX] : (short)0);
ret[loper] = (short) ((dataI - dataIII) >> 1);
}
}
else
{
for (int loper=0;loper < totalLength;loper++)
{
short dataI = ((loper >= beginI) && (loper < (beginI + lengthI)) ? leadI[loper - beginI] : (short)0);
short dataII = ((loper >= beginX) && (loper < (beginX + lengthX)) ? leadX[loper - beginX] : (short)0);
ret[loper] = (short) (((dataI << 1) - dataII) >> 1);
}
}
return ret;
}
/// <summary>
/// Calculate lead aVF from lead I/III and II.
/// </summary>
public static short[] CalculateLeadaVF(short[] leadX, int beginX, int lengthX, short[] leadII, int beginII, int lengthII, int totalLength, bool threeLead)
{
if ((leadX != null)
&& (leadII != null)
&& (beginX >= 0)
&& (beginII >= 0)
&& (lengthX <= leadX.Length)
&& (lengthII <= leadII.Length)
&& ((beginX + lengthX) <= totalLength)
&& ((beginII + lengthII) <= totalLength))
{
return _CalculateLeadaVF(leadX, beginX, lengthX, leadII, beginII, lengthII, totalLength, threeLead);
}
return null;
}
/// <summary>
/// Hidden function to calculate aVF (input must be checked before using this function).
/// </summary>
private static short[] _CalculateLeadaVF(short[] leadX, int beginX, int lengthX, short[] leadII, int beginII, int lengthII, int totalLength, bool threeLead)
{
short[] ret = new short[totalLength];
if (threeLead)
{
for (int loper=0;loper < totalLength;loper++)
{
short dataIII = ((loper >= beginX) && (loper < (beginX + lengthX)) ? leadX[loper - beginX] : (short)0);
short dataII = ((loper >= beginII) && (loper < (beginII + lengthII)) ? leadII[loper - beginII] : (short)0);
ret[loper] = (short) ((dataII + dataIII) >> 1);
}
}
else
{
for (int loper=0;loper < totalLength;loper++)
{
short dataI = ((loper >= beginX) && (loper < (beginX + lengthX)) ? leadX[loper - beginX] : (short)0);
short dataII = ((loper >= beginII) && (loper < (beginII + lengthII)) ? leadII[loper - beginII] : (short)0);
ret[loper] = (short) (((dataII << 1) - dataI) >> 1);
}
}
return ret;
}
/// <summary>
/// Function to resample a signal.
/// </summary>
/// <param name="src">signal to resample</param>
/// <param name="srcFreq">sample rate of signal</param>
/// <param name="dstFreq">destination sample rate</param>
/// <param name="dst">resampled signals</param>
/// <returns>0 on success</returns>
public static int ResampleSignal(short[][] src, int srcFreq, int dstFreq, out short[][] dst)
{
dst = null;
if ((src != null)
&& (src.Length > 0)
&& (srcFreq > 0)
&& (dstFreq > 0))
{
if (srcFreq == dstFreq)
{
dst = src;
return 0;
}
dst = new short[src.Length][];
// Do resampling for each lead.
for (int loper=0;loper < dst.Length;loper++)
{
if (ResampleLead(src[loper], srcFreq, dstFreq, out dst[loper]) != 0)
{
dst = null;
return (0x2 << loper);
}
}
return 0;
}
return 1;
}
/// <summary>
/// Function to resample a signal.
/// </summary>
/// <param name="src">signal to resample</param>
/// <param name="nrsamples">nr of samples to resample</param>
/// <param name="srcFreq">sample rate of signal</param>
/// <param name="dstFreq">destination sample rate</param>
/// <param name="dst">resampled signals</param>
/// <returns>0 on success</returns>
public static int ResampleSignal(short[][] src, int nrsamples, int srcFreq, int dstFreq, out short[][] dst)
{
return ResampleSignal(src, 0, nrsamples, srcFreq, dstFreq, out dst);
}
/// <summary>
/// Function to resample a signal.
/// </summary>
/// <param name="src">signal to resample</param>
/// <param name="startsample">sample number to start at.</param>
/// <param name="nrsamples">nr of samples to resample</param>
/// <param name="srcFreq">sample rate of signal</param>
/// <param name="dstFreq">destination sample rate</param>
/// <param name="dst">resampled signals</param>
/// <returns>0 on success</returns>
public static int ResampleSignal(short[][] src, int startsample, int nrsamples, int srcFreq, int dstFreq, out short[][] dst)
{
dst = null;
if ((src != null)
&& (src.Length > 0)
&& (srcFreq > 0)
&& (dstFreq > 0)
&& (startsample >= 0)
&& (nrsamples > 0))
{
if (srcFreq == dstFreq)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?