Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

Find local maximum of data files in gnuplot


Asked 5 years, 7 months ago Active 1 year, 2 months ago Viewed 2k times

I have a list of data (two columns) and I want to plot in gnuplot only the value for which my
second column has a local maximum.
3
To do so I would like to see if the second column of the ith row is bigger than the (i-1) and
(i+1)th rows.

2 gnuplot

asked Jan 27 '15 at 14:42


lambertmular
87 6

2 Answers Active Oldest Votes

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

1 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

It can be done and I was bored enough to do it. I generated the following set of random
data:
5
5191
29375
23222
32118
3185
32355
17173
8734
28850
20811
5956
6950
28560
25770
4630
28272
10035
7209
19428
26187
30784
20326
12865
23288
20924

Plotting the values against their position in the list looks like this:

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

2 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

Thank you very much that seems perfect. However, did you have only one column in your file? I
have two in my case and I think I need to adapt the code. but I am not sure to understand exactly
the meaning of column(0) – lambertmular Jan 27 '15 at 17:41

I managed to make it work, but now I would like to make lines instead of points, is it possible?
– lambertmular Jan 27 '15 at 19:00

See the edited answer for a general solution. For lines you'll need to plot to a table (only the points,
skip the first plot "data" ), then filter it to remove the invalid points: set table "table"; plot
...; unset table; plot "< grep "i" table" w l . – Miguel Jan 27 '15 at 19:06

I already wrote that but it does not work. If I put linepoints lp only the points appear.
– lambertmular Jan 27 '15 at 19:09

1 @Miguel, thank you very much it works. Is it possible to print the count of the peaks? – Mahsolid
Nov 29 '16 at 21:43

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

3 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

Simple gnuplot peak-finder


3 Comparing 3 consecutive data points is a simple way to look for maxima/peaks.

if y(i-1) < y(i) > y(i+1) then you have a maximum/peak at x(i).

However, if you have noisy curves (like experimental spectra typically are) you will get too
many "peaks".

The following gnuplot code allows to set a threshold for what is considered as a peak. The
code basically calculates a number for each peak which is a measure for the
"independence" of a peak. (see https://en.wikipedia.org/wiki/Topographic_prominence). The
code uses datablocks and therefore gnuplot >=5.2 is required. Comments and
improvements are welcome.

Test data: Spectrum.dat

# x y
6.02 153.33
9.59 154.03
9.59 154.03
9.59 154.03
9.83 153.66
10.58 153.22
10.62 153.85
11.32 152.33
11.53 153.67
11.88 153.27
13.28 153.97
13.42 154.35
14.56 152.99
14.75 153.50
15.23 153.91
15.59 153.58
16.56 153.85
16.70 154.49
16.97 153.98
17.23 154.49
17.37 153.73
17.90 154.24
17.93 154.91
18.80 154.23
18.83 154.64
20.37 153.59
20.48 153.99
21.70 153.19
22.29 154.36
22.41 153.38
23.34 155.04
23.41 155.38
24.38 154.71
24.58 154.96
25.40 155.13
By using our25.41
site, you154.70
acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
26.20 154.21
and our Terms of Service.
Code:

4 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

75.18 184.80

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

5 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

6 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

7 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

8 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

9 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

10 sur 11 01/09/2020 à 17:59


Find local maximum of data files in gnuplot - Stack Overflow https://stackoverflow.com/questions/28173128/find-local-maximum-of...

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy,
and our Terms of Service.

11 sur 11 01/09/2020 à 17:59

You might also like