1

I am trying to typeset some examples of given signals using tikz/pgf. My initial thought was to plot them from ascii files using pgfplots. My code is the following

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    domain=100:500,
    xmin=100, xmax=500,
    ymin=-260, ymax=40,
    axis lines=center,
    axis equal image,
    xlabel=$time$,
    ]
    \addplot[smooth] table {FINAL.dat}
        node[right] {$g(x)$};
    \addplot[color=blue, smooth] table {Single_Narrow.dat}
        node[above right] {$v(x)$};
    \addplot[color=red, smooth] table {Single_Medium.dat}
        node[above right] {$v(x)$};
\end{axis}
\end{tikzpicture}
\end{document}

The problem is that the signals I have are not smoothed, as can be seen in the following picture

enter image description here

My questions are

  1. Is there a way to smooth the plotted signals?
  2. Can I generate such signals in tikz/pgf?

An editable copy of the project can be found in overleaf.

  • Of course I do not know the content of your .dat files but I would guess that the issues are do to those, not pgfplots. And yes, you can draw such things with plain TikZ, just sometimes it is a bit more cumbersome to set the units by hand, and pgfplots has more built in features that allow you to tame large or very small numbers. – user121799 Jul 17 '18 at 16:45
  • @marmot : Thank you very much for your comment. The .dat files can be seen in the link I provided, but I was thinking of generating "signals" with different rise times in tikz/pgf. – Thanos Jul 17 '18 at 16:48
  • Which link? (I didn't see any dat files in the overleaf link, but I should add that I don't know anything about overleaf except that I made a very bad experience once answering a question where the code was in a overleaf project...) – user121799 Jul 17 '18 at 16:51
  • 1
    You can also play with the tension parameter (pgfmanual v3.0.1a Section 22.8 p333) for the smoothing. When plotting data with high frequency content, I also modify the line join parameter. Initially, it is set to miter which gives sharp edges. You can try bevel or round (pgfmanual v3.0.1a Section 15.3.1 p167). – BambOo Jul 17 '18 at 17:01
3

The .dat files in your Overleaf project have a lot of data points. You could add each nth point=10, filter discard warning=false, unbounded coords=discard to your axis options, this should make your plot a lot smoother. I've added markers so you can see which points are plotted.

\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    domain=100:500,
    xmin=100, xmax=500,
    ymin=-260, ymax=40,
    axis lines=center,
    axis equal image,
    xlabel=$time$,
    each nth point=10, 
    filter discard warning=false, 
    unbounded coords=discard,
    ]
    \addplot[smooth,mark=x] table {FINAL.dat}
        node[right] {$g(x)$};
    \addplot[color=blue, smooth,each nth point=10,mark=x] table {Single_Narrow.dat}
        node[above right] {$v(x)$};
    \addplot[color=red, smooth,each nth point=10,mark=x] table {Single_Medium.dat}
        node[above right] {$v(x)$};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The problem is that this method will throw away data, so the very small crevice at approximately x=300 will probably disappear. To counteract that, you can locally add some extra points, but you will have to plot multiple times. I only did this for the first plot, and added a legend because the nodes you draw are clipped away because they are drawn outside the axis region.

\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    domain=100:500,
    xmin=100, xmax=500,
    ymin=-260, ymax=40,
    axis lines=center,
    axis equal image,
    xlabel=$time$,
    filter discard warning=false, 
    unbounded coords=discard,
    ]
    \addplot[smooth,each nth point=10,mark=x,restrict x to domain=100:290] table {FINAL.dat};
    \addplot[smooth,each nth point=1,mark=x,restrict x to domain=280:320,forget plot] table {FINAL.dat};
    \addplot[smooth,each nth point=10,mark=x,restrict x to domain=310:500,forget plot] table {FINAL.dat};
    \addlegendentry{\( g(x) \)};
\end{axis}
\end{tikzpicture}
\end{document}

Note that the domains overlap, otherwise jumps will occur. The forget plot key is to prevent the second and third \addplot from showing up in the legend.

enter image description here

  • Thank you very much for your answer! It was exactly what I was looking for! – Thanos Jul 18 '18 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.