如何用 TIkZ 绘制心电图

如何用 TIkZ 绘制心电图

我正在尝试使用 TikZ 绘制心电图轨迹。这是例子心电图上的单次心跳。以下代码生成可接受的模拟,但很难将此代码用作生成器。

 \documentclass{article}
 \usepackage{tikz}
 \usetikzlibrary{calc}


\newcommand{\PR}[1]{\draw (0,0) -- (#1,0);}
\newcommand{\QRS}[2]
{
    \draw (0,0) -- (#2/2,#1) -- (#2,0);
}
\newcommand{\ST}[1]
{
    \draw (0,0) -- (#1/3,0);
    \draw (#1/3,0) arc (180:0:#1/3);
    \draw (#1,0) -- (#1*1.5,0);
}    
\begin{document}

\begin{tikzpicture}
 \PR{2}
 \begin{scope}[shift={(2,0)}]
    \QRS{3}{2}
  \end{scope}
  \begin{scope}[shift={(4,0)}]
     \ST{2}
   \end{scope}
\end{tikzpicture}

\end{document}

我尝试将代码粘贴到函数中:

\newcommand{\RR}[1]
{
    %Healthy PR segment duration upper bound = 0.20s
    %Healthy QRS segment duration upper bound = 0.12s
    %Healthy ST segment duration upper bount = 0.12s

    %Fix QRS amplitude for now
    %This inspires the ratio, 2/1/1

    \PR{#1/2}
    \begin{scope}[shift={(#1,0)}]
        \QRS{3}{#1/4}
        \begin{scope}[shift={(#1/2,0)}]    
            \ST{#1/4}
            \begin{scope}[shift=(#1/2,0)]
                \PR{#1/2}
            \end{scope}
        \end{scope}
    \end{scope}
 %Yes I know this sums to more than 1.
}

此函数超时,可能是因为所有嵌套范围。我怎样才能更优雅地绘制心电图?

答案1

也许这对你有用:

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
 \usepackage{tikz}
 \usetikzlibrary{calc}

\newcommand\aelastnode{\coordinate (lastnode);}
\newcommand{\PR}[1]{  \draw (lastnode) -- ++(#1,0) coordinate (lastnode);}
\newcommand{\QRS}[2]{ \draw (lastnode) -- ++(#2/2,#1) -- ++(#2/2,-#1) coordinate (lastnode);}
\newcommand{\ST}[1]{  \draw (lastnode) -- ++(#1/3,0) arc (180:0:#1/3) -- ++(#1/3,0) coordinate (lastnode);}    

\begin{document}

\noindent
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
  \aelastnode
  \foreach \myn in {1,...,10}
  {
    \PR{2}
    \QRS{3}{2}
    \ST{2}
  }
\end{tikzpicture}

\end{document}

在这里我做了心跳相对于上一个宏进行定位。因此,每个宏都会重新定义lastnode以获取 EKG 的延续。

在此处输入图片描述

答案2

您可以使用定义曲线pic,然后使用\foreach循环重复放置该曲线。

以下是使用来自的形状的示例http://commons.wikimedia.org/wiki/File:EKG_Complex_en.svg,使用以下方式转换为 TikZ 代码SVG2TikZ

 \documentclass{article}
 \usepackage{tikz}

\begin{document}

\begin{tikzpicture}[line join=round, x=2pt, y=-2pt]
\tikzset{
    normal ecg/.pic={
        \draw (0,455.0021) -- (11.8345,455.0021);
        \draw (11.8345,455.0021) .. controls (14.2834,454.8958) and
          (14.1385,448.7114) .. (18.8842,448.7114) .. controls (24.2116,448.7114) and
          (23.9695,454.8958) .. (26.3035,455.0021);
        \draw (26.3035,455.0021) -- (40.7723,455.0021);
        \draw (40.7723,455.0021) -- (42.7455,463.0645) -- (46.0339,413.3461)
          -- (48.6647,466.4235) -- (51.2955,455.0021);
        \draw (51.2955,455.0021) -- (61.1605,455.0021);
        \draw (61.1605,455.0021) .. controls (64.4487,454.3298) and
          (65.7118,441.6860) .. (70.3679,441.5241) .. controls (75.2428,441.3542) and
          (76.9447,454.3301) .. (80.2329,455.0021);
        \draw  (80.2329,455.0021) .. controls (81.4852,455.0021) and
          (82.2677,452.8462) .. (84.1792,452.9863) .. controls (85.8717,453.1101) and
          (86.4830,455.0021) .. (87.4678,455.0021);
        \draw (87.4678,455.0021) -- (100,455.0021);
    }
}
\foreach \x in {0,...,3}{
\pic [very thick, scale=0.5] at (\x*50, 0) {normal ecg};
}
\end{tikzpicture}

\end{document}

相关内容