release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide

# Rockbox mail archive

Subject: Re: Log base 2

# Re: Log base 2

Date: Fri, 20 Sep 2002 00:42:13 +0200

For 0 < n <= 15, n = log2(x) :

8------------12------14---15
\ \ \
4------6---7 10---11 13
\ \ \
2---3 5 9
\
1

// rounded to superior : untested
int log2(int x) {
if (x >= (1<<8)) {
if (x >= (1<<12)) {
if (x >= (1<<14)) {
if (x == (1<<15))
return 15;
return 14;
} else if (x == (1<<13))
return 13;
return 12;
} else if (x >= (1<<10)) {
if (x == (1<<11))
return 11;
return 10;
} else if (x == (1<<9))
return 9;
return 8;
} else if (x >= (1<<4)) {
if (x >= (1<<6)) {
if (x == (1<<7))
return 7;
return 6;
} else if (x == (1<<5))
return 5;
return 4;
} else if (x >= (1<<2)) {
if (x == (1<<3))
return 3;
return 2;
}
return 1;
}

This code seems very long but only four comparisons are computed at most to
find out the result instead of a 15 iterations by shift method at worse
case.

I don't know if SH1 generate a good code for this.

Another one which computes the same result I think :

int log2(int x) {
int n = 0;
if (x >= (1<<8))
n += 8;
x >>= 8;
if (x >= (1<<4))
n += 4;
x >>= 4;
if (x >= (1<<2))
n += 2;
x >>= 2;
if (x >= (1<<1))
n += 1;
return n + 1;
}

----- Original Message -----
From: "Jeff Waltzer" <jeffwaltzer_at_yahoo.com>
To: <rockbox_at_cool.haxx.se>
Sent: Thursday, September 19, 2002 8:17 PM
Subject: Log base 2

>
> One could do a primitive approximation of a Log base 2 function by
> rotating the bits to find the left most location set bit. This would be
> good if all you need is a number from 0 to 15.
>
> --- Jack Peel <jhp_at_cypress.com> wrote:
> > Philipp Pertermann wrote:
> >
> > > From: "Jack Peel" <jhp_at_cypress.com>
> > > > > > Also could you put "clip" "lights" on it so that if
> > > the MP3 goes
> > > > > > to full scale it shows a different symbol and it hangs
> > > out longer
> > > > > > so that we know if the MP3 itself is clipping ?
> > > > >
> > > > > Yes, I could.
> > >
> > > Now I fiddled a bit with that. I don't like it much because
> > > it is incredibly unreliable. The 'quasi peak detector' of
> > > the mas is very 'quasi' but not so much a 'peak detector'.
> > > If we don't read the peak value exactly when it happens we
> > > miss it. I now installed a polling thread that reads out the
> > > mas quite often but still many peaks remain undetected. So
> > > the whole peak meter is a toy and not a reliable measuring
> > > tool.
> > >
> > > Phil
> >
> > Well, in theory the human ear cannot hear a clip if it is only a few
> > samples long, so we would only be interested in seeing a "clip" if
> > it were several samples long. But after thinking about it, seeing the
> > peak level indicators at full scale will tell you enough. And the user
> > can set how long they stick around already. It would be nice however
> > to do the "set until reset" function on the peak meters :) The only
> > problem is how do we easily reset them without having to go through
> > 3 levels of menus :) I guess you could just add it to one of the F2 or
> > F3
> > key functions from the wps screen.
> >
> > Any luck with the linear to log conversion ?
> >
> > I was thinking about the lookup table issue, and you could probably drop
> >
> > a couple of bits off of the number to reduce the size of the table and
> > we
> > would probably never be able to tell. Since it is a 15 bit number you
> > could
> > probably drop 5 bits and I would never be able to tell, maybe even 7
> > bits.
> > Another Idea is that we only really care about the accuracy when we are
> > close to the top so you might be able to "shape" the table to have
> > more accuracy at the top. The Idea is that -1 dB and -2 dB are VERY
> > difference, however -41 dB and -42 dB are not. It may however be
> > too much coding trouble to handle the shaped table.
> >
> > Just some Ideas :)
> >
> > Jack
> >
> >
>
>
> =====
> Whoever fights monsters should see to it that in the process he doesn't
become a monster.
> --Frederick Wilhelm Nietsche
>
> __________________________________________________
> Do you Yahoo!?
> New DSL Internet Access from SBC & Yahoo!
> http://sbc.yahoo.com