From: "Earl F. Glynn" Subject: Re: Algorithm needed for counting peaks on chart Date: Wednesday, September 30, 1998 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 >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 Earl F. Glynn Overland Park, KS USA