Board index » delphi » Algorithm needed for counting peaks on chart

Algorithm needed for counting peaks on chart

Does anyone know of an algorithm to count no of peaks on a line/bar
chart
Thanks in advance.
--
Claire
 

Re:Algorithm needed for counting peaks on chart


Quote
Claire wrote in message ...
>Does anyone know of an algorithm to count no of peaks on a line/bar
>chart

I'm not sure I understand your question.  Depending on what you're
looking for, there are several possible approaches.

Have you considered thresholding?  Just plot (internally) a new
function that is 1 above a certain threshold, and 0 below the
threshold.  Runs of 1's could be considered a "peak."
You may need to smooth your data.

If you have a function, y = f(x), the max and min points are
found when the first derivative is zero.  That is, dy/dx = f'(x) = 0.
So, you could compute the first derivative and count the number
of times it's near zero -- but then you need to figure out if the
point is a min or a max.

Could you give a specific example of your data?  That might
help in the approach.

efg
_________________________________________
efg's Computer Lab:  http://infomaster.net/external/efg

Earl F. Glynn                 E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

Re:Algorithm needed for counting peaks on chart


Hi Earl,

The data is acquired from measuring high voltage discharges during a
single AC cycle with x being phase, y is discharge level. So values dont
follow a nice curve mathematically speaking but a human can look and
count the peaks easily.
I need to obtain the number of peaks from this data.
For example, to human eye the following would show 2 peaks, if you use
an algorithm that compared gradients then the program would see 4.

       .
       ..
     . ..         .
     . .. .      ...
     . .....     ....
   .........     ....
..........................

How could I smooth the curves in this ? My maths is rather stale.
Claire
Software engineer
Work clair...@robinson.timewarp.co.uk
home c...@hallworthhome.demon.co.uk

Re:Algorithm needed for counting peaks on chart


Quote
Claire wrote in message ...
>but a human can look and
>count the peaks easily.
>I need to obtain the number of peaks from this data.
>For example, to human eye the following would show 2 peaks

No noise assumption
------------------------------

I approximated you curve mathematically with the function
y = exp(-x) * | SIN(2X) |, for X = 0.0 to 3.0.

Take a look at the table below for the (X,Y) values.  The
slope is just (Delta Y)/(Delta X) -- actually Delta Y alone
would work.  The "Sign" function of the slope then returns
a value -1 or 1 (treat 0 as a 1).  So a peak is when the
slope goes negative after being positive -- a 1 to -1
transition in the Sign of the slope.  If you add up these
transitions, you'll get the number of peaks.

Note:  View the following tables with a fixed-width font

X       Y        Slope   Sign  1 -> -1         Assume No Noise
0.0     0.000
0.1     0.180    1.798    1
0.2     0.319    1.391    1       0
0.3     0.418    0.995    1       0
0.4     0.481    0.626    1       0
0.5     0.510    0.295    1       0
0.6     0.512    0.011    1       0
0.7     0.489   -0.222   -1       1
0.8     0.449   -0.402   -1       0
0.9     0.396   -0.532   -1       0
1.0     0.335   -0.614   -1       0
1.1     0.269   -0.654   -1       0
1.2     0.203   -0.657   -1       0
1.3     0.140   -0.630   -1       0
1.4     0.083   -0.579   -1       0
1.5     0.031   -0.511   -1       0
1.6     0.012   -0.197   -1       0
1.7     0.047    0.349    1       0
1.8     0.073    0.265    1       0
1.9     0.092    0.184    1       0
2.0     0.102    0.109    1       0
2.1     0.107    0.043    1       0
2.2     0.105   -0.013   -1       1
2.3     0.100   -0.058   -1       0
2.4     0.090   -0.093   -1       0
2.5     0.079   -0.117   -1       0
2.6     0.066   -0.131   -1       0
2.7     0.052   -0.137   -1       0
2.8     0.038   -0.135   -1       0
2.9     0.026   -0.128   -1       0
3.0     0.014   -0.117   -1       0

                                  2  peaks

Quote
>How could I smooth the curves in this ? My maths is rather stale.

What to do with noise in the data
--------------------------------------------

To approximate this mathematically, let's take the same
function as above, but add noise using a random number
generator.  A term 0.1*Rand(), which will be 0.0 to 0.1,
was added to each Y.

If you apply the "No Noise" approach from above, you'll
get that there are 7 peaks.  By Smoothing Y, by taking
the average of its value and its two nearest neighbors,
the smoothed curve shows only 2 peaks as before.
This is simply a moving average.

 X        Y    Y Smooth  Slope  Sign   1 -> -1
