constantly report the median of last second flow of numbers

personal_username's icon

HI all,

I'm trying to solve this problem, which looked simple to me, but I can't solve it.
I have a flow of numbers coming at irregular intervals and I'd like to calculate the median for istance of the past 1 second. I know how to return the median value every second, but what i can't figure out is how i can have the median not only giving me result every second, but constantly.
If something happens at a millisecond rate, I'd like to have the idea istant by istant what the last second was. And i can't solve this problem.

I tried several obkects like [bucket] [pak] [zl median] [accum], but i can only have them reporting me the median of the (very quick) flow "every" second, not instantly (like 200 msec).

Do you have any tip?
Thanks!

Chris Muir's icon

Post your patch annotated to show what happens, and what you want to have happen.

personal_username's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Thanks Chris, here's my code:

Chris Muir's icon

Might the mean work as well for you?
Are there known temporal bound for how the data comes in? (min rate & max rate)

Chris Muir's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Here's a stab at it;

personal_username's icon

p
e
r
f
e
c
t

simple, efficient, clear
thanks a lot!

Floating Point's icon
Max Patch
Copy patch and select New From Clipboard in Max.

using quickthresh is simpler:

Chris Muir's icon

Terry, read the problem statement again. personal_username wants the median over a one second period, but wants it reported at 200 ms intervals, with each report covering the previous second.

Roman Thilenius's icon

if you really need a timing accuracy of 1ms t here is no other option but to
go from a [metro 1] - but i wouldnt do that. just try 25 or 40 or 200 ms and
see if you like the output.

Yoann's icon

Not sure if it's the question here, but I recently discover [meandev], from A.Harker
(http://www.alexanderjharker.co.uk/Software.html)
It reports mean and deviation over time. One can set 4 different time ranges.

Floating Point's icon
Max Patch
Copy patch and select New From Clipboard in Max.

ok Chris-- so here is my modified patch using quickthresh (note it is no longer simple)...

Chris Muir's icon

Nice, and probably more efficient than mine. I'll try to get around to benchmarking them tonight.

Christopher Dobrian's icon
Max Patch
Copy patch and select New From Clipboard in Max.

And there's always our old friend bag...

Chris Muir's icon

I _really_ have to add bag in my bag of tricks.

Floating Point's icon

but as yoann points out, [meandev] will easily do it all in one external

Chris Muir's icon

But does meandev calculate the median? Max already has mean.

Floating Point's icon

yes i'm evidently mildly dyslexic-- looking at its help file it talks about mean of means, which I read as median of medians (a statistical procedure), so anyway chris' (topher) bag approach is probably the one to use

personal_username's icon
Max Patch
Copy patch and select New From Clipboard in Max.

wow, I didn't see your last posts guys! very very nice solution [bag], I didn't know it either, and looks like it is the best memory-efficient and simpler tool, great!
I'll have a look at [meandev] now, but this solution Cristopher is really good for me!
So, i assume if I want to check different time spans all I have to do is to change the pipe attribute, so if I want to track 30 secs all I have to do is this:

AlexHarker's icon

Hi,

To clarify meandev does not calculate the median ever, only means and deviations. The mean of means business is something that was probably useful at the time, but I forget what for (this is one of the oldest object in the package dating back over 5 years now I think and when I updated it more recently I just left all the old stuff in there).

I could update it to do medians some time, but no promises especially as there are some good max solutions here.

However, a word of caution:

zl will only allow up to 256 values unless you specify otherwise.

Below is my altered solution from christopher's. This will work such that whenever something goes in or comes out of the bag the output is updated, without needing a metro 1 (which is a lot of unnecessary calculation).It also allows lists of up to 4096 values.

Max Patch
Copy patch and select New From Clipboard in Max.

Alex

AlexHarker's icon

In case it wasn't clear above the idea is the output should always relate to the last second exactly in my solution (assuming I got it right - I've only quickly tested)...