Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript converter for converting Decibels and Watts-per-square-meter.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<table>
<tr>
<td>To Decibels</td>
<td>Output</td>
</tr>
<tr>
<td><input id="decibelsInput" onchange="decibelsInputEvent()"></input></td>
<td><input id="decibelsOutput" disabled="true"></input></td>
</tr>
</table>
<table>
<tr>
<td>To WM</td>
<td>Output</td>
</tr>
<tr>
<td><input onchange="wmInputEvent()" id="wmInput"></input></td>
<td><input disabled="true" id="wmOutput"></input></td>
</tr>
</table>
</body>
</html>
Math.logBase = function(x, base) {
var baseLog = 0;
// don't use resources if they're already constants.
if(base == 10) {
baseLog = Math.LN10;
}
else if (base == 2) {
baseLog = Math.LN2;
}
else {
baseLong = Math.log(base);
}
return Math.log(x) / baseLog;
};
Math.log10 = function(x) {
return Math.logBase(x, 10);
};
function toDb(wm)
{
return 10 * (Math.log10(wm / Math.pow(10, -12)));
}
function toWm(db)
{
return Math.pow(10, db / 10) * Math.pow(10, -12);
}
var decibelsInput = document.getElementById("decibelsInput");
var decibelsOutput = document.getElementById("decibelsOutput");
var wmInput = document.getElementById("wmInput");
var wmOutput = document.getElementById("wmOutput");
function decibelsInputEvent() {
var db = toDb(decibelsInput.value);
if (isNaN(db)) {
decibelsOutput.value = "Error";
}
else {
decibelsOutput.value = db;
}
}
function wmInputEvent() {
var wm = toWm(wmInput.value);
if (isNaN(wm)) {
wmOutput.value = "Error";
}
else {
wmOutput.value = wm;
}
}
//var one = toDb(0.000316);
//console.log(one);
//var two = toWm(one);
//console.log(two);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.