Page 1 of 2

Maximum value from mem buffer?

Posted: Wed Dec 10, 2014 4:45 pm
by Youlean
Is there any way to get maximum value from a mem buffer with stream connection (not green)?

I need to find max value from 1024 buffer, and so far I made it with stacking one sample delays, but CPU usage is insane...

Re: Maximum value from mem buffer?

Posted: Wed Dec 10, 2014 6:53 pm
by KG_is_back
Connect the buffer to wave read prim with index counter and connect that to this:

Code: Select all

streamin in;
streamout out;
out=max(out,in);


connect that to analyzer prim, and read the last value from the output float array (the number of values to process should be equal to the size of the buffer).

Alternatively you can use memin input and put the index counter into the code block dirrectly.

Re: Maximum value from mem buffer?

Posted: Wed Dec 10, 2014 8:01 pm
by Youlean
Well, I don't think that this will work...

I need to get new max value every sample from 32 samples buffer.
Is this possible with ASM, or ruby?

BTW, thanks KG for helping me out...

Re: Maximum value from mem buffer?

Posted: Wed Dec 10, 2014 11:24 pm
by KG_is_back
it is possible with code component using loop if the buffer is array

Code: Select all

c=0;
maximum=0;
loop(32){
maximum=max(maximum,array[c]);
c=c+1;
}

Re: Maximum value from mem buffer?

Posted: Wed Dec 10, 2014 11:55 pm
by Youlean
KG_is_back wrote:it is possible with code component using loop if the buffer is array

Code: Select all

c=0;
maximum=0;
loop(32){
maximum=max(maximum,array[c]);
c=c+1;
}


Sorry KG, but can you write the whole code, I don't understand how to implement this... :(

Re: Maximum value from mem buffer?

Posted: Thu Dec 11, 2014 12:15 am
by KG_is_back
Oh sorry, I think I'm missing the point. So there is something happening with the buffer (you are writing to it at some place in the schematic using write mem prim or asm) in every sample and each time you then want to find the maximum value in that buffer each time, every sample. Is that right?

Re: Maximum value from mem buffer?

Posted: Thu Dec 11, 2014 12:49 am
by Youlean
KG_is_back wrote:Oh sorry, I think I'm missing the point. So there is something happening with the buffer (you are writing to it at some place in the schematic using write mem prim or asm) in every sample and each time you then want to find the maximum value in that buffer each time, every sample. Is that right?

Yes.

Re: Maximum value from mem buffer?

Posted: Thu Dec 11, 2014 1:10 am
by KG_is_back
Ok! that needs ASM to work. This should do it...

Re: Maximum value from mem buffer?

Posted: Fri Dec 12, 2014 5:52 pm
by Youlean
Sorry for late response..

Thanks a lot, but this is not what I am looking for...

I am looking for something that will emulate "finding max from 16 samples" in my example with much less CPU usage.

Hold max custom amount of samples will not work here, as it is slightly different from finding max from 16 samples...

BTW, is there a way to have really optimized fixed delay (that will delay signal for fixed amount of time). To be low on CPU as one sample delay?

Thanks!

Re: Maximum value from mem buffer?

Posted: Fri Dec 12, 2014 10:16 pm
by KG_is_back
Oh, now I get it... this can be done in code component:

Code: Select all

streamin in;
streamout out;
float buf[16];
float c,d;

buf[d]=in;
d=d+1;
d=d&(d<16);
c=0;
out=0;
loop(16){
out=max(out,buf[c]);
c=c+1;
}

It can be optimized in assembly, but the functionality is there...

As for the delay:


Code: Select all

streamin in;
streamout out;

int c=0;
float mem[1024]; //this can be of any size

mov eax,c[0];
add eax,-16;
and eax,16 383; //looping mask - this will force "c" to cycle in 0-16383 range. Works only with powers of two minus one
mov c[0],eax;

movaps xmm0,in;
movaps mem[eax],xmm0;
add eax,{delay*16};
and eax,16 383;
movaps xmm0,mem[eax];
movaps out,xmm0;

This should be the most efficient way to do the fixed delay. Just copy-pase this code into assembler and insert your desired delay multiplied by 16 on the spot "{delay*16}" I have made the buffer fairly big, you may opt to use smaller buffers (it will not make much difference though) if you want - in that case replace every "and eax,16 383;" with 16*buffersize.
You may even easily modify it, to output multiple delays of the input. Simply copy-paste last four lines multiple times - keep in mind the delays will be differential (every next computed delay is computed from the position of the previous one)