5
$\begingroup$

I'm very often in the situation that I need to explain / visualize spectrum of signals or frequency behaviour of systems (esp. filters). My favourite, very "textbook" styled way of communicating these would be, in simpler cases:

simple spectra, Bellanger¹

Typically, I'd have to visualize something like aliasing, effects of suppressing carriers etc. This also leads to less simple situations, like

complicated spectra, Bellanger¹

Now, drawing these kinds of diagrams is really a burden if done with my vector program of choice (that being Inkscape) – I want to parametrically specify the spectrum, not manually shift, copy, mirror and repeat lines!

So:

How do you approach theses kind of visualizations? Vector graphics programs & patience? Pen & paper? Or do you have a private set of scripts that e.g. draw the diagram with the help of matplotlib or tikz?


¹ Bellanger: Digital Processing of Signals: Theory and Practice. 2nd Edition, Wiley 1988. P. 17 & p. 18.

$\endgroup$
7
+100
$\begingroup$

Personally, I value aesthetics and consistency in my documents, so I use Latex whenever I can. I use Inkscape if I'm in a hurry, though.

I have found that the pgfplots package (built on top of tikz) reduces the time needed to code a plot substantially, especially once you get the hang of it. As a simple example, the following filter response:

filter

was generated with the following Latex code:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
        height=5cm, width=7cm, xlabel={$f$}, ylabel={Filter A},
        xmin=0, xmax=300, ymin=0, ymax=1.6, no markers,
        xtick={100,175,250}, ytick={0.25,0.5,0.75,1},
        axis lines=middle
    ]
    \addplot coordinates {(0,1) (100,1) (250,0)};
    \addplot[black,dotted] coordinates {(0,0.5) (175,0.5) (175,0)}; 
\end{axis}
\end{tikzpicture}
\end{document}

Excluding boilerplate (which can be reused between plots), this is just two lines of "real" code (the \addplot lines). The benefits are the usual latex quality, plus font/style/size consistency with the rest of the document.

As a further example I tried a (simplified) version of the last plot in the question:

filter2

which requires just a bit more code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
        height=5cm,width=9cm,xlabel={$f$},axis lines=middle,
        xmin=-120,xmax=200,ymin=0,ymax=1.8,no markers,
        xtick={80,160},xticklabels={$\frac{f_s}{2}$,$f_s=\frac{1}{T}$},
        ytick={0},yticklabels={},ylabel={$S_s(f)$}
	]
    \addplot[black] coordinates {(-100,0) (-20,1) (0,0)};
    \addplot[black] coordinates {(0,0) (20,1) (100,0)};
    \addplot[black,name path=A] coordinates {(60,0) (140,1) (160,0)};
    \addplot[black,dashed,name path=B] coordinates {(80,0) (80,1.5)};
    \addplot[blue] fill between[of=A and B,soft clip={domain=60:80}];
\end{axis}
\end{tikzpicture}
\end{document}

It is true that generating these plots takes time. When working on anything but the most trivial plot, I constantly need to refer to the manuals and to tex.stackexchange.com. I think the examples above show that the pgfplots package removes some of the pain; once you get practice with the most common commands, it shouldn't take too long to create nice plots.

A final point: while I don't (yet) have a program to automatically generate spectrum plots, it shouldn't be too hard to do. I do have a script to generate pgfplots scripts from Matlab/Octave, which you can see here. This script takes two or more Matlab arrays and a plot description, writes a latex file, and runs pdflatex on it. While laborious, coding it wasn't particularly difficult. It should be possible to modify it to take spectrum descriptions (maybe in the edges, amplitudes format of the filter design commands) and generate the plots automatically.

$\endgroup$
  • 1
    $\begingroup$ Nice answer!!!!! $\endgroup$ – Peter K. May 1 '16 at 2:53
  • $\begingroup$ This really matches my needs extraordinarily well $\endgroup$ – Marcus Müller May 1 '16 at 10:33
  • $\begingroup$ I'm glad my answer was nice and helpful! :) $\endgroup$ – MBaz May 1 '16 at 18:10
  • $\begingroup$ @MBaz shoot, nearly forgot to mention: Used this at least once in public, talking to radio amateur (so this was a bit of a "get a feeling", not a "get the math" talk); p. 23f in marcus.hostalia.de/sdra16.pdf $\endgroup$ – Marcus Müller Jul 31 '16 at 8:03
  • $\begingroup$ @MarcusMüller That's good to know! Thanks for sharing. $\endgroup$ – MBaz Jul 31 '16 at 16:36
3
$\begingroup$

I had to visualize different mathematical and technical processes in the past. I know Inkscape and Inkscape... Well, it is free and more or less cross platform. But besides of that, it also is very painful to use.

Here is what I do in those situations:

If it is a one-time sketch to be done

If I just need a sketch of something (i.e. the functions can be wobbly lines and do not have to be very accurate, angles don't have to be fully correct, etc.), I usually use an appropriate program and create the illustration by hand.

If it has to be quite accurate

I do the calculations in some math program (e.g. MATLAB, iPython, Maple) with appropriately parametric functions etc. If the graphical output is...

  1. ok, I just take it and be happy
  2. not ok (I am looking at you, MATLAB! Your SVG output is terrible!), I try to export the graphics and enhance them by hand, e.g. in a vector image program

If it has to be reproducible

If chances are, that you have to do the visualization more often, maybe with modifications, there is no way around a scripted creation of your images. Normally, I spend long hours to get the graphics output good enough in the math program. If there is another way of visualizing, I would script the math program to output the numbers and feed those to the appropriate program, that then creates my images. Here I have to mention PovRay for 3D visualizations, and it also does come with scripting capabilities, which is helpful.

Thoughts on your diagrams

I guess in your case, I would go for another option:

  1. Think about the output style to simplify it. For example, do you really need overlapping regions to be hatched? Would it be easier, if the overlap would be another color, or would be visible from two overlapping semi-transparent polygons that yield a darker color in the overlap region due to the semi-transparancy?

  2. Implement a parametric model of the things you would like to visualize in a scripting language that has high-level support for vector graphics, e.g. Python with some SVG modules

  3. Create the necessary SVG objects (lines with arrow heads as axes, dotted lines as grids, triangles and squares, etc.), and position them accordingly.

  4. Make sure that your images are always contained in the drawing area, so that they always have the same size when you change the model parameters and recreate the images. You do not want to end up with images that vary in their canvas size, because this makes recreating documents a new kind of hell.

Final note

I am sure that there are visualization tools out there that allow you to draw something and apply some kind of dynamic parametrization, where a click on a slider directly produces a live-view of the image with the modified parameter. However, I would not count on such a thing, because of a simple reason: If there is one option missing that you need, it might become painful or impossible to implement a workaround for the function you require. Besides from OS restrictions, interoperability issues, costs, etc.

$\endgroup$
  • $\begingroup$ Thank you for your extensive answer! To be completely frank, the point is that I know of no Matlab/Octave/Python/R… module to generate such spectral visualizations (hence, the section "Thoughts on your diagrams" is the most relevant to me); so, really, you're sadly not offering me anything I don't know yet :( $\endgroup$ – Marcus Müller Apr 30 '16 at 6:35
  • $\begingroup$ However, you mention that "if it has to be quite accurate", you do the drawing with some parametric function; that is a pretty interesting lead; you don't happen to have a recipe on how to e.g. highlight overlapping areas (you're right, they don't have to be hatched, colored would be OK,too) in Octave, matplotlib, Matlab etc? $\endgroup$ – Marcus Müller Apr 30 '16 at 6:36
  • $\begingroup$ I do not know of an automated out-of-the-box way to do that in MATLAB. However, there is the function fill() that lets you create filled polygons. By setting the coordinates of the polygon to your function values, it should be easy to come up with a function that produces the desired filled area for two given functions. $\endgroup$ – M529 Apr 30 '16 at 9:02

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.