Finding a peak in data

Go To Last Post
28 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is a graph of 37 data points taken once every 5 minutes. I'm trying to find the point where the slope goes negative after the peak. This is easy when you have all the data in front of you like this, but I need to stop a process the sample after the peak is detected.

I'm doing this all on a tiny 24, so I don't have room for log functions. I am keeping an average value and I'm comparing the slope of the current data to the slope of the average, and that seems to be good, but when I change sensors, the Y-scale changes, and that messes up my assumptions, so I have to be unit-less in my calculations.

I guess I have some ram that I could keep some data other than the average to work with.

Anybody have some good ideas?

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Based upon your verbal description, I would stop the process at sample 4. I think you need to describe your algorithm better ... at least for my tiny brain.

Cheers,

Ross

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you keep a track of your current highest value, then if there is a downturn, then you've got the trigger. You could also average the delta, then trigger if you get a negative delta of sufficent magnitude.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you want to find one point, or nine (more or less), in the data you've shown? What constitutes a local maximum?

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Based upon your verbal description, I would stop the process at sample 4. I think you need to describe your algorithm better ... at least for my tiny brain.

That's what I'm grappling with. The bump seems so obvious, but you caught on to my issue as there are several valleys after peaks.

I want to find one point. The point around sample 32 is what I'm after.

I was just considering an array of the last 10 samples. This would get rid of the anomaly at the beginning.

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, so you are actually looking for a downturn after a prolonged period of increases. Probably achievable with a logical flag, a variable to define minimum increase/decrease step sizes (crude noise filter) and a counter.

Cheers,

Ross

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it not simply:

if (SampleNplus1 < SampleN) {
 // downturn
}

or if you want to ignore the small ripples (which each involve a small downturn) then:

