3

I have a very huge text file with 11 columns. As I can't post the whole data, I have uploded the text file to a public repo and is found in this link: http://s000.tinyupload.com/?file_id=59483318155908771897

Is there any way to COUNT the number of peaks using GNU plot in Linux? From the above text file, I am plotting the 1st and 7th column as x and y columns where the peaks are variations of the 7th column and that's what I am interested in. For example, to count the number of peaks of frequency as in the following image as 10.

Here a simple plotting script i am using.

set key right top
set xrange [:10]


#show timestamp
set xlabel "time in sec"
set ylabel "Freq"


set title "Testing"
plot "data/freq.csv" using 1:7 title "Freq", \

Thanks for any help.

2
  • 1
    Not with Gnuplot. GNU Octave findpeaks() should be able to do it though. Commented Nov 24, 2016 at 11:50
  • 1
    A couple of points: It is called Gnuplot not GNU plot. Doing signal analysis, like finding peaks in a signal, calls for a tool designed for such tasks, e.g. GNU Octave. Commented Nov 24, 2016 at 15:31

2 Answers 2

4

Gnuplot is for plotting and minor arithmetic, finding peaks in a signal is a signal processing task and you need something like GNU Octave to do a reasonable job. If you load freq.csv file and run findpeaks() on it with a plausible value for MinPeakDistance you get:

Found most peaks

The code I used to generate the above plot:

y = dlmread('freq.csv', ' ');
[peak_y, peak_x] = findpeaks(y(:,7), "MinPeakDistance", 40);
plot(y(:,1), y(:,7), y(peak_x,1), peak_y, '.r');

Depending on what you want findpeaks() might be enough, see help findpeaks and demo findpeaks for other options you can tweak.

Sign up to request clarification or add additional context in comments.

Comments

1

It's a bit of tweaking but this example should help:

y2=y1=y0=NaN
stat "data/freq.csv" using (y2=y1,y1=y0,y0=$7,(y1>y2&&y1>y0?y1:NaN)) prefix "data"

Now in the variable data_records you should get the COUNT of local maximums you have in column 7.

You can print via

print data_records

To understand more, I post here an example of the sinus function

set table 'test.dat'
plot sin(x)
unset table
x2=x1=x0=NaN 
y2=y1=y0=NaN
plot 'test.dat' using (x2=x1,x1=x0,x0=$1,x1):(y2=y1,y1=y0,y0=$2,(y1>y2&&y1>y0?y1:NaN)) w p,  'test.dat' u 1:2 w l

Should plot a sinus and also the maximum points.

In case several points have the same value:

x2=x1=x0=NaN
y2=y1=y0=NaN
plot 'freq.csv' u 0:7 w l, '' using (x2=x1,x1=x0,x0=$0,x1):(y2=y1,y1=y0,y0=$7,(y1>=y2&&y1>y0?y1:NaN)) w p

or

plot 'freq.csv' u 0:7 w l, '' using (x2=x1,x1=x0,x0=$0,x1):(y2=y1,y1=y0,y0=$7,(y1>y2&&y1>=y0?y1:NaN)) w p

depending on which side of the plateau you want to count the peak

The stat command becomes:

stat 'freq.csv'  using (y2=y1,y1=y0,y0=$7,(y1>=y2&&y1>y0?y1:NaN)) prefix "data"

9 Comments

Why sin(x)? I don't understand it dear. Could you edit it as per my sample code in the question? Thanks!
I've updated the answer. The second part on sin is just to make it clear, since in you snippet of data there's no local maximum on column 7. But you just asked a way to COUNT, not to plot them.
There is another quirk in your datafile, it's comma separated data, so you need to tell this to gnuplot: set datafile separator ','
by default gnuplot looks for spaces, so you shouldn't do anything. If you still have problems, please post the data you want to plot by updating the question
ok data is peculiar because I was looking for a peak, but in your case you have several peaks with the same value so you either change the first or the second > to >=. I have uploaded a comment to the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.