Simple Problem: Detect peaks & Troughs in Sine wave in MAX (not msp)

Jun 21, 2013 at 11:12am

Simple Problem: Detect peaks & Troughs in Sine wave in MAX (not msp)

Like the title implies, I want to detect when a range of values is at a maximum and a minimum (as the data rises in falls in a simple sine wave function), and maybe store those values over time?

I know this is fairly simple but I’m still a Max novice – Help is much appreciated! Please let me know! Thanks

#253622
Jun 21, 2013 at 11:47am

the objects [peak] and [trough] could help. use [uzi] and its index output to look up the points the quickest way possible.

You can also store the data points into [zl group], then at the end, bang that group into [zl sort] to get the maximum and/or minimum. Sorting low-to-high by default, the minimum will be at the front of the list, get it with [zl slice 1]; the maximum will be at the back, get it with [zl ecils 1].

If you have more than 512 points use [zl 2000 group] (and the same for the other zl objects you’re using) so they know to process longer lists than the default. Note that the list length maximum is entered BEFORE the zl type: [zl 2000 group], not [zl group 2000]. So enter whatever your maximum possible list will be.

The [zl] help file takes some study but will open up a lot of possibilities…

#253635
Jun 21, 2013 at 3:09pm

Good advice – Ironically i never thought to look up if there was an object for peak/troughs. Very helpful, though I haven’t quite figured out how to use uzi’s index format in that way.

Additionally, do you happen to have any tips on a way of using the changes in direction as a trigger? I tried using the [change/~] objects to no avail.

#253665
Jun 21, 2013 at 8:28pm

the [uzi] index is just to look up the data points in your [table] or [function] or wherever your sine wave is. Take the output of what you look up and send it to both [peak] and [trough] and you should get the max/min of the whole list.

For changes in direction, just run the output of your data (one number at a time) into both inlets of [-], making sure the left inlet is triggered FIRST. (look up [trigger] for help on message order execution…hugely important in Max.) This way, you get the difference between the current data point and the previous one. Then use [> 0] to [< 0] to test for positive or negative, and you'll see your direction changes. There are many other ways to do it too…you could use [if], [expr], etc.

#253688
Jun 22, 2013 at 2:01am

Hi
In the past I have relied on change +/- to grab peaks and troughs; but only because I can never get peak and trough to do the following; @seejay, have you got time to demonstrate how peak and trough would simplify this solution?

<code>

– Pasted Max Patch, click to expand. –

</code>

Brendan

#253701
Jun 23, 2013 at 10:02pm

Cool patch!

I think the issue is that you need to set a limit for how many numbers you want to sample. [peak] and [trough] will give the values you’re looking for, but you need to end the testing at some point to report the values.

#253819

You must be logged in to reply to this topic.