if ((SampleN - SampleNplus1) > big_number) {

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there always a plateau before the peak of interest?

Is the peak of interest always significantly (X%) greater than the plateau?

If so you could also watch for the plateau, then watch for the peak to follow, then watch for the fall or change in slope, (+ to -).

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You keep 2 averages... one over maybe 16 samples and one over maybe 4 samples. Trigger when fast avg this pass < fast avg last pass and fast avg > slow avg + size of hump.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not sure you've fully thought it through (nor have I).....but if you only have points 0-10 (since as you say they are coming in point-by point, preventing a post analysis) then you MUST say SOMETHING is currently the peak value. So why not just track the all-time peak as they come in point-by point??

At some time you may want to auto reset the all-time peak, but you will need some logic/rules to decide when to do this.

Now, if you were trying to PREDICT the peak as data came in point-by-point, you'd really have something to chew on--looking at slopes, valleys, etc. You could perform a multitude of calculations, just like the stock market pros do.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
I'm not sure you've fully thought it through (nor have I)... So why not just track the all-time peak as they come in point-by point?
Because at sample 4 in the example you would declare the peak passed and terminate the process too early.

If there is going to be some relatively minor ups/downs in the parameter being monitored, you must allow for this so that the control does not become too sensitive.

Cheers,

Ross

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
stock market pros

Another all time great Misnomer .

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:
Quote:
stock market pros

Another all time great Misnomer .

JC

Maybe not. Here in Ozland, a "pro" is also slang for a woman of the night ... or someone who would sell anything, even themselves, for money.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As already stated you can't simply look for a dip in your value. First you have to characterize what constitutes a peak. Only then can you trigger based on a fall. Alternatively you if you can characterize the falling slope, you can trigger once you have two or more samples that indicate the desired negative slope. The more samples you use, the less likely you are to get a false trigger.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Give me the data set. I'll run the posted fast and slow avg algo above on it. It will trigger whenever I want it to.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I write code that triggers (or anything else) when I want it to, I don't need no stinkin' data.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

-

Stealing Proteus doesn't make you an engineer.

Last Edited: Thu. May 7, 2009 - 04:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Bob, I sent you the data.

Ross, you have the right idea. The little changes need to be ignored.

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

if ((SampleN - SampleNplus1) > big_number) {

+1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For what its worth, here's what I came up with that seems to be working quite well.

Taking a hint from calculating standard deviation, I took a "population" of 10 samples. I summed them up and divided by 10 to come up with the mean. I then subtracted the mean from the current sample and squared the result (the magenta line). This exaggerates the peak quite nicely. This is where the statical math stopped and intuition took over (to avoid the square root function.

I set the threshold by dividing the mean by 10 and then squaring it (the blue line).

My logic is then:

if((new_data > mean) && (new_data < old_data))
     trigger_event();
     old_data = new_data;

What I like about this is that the square function takes care of negative values so the sensor can work in any position now.

It also sets the threshold and data relative to the range of data the sensor is kicking out. So, I don't have any absolute comparisons. If the mean is 600 or 6000 the equation works.

My tiny 24 has only 2 words left.

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

My tiny 24 has only 2 words left.

LOL.

One of my professors had a saying: "Any program can be made one cell shorter."

Dragging out another chestnut, "Engineering is the art of making what you need from what you have." So if you are making one, why not use say Mega48 with multiplier? Same price (qty. 100). Same/less board space in TQFP. DIP a hair longer--who cares? More resources all around.

That said, I have several "mature" production apps that were at a comfortable flash/EEORM usage level, and repeated "just one more feature" over the years has squeezed them quite tight.

From the pictures and the discussion it looks a lot like the "Bollinger bands" that I see on stock graphs; I haven't looked at how those lines are drawn.

Hmmmm--looking at your last "results" graph, what happens if you add a few more samples from 40-60 at the 600 level? the secondary rise in the magenta line lookes like it may turn into some kind of ringing, and give a secondary "false" hit around 60?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

So if you are making one, why not use say Mega48

If you are only making one then you might as well go straight to a mega2560 ;-)

(actually, these days perhaps an atxmega128A1 might be an even more "fun" prospect? How long, I wonder, before someone makes a decent dev board or will Atmel finally make the Xplain available to mortals?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are 8 of these sensors in each unit. The plan is 1000 units per year. Cost is king.

Mega48 is no good. I would be using that if I didn't need the differential A/D. Thank goodness there is a Tiny44 and 84 I can move up to.

The reason for the up-tick at the end is that the values have gone negative, and the square function changes that. There is only one bump per cycle.

This is a liquid level sensor. The peak is where the level is centered on the sensor. Once the peak is found, I don't have to continue sampling.

If I were building only one of these I would be using an $80 sensor rather than designing my own $8 sensor.

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

the differential A/D

Ah, yes. There are a couple things that the '48 series doesn't have: high-speed PWM (is that on >>any<< Mega?), and differential/gain on the ADC. I was frankly quite surprised that when the '48 series came out as the "better Mega8", or when the P-chip refinement came out, that the feature was not included--most (many?) Megas have it.

Quote:

If I were building only one of these I would be using an $80 sensor rather than designing my own $8 sensor.

LOL.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
How long, I wonder, before someone makes a decent atxmega128A1 dev board?

I'm game. Do you think we could sell them here?

If you help me define "decent" I'll spin a board.

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

How long, I wonder, before someone makes a decent atxmega128A1 dev board?

(IMO) In the long [pun intended] run, a centipede is overkill for those that want an Olimex-type chip+basics+signal headers cheapie to add the app peripherals and stuff into a case--e.g., signal generator or sequencer for a test rig. For that type of "everything already on it", the STK600 comes close but without the integrated displays and commo links of some of the fancier AVR8 dev setups.

Once the A3 & A4 become available as the A1 is now, then I think you will see more offerings. Too bad the A4 doesn't come in DIP or a "generic" 40-pin DIP board could be used. (There was a mention a while back about an AVR on a very generic PIC dev board, Olimex-type with a breadboard area. On that Microchip board it appeared that the Gnd, Vcc, Ref, and the like could be easily routed to the needed pin(s).)

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
Quote:

the differential A/D

Ah, yes. There are a couple things that the '48 series doesn't have: high-speed PWM (is that on >>any<< Mega?)

Though technically not "mega" AVR's IIRC the "90PWM" AVR's have the high speed PWM feature.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, here's my program that runs a 16 and 4 sample rolling avg across the data. It doesnt start looking for a peak till sample 16 (lets the slow avg stabilize), and it finds a peak at sample 34

Attachment(s): 

Imagecraft compiler user