Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.graphics.apps.gnuplot > #933

Re: gaussians as plotting style

From Péter Juhász <peter.juhasz83@gmail.com>
Newsgroups comp.graphics.apps.gnuplot
Subject Re: gaussians as plotting style
Date 2012-02-10 08:28 -0800
Organization http://groups.google.com
Message-ID <cb4d5686-b717-4504-aaeb-1577825e3aa3@k6g2000vbz.googlegroups.com> (permalink)
References <slrnjjab2i.f4h.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>

Show all headers | View raw


On Feb 10, 3:40 pm, Martin Keiter <Martin.Keiter_NOS...@gmx.de> wrote:
> Hi,
>
> background: I'm doing theoretical calculations of absorption spectra.
> The results are sharp lines (energy vs. absorption strength).
> Experimental data does in many cases not have sharp lines, but broader
> absorption. many small absortions in a small energy range may add up to
> a bigger absorption band.
> In order to better compare with experiment, I want to have broadened
> lines.
>
> I have a file with x0/i value pairs in every line.
> for every pair of x0/i I want to plot i * exp(-(x0-x)**2)
>
> And then I want to have a sum of all these plots (as in "smooth
> frequency")
>
> I have tried
>
> filename = "1a"
> lines = system(sprintf("wc --lines %s | sed 's/ .*//'", filename))
> plot for [line=1:lines] (\
> x0 = system(sprintf("awk '{if (NR==%d) print $%d;}' < %s " , line, 1, filename)),\
>  i = system(sprintf("awk '{if (NR==%d) print $%d;}' < %s " , line, 2, filename)),\
> i* exp(-(x-x0)**2) )
>
> but this is incredibly slow for a data file with 5 lines:
> 1 1.1
> 2 2.2
> 3 3.3
> 4 4.4
> 5 5.5
>
> And it looks incredibly ugly!
>
> can it be done in a more clever way? Best thing wohl be if gnuplot would
> accept a user defined function for the style, but I think that's not
> possible...?

Both the slowness and ugliness appears to be entirely credible.

Bad jokes aside, your approach can be done more cleverly:

#####################################
filename = '1a'

getcolumn(file, column) = sprintf("perl -ane 'print $F[%d].q/ /' %s",
column-1, file)
x0_str = system(getcolumn(filename,1))
i_str  = system(getcolumn(filename,2))
x0(l)  = real(word(x0_str, l))
i(l)   = real(word(i_str,  l))

# cosmetic fixes
set samples 200
set style fill transparent solid 0.5

plot for [l=1:words(x0_str)] i(l)*exp(-(x-x0(l))**2) w filledc lt 1
notit
#####################################

Note that it uses perl which tends to be faster than awk, and it calls
perl only twice before plotting.

If you want to plot the sum of the resulting gaussians, a different
approach is needed:

#####################################
filename = '1a'

gaussian(x0,a) = sprintf("+%g*exp(-(x-%g)**2)", a, x0)

plotcmd = "sum_gausses(x) = 0+"

set term unknown

plot filename u 1:(plotcmd = plotcmd.gaussian($1,$2), 2)

set term pop

eval plotcmd

plot sum_gausses(x)
######################################

Péter Juhász

Back to comp.graphics.apps.gnuplot | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

gaussians as plotting style Martin Keiter <Martin.Keiter_NOSPAM@gmx.de> - 2012-02-10 14:40 +0000
  Re: gaussians as plotting style sfeam <sfeam@users.sourceforge.net> - 2012-02-10 08:07 -0800
  Re: gaussians as plotting style Péter Juhász <peter.juhasz83@gmail.com> - 2012-02-10 08:28 -0800
    Re: gaussians as plotting style Martin Keiter <Martin.Keiter_NOSPAM@gmx.de> - 2012-02-13 10:34 +0000

csiph-web