Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2014 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/10221295 to your computer and use it in GitHub Desktop.
Save anonymous/10221295 to your computer and use it in GitHub Desktop.
<!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;
if(base == 10) {
baseLog = Math.LN10;
}
else if (base == 2) {
baseLog = Math.LN2;
}
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