Page 1 of 1

Bitwise usage for IF

PostPosted: Mon Jan 25, 2016 8:14 am
by Kirill_Neoris
Hello!

Can you give me an example how to organize the expression like this in DSP code?

If asum1 <=1 then x = 1.0
else x = asum1

Pardon my free "code" ;)

In practice I'm trying to make a module that will be keeping a sum of three inputs below 1.0
Dancing around "&" gives me the asum1 or zero on the variable. I can't figure out how to make it properly. I'll add the code later when i'll be at home.

Re: Bitwise usage for IF

PostPosted: Mon Jan 25, 2016 8:43 am
by martinvicanek
I can think of three possibilities:
Code: Select all
x = max(asum1,1.0);

Code: Select all
x = asum1 + (1.0 - asum1)&(asum1 <= 1);

Code: Select all
x = 1.0 + (asum1 - 1.0)&(asum1 > 1);

And a 4th one which may be easier to grasp but has more operations:
Code: Select all
x = 1.0&(asum1 <= 1) + asum1&(asum1 > 1);

Re: Bitwise usage for IF

PostPosted: Mon Jan 25, 2016 8:56 am
by Kirill_Neoris
Thank you! I'll check this out when will be at home!
Extra thanks for the quick answer!

Re: Bitwise usage for IF

PostPosted: Mon Jan 25, 2016 11:08 am
by Kirill_Neoris
martinvicanek wrote:I can think of three possibilities:
Code: Select all
x = max(asum1,1.0);

Code: Select all
x = asum1 + (1.0 - asum1)&(asum1 <= 1);

Code: Select all
x = 1.0 + (asum1 - 1.0)&(asum1 > 1);

And a 4th one which may be easier to grasp but has more operations:
Code: Select all
x = 1.0&(asum1 <= 1) + asum1&(asum1 > 1);


Code: Select all
streamin lfo1;
streamin lfo2;
streamin lfo3;
streamout lfo1n;
streamout lfo2n;
streamout lfo3n;
streamout k;
streamout sum;

float lfosum;
float k1;
float x;

lfosum = lfo1+lfo2+lfo3;
x = 1.0&(lfosum <= 1) + lfosum&(lfosum > 1);
k1 = 1/x;

k = k1;
sum = lfosum;

lfo1n = lfo1 * k1; //this is to make the sum of amplitudes
lfo2n = lfo2 * k1; //equal or lower than 1.0
lfo3n = lfo3 * k1;


I've tried the last right away and it works perfectly! :)
My version was:
Code: Select all
x = (lfosum>=1) & 1.0;


I still can't figure out how your code works... Maybe it need a time to settle down in my head...
Does the "plus" means it's and additional condition?

Re: Bitwise usage for IF

PostPosted: Mon Jan 25, 2016 2:51 pm
by MyCo
Version 3.0.9 has the ternary operator build in, so this should work:
Code: Select all
x = (asum1 <=1) ? 1.0 : asum1;