Calculating dB; Broken DSP?

DSP related issues, mathematics, processing and techniques
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Calculating dB; Broken DSP?

Post by Perfect Human Interface »

Code: Select all

streamin x;
streamout out;

out = 20*log10(x);


Why does this DSP code output "400" and only "400"?
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Calculating dB; Broken DSP?

Post by Nubeat7 »

because x needs to be multiplied with 20 before you do log10
so it should be :

Code: Select all

out=log10(20*x);
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: Calculating dB; Broken DSP?

Post by tester »

Nubeat7 - are you sure? :-)
It's not log10 of (20*x) but 20 times log10(x).
Totally different formula.

For some reason you have to reverse the order. Or - use the multiplier externally. Ranges: >=0
Attachments
dB.fsm
(1.06 KiB) Downloaded 1396 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Calculating dB; Broken DSP?

Post by Perfect Human Interface »

tester wrote:

Code: Select all

streamin in;
streamout out;

out = log10(in)*20;


Thanks tester, this works fine.

Of course, this is mathematically nonsensical, but it works now.

It doesn't work with extra parenthesis in the original order either; I had tried that.
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Calculating dB; Broken DSP?

Post by trogluddite »

It's a bug in the code compiler. (If you connect a text box to the String output of the DSP primitive, you get to see the compiler's assembly code.)

In the original form of the equation, the 'log10(x)' result is put into in register xmm0. The '20' value is then read from memory - straight into the same register! - and then gets multiplied by itself. 20 * 20 = 400! :geek:

So, you did nothing wrong - the alternative version just does things in a different order that the compiler doesn't mess up!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Calculating dB; Broken DSP?

Post by Nubeat7 »

Nubeat7 wrote:because x needs to be multiplied with 20 before you do log10


thanks tester, AFTER the log10 :roll:
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Calculating dB; Broken DSP?

Post by Perfect Human Interface »

This code doesn't work either. :?

Code: Select all

streamin y;
streamin a;
streamin b;
streamin c;

streamout x;

x = -1*((a-y)*(b-c))/(a*(-2*b+c+y)+b*(c+y)-2*c*y);

Input values are
y = 3.58509
a = 0
b = 16
c = 1

Clearly not as easy to follow but... it just spits out 0. I did it out manually and it gives me 0.8125, which is exactly what I was looking for.

Even if I change all the input values it just spits out 0. :?
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: Calculating dB; Broken DSP?

Post by tester »

There is a limitation per lenght in "code", line lenght (some sort of asm translation limit). This is in manual, and I encountered it too. I would split this equation into smaller pieces, and thus - it should be easier to figure out which part is not working if that's still the case.

//edit:

Like this:

Code: Select all

    streamin y;
    streamin a;
    streamin b;
    streamin c;

    streamout x;

float asd1, asd2;
asd1=(a-y)*(b-c);
asd2=a*(-2*b+c+y);

x = -1*asd1/(asd2+b*(c+y)-2*c*y);


Works here.

But generally these parts can be done with regular prims (add, sub, multiply, etc).
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Calculating dB; Broken DSP?

Post by Nubeat7 »

this is what i thought too but i think its not the reason..
from user guide:

"One limitation of the code component is that you can only have 8 successive operations in an
expression. For example, the expression:
a = 0+1+2+3+4+5+6+7+9+10;
is not allowed because there are 9 addition operations in a row. You can easily get round this by
splitting the expression up into smaller sections using brackets ‘(‘ and ‘)’. For example:
a = (0+1+2+3+4)+(5+6+7+9+10);"

so after the code is very nested it shouldnt be a problem,
if you just put the -1* to the end of the code it works,

Code: Select all

x = ((a-y)*(b-c))/(a*(b*-2+c+y)+b*(c+y)-2*c*y)*-1;

if its at the beginning assembly tries to use register xmm999 which does the error, but maybe it is still because of the same problem, you have to think that there are only 8 registers in use so if there is to much to remember i think this can happen..
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: Calculating dB; Broken DSP?

Post by tester »

I would be careful anyway. Having too many things in one line - may lead to easy mistakes (human factor).
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply