Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Bitwise usage for IF

DSP related issues, mathematics, processing and techniques

Bitwise usage for IF

Postby Kirill_Neoris » Mon Jan 25, 2016 8:14 am

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.
Kirill_Neoris
 
Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

Re: Bitwise usage for IF

Postby martinvicanek » Mon Jan 25, 2016 8:43 am

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);
User avatar
martinvicanek
 
Posts: 1328
Joined: Sat Jun 22, 2013 8:28 pm

Re: Bitwise usage for IF

Postby Kirill_Neoris » Mon Jan 25, 2016 8:56 am

Thank you! I'll check this out when will be at home!
Extra thanks for the quick answer!
Kirill_Neoris
 
Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

Re: Bitwise usage for IF

Postby Kirill_Neoris » Mon Jan 25, 2016 11:08 am

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?
Kirill_Neoris
 
Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

Re: Bitwise usage for IF

Postby MyCo » Mon Jan 25, 2016 2:51 pm

Version 3.0.9 has the ternary operator build in, so this should work:
Code: Select all
x = (asum1 <=1) ? 1.0 : asum1;
User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany


Return to DSP

Who is online

Users browsing this forum: No registered users and 19 guests