0.0     0.062
0.1     0.231
0.2     0.376   0.327
0.3     0.452   0.422    0.950   1       0
0.4     0.513   0.478    0.567   1       0
0.5     0.537   0.508    0.299   1       0
0.6     0.515   0.525    0.165   1       0
0.7     0.526   0.519   -0.061  -1       1
0.8     0.534   0.487   -0.316  -1       0
0.9     0.482   0.438   -0.489  -1       0
1.0     0.379   0.381   -0.570  -1       0
1.1     0.270   0.314   -0.674  -1       0
1.2     0.241   0.245   -0.683  -1       0
1.3     0.197   0.186   -0.596  -1       0
1.4     0.140   0.143   -0.432  -1       0
1.5     0.081   0.117   -0.251  -1       0
1.6     0.054   0.094   -0.234  -1       0
1.7     0.115   0.099    0.049   1       0
1.8     0.080   0.121    0.225   1       0
1.9     0.165   0.141    0.192   1       0
2.0     0.193   0.151    0.108   1       0
2.1     0.150   0.158    0.067   1       0
2.2     0.169   0.160    0.018   1       0
2.3     0.114   0.141   -0.192  -1       1
2.4     0.174   0.138   -0.026  -1       0
2.5     0.097   0.124   -0.137  -1       0
2.6     0.137   0.124   -0.003  -1       0
2.7     0.101   0.107   -0.170  -1       0
2.8     0.112   0.109    0.015   1       0
2.9     0.089
3.0     0.104
                                         2  peaks

So, how you handle the "noise" in your data
may be your biggest problem.  If your signal-to-noise
ratio is fairly high, you can smooth your data and get
the result you want.

If you'd like the Excel 97 spreadsheet with the
above data (and graphs), just drop me an E-Mail
and I'll send it to you.

efg
_________________________________________
efg's Computer Lab:  http://infomaster.net/external/efg

Earl F. Glynn                 E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

Re:Algorithm needed for counting peaks on chart


Quote
Claire wrote:

> Hi Earl,

> The data is acquired from measuring high voltage discharges during a
> single AC cycle with x being phase, y is discharge level. So values dont
> follow a nice curve mathematically speaking but a human can look and
> count the peaks easily.
> I need to obtain the number of peaks from this data.
> For example, to human eye the following would show 2 peaks, if you use
> an algorithm that compared gradients then the program would see 4.

>        .
>        ..
>      . ..         .
>      . .. .      ...
>      . .....     ....
>    .........     ....
> ..........................

I would suggest to filter the data with FFT or sliding window to
eliminate "grass". After that take the first derivative and check for
changing sign on it from + to -.

George

Re:Algorithm needed for counting peaks on chart


Quote
Denis Jeanroy wrote in message <6utubl$bs...@aristote.pratique.fr>...
>for a discrete series of points, a (too ?) simple solution might be
>something like

>for p:= 1 to points_count-1  do
>if (my_points[p]>my_points[p-1 ]) and (my_points[p]>my_points[p+1]) then
>    peak_found:=true;

>but this assumes that you want to detect every peak, not only big ones,
>while ignoring smaller ones.

IMO that's a viable solution, since you could always store the "height"
of each peak as you go, and rank the results, or filter out peaks that
are "too small" after you've finished. This allows the user to specify
the height at which peaks are interesting, rather than relying on a
hard coded algorithm. It would also be fairly simple to make your
algorithm less sensitive by comparing with a wider range of points
on either side. This would automatically allow the user to perform a
simple sensitivity analysis on the results by specifying different ranges.

Watch out for that algorithm, though, as it'll crash when it tries to
access my_points[p +  1] on the final iteration.

--
Jeremy Collins
Kansai Business Systems

Re:Algorithm needed for counting peaks on chart


In article <efptVCA+jIE2E...@hallworthhome.demon.co.uk>,
  Claire <clair...@hallworthhome.demon.co.uk> wrote:

Quote
> Does anyone know of an algorithm to count no of peaks on a line/bar
> chart
> Thanks in advance.
> --
> Claire

Claire, why not try a moving average.  It is very siple mathematically, and
will probably do the job quite well.  To jog your memory, create a duplicate
set of figures, but when making the duplicate, set each value to the average
of its own value plus the value before it and after it.  If this is not
effective, you may include more than one value before and after the current
value, (ie. a five reading moving average.)

eg.  NewValue[i] := (OldValues[i-2] + OldValues[i-1] + OldValues[i] +
OldValues[i+1] + OldValues[i+2])/5;

Of couse, with an AC cycle, you will end up with a slightly lower peak value.
I f this peak is important, then you will have to scale the answer, depending
on the amount of averageing and the sampling rate.

Hope this is useful,
Paul Harrington.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Algorithm needed for counting peaks on chart


Thank you everyone for your help :-)
Kind Regards - A very smiley Claire
Software Engineer
email : Cla...@HallworthHome.demon.co.uk

Other Threads