calculations in awk? (log10?)

Quote:

>> I have written an awk script that searches a file for a

>> series of numbers, finds them, and does a simple calculation on

>> them (division and addition)...However, I then need the dB value

>> of this number which requires a log10 calculation..(eg: noise figure

>> in dB calculated from the noise power ratios). As far as I can

>> tell, only ln is available...

>Standard solution for calcation with a base 'x':

>x-log(number) = ln(number) / ln(x)

>So in your case:

>10-log(number) = ln(number) / ln(10).

The solution is to create your own log10 function

function log10(number){return log(number)/log(10.0)}

then you can use this function whenever you want.

See the man page for a description of this

man awk

man gawk

here's one example:

gawk 'function log10(number){return log(number)/log(10.0)}

{print "The log of", $1, "is", log10($1)}' infile

For this infile:

10

20

100

It produces:

The log of 10 is 1

The log of 20 is 1.30103

The log of 100 is 2

Chuck Demas

Needham, Mass.

--

Eat Healthy | _ _ | Nothing would be done at all,

Die Anyway | v | That no one could find fault with it.