Create scientific plots using gnuplot

May 6th, 2011 | No Comments

loudspeaker circle

Fig. 1 A circular loudspeaker array drawn with the object command (code to produce this figure, set_loudspeaker function)

In one of the last entries we have seen how to plot a loudspeaker with Gnuplot.
This time we will have a look at the case of setting more than one loudspeaker to your plot. Furthermore we allow the placement of the loudspeakers after entries in a data file.
Let us assume we have a data file containing the x position, y position and orientation phi of a single loudspeakers per line. Now we have to read the data with Gnuplot and set the objects according to the data. This can be done by a dummy plot, because by applying the plot command, variables can be stored. For the dummy plot we setting the output of the plot command to table and use /dev/null as the place to write the data.

# --- Read loudspeaker placement from data file
set table '/dev/null'
add_loudspeaker(x,y,phi) = sprintf(\
    'call "set_loudspeaker.gnu" "%f" "%f" "%f" "%f";',x,y,phi,0.2)
CMD = ''
plot 'loudspeaker_pos.dat' u 1:(CMD = CMD.add_loudspeaker($1,$2,$3))
unset table

The plot command now enables us to add the data from the file to the variable CMD, which is then executed by the eval command. To create the variable, the add_loudspeaker function creates a string with the data for every single line of the data file. The eval(CMD) calls the set_loudspeaker.gnu function once for every single data line, which corresponds to a single loudspeaker. The set_loudspeaker.gnu function itself does the same as we have done in the draw a single loudspeaker entry, but in addition it uses a rotation matrix to change the orientation of the single loudspeakers.

After having set the loudspeakers, we add some activity to three of the loudspeakers and finally get the result in Fig. 1.

# --- Plot loudspeaker activity
set parametric
fx(t,r,phi) = -1.5*cos(phi)+r*cos(t)
fy(t,r,phi) = -1.5*sin(phi)+r*sin(t)
set multiplot
set trange [-pi/6+pi/8:pi/6+pi/8]
plot for [n=1:3] fx(t,n*0.25,pi/8),fy(t,n*0.25,pi/8) w l ls 2
unset object
set trange [-pi/6-pi/8:pi/6-pi/8]
plot for [n=1:3] fx(t,n*0.25,-pi/8),fy(t,n*0.25,-pi/8) w l ls 2
set trange [-pi/6:pi/6]
plot for [n=1:3] fx(t,n*0.25,0),fy(t,n*0.25,0) w l ls 1
unset multiplot

The three waves before the desired loudspeakers are plotted within an iteration that effects the radius by using the for command. The unset object is executed after the first plot in the multiplot environment, because the loudspeakers should only be drawn once.

Leave a Reply