<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A压缩率</title>
</head>
<style>
body{width:95%; text-align:center;}
h1{font-size:18px; height:20px;}
font{text-align:left;}
.divImg{width:340px;}
.divR{float:right;width:140px;text-align:left;}
.divL{float:left;width:170px;}
.divMain{width:500px;}
</style>
<script language="javascript" type="text/javascript" src="jscharts.js"></script>
<script language="javascript" type="text/javascript">
var arrayTmp1=null;
function drawLine(constantA){
try{
var arrayTmp=new Array();
var x1=0.0;
var y1=0.0;
var x2=0.0;
var x3=0.0;
var changeIndex=-1;
var cy=1+Math.log(constantA);
var constanta=1/constantA;
var arrayK=new Array();
arrayTmp1=new Array();
for(var i=7;i>-1;i--){
x2=1/Math.pow(2,i);
x3=(x2-x1)/16;
arrayK.push([(pcmA(x2)-pcmA(x1))/(x2-x1),pcmA(x2)]);
y1+=x1*arrayK[7-i][0];
for(var x=x1;x<x2;x+=x3){
if(x>=constanta&&changeIndex<0){
arrayTmp.push([constanta,1/cy]);
changeIndex=7-i;
}
arrayTmp.push([x,pcmA(x)]);
arrayTmp1.push([x,y1+arrayK[7-i][0]*(x-x1)]);
}
x1=x2;
}
var myChart = new JSChart('chartcontainer', 'line');
myChart.setDataArray(arrayTmp, 'first_line');
myChart.setDataArray(arrayTmp1, 'second_line');
myChart.setBackgroundColor('#efe');
myChart.setAxisNameX('X');
myChart.setAxisNameY('Y');
myChart.setLineColor('#ff0f0f', 'first_line');
myChart.setSize(400, 320);
myChart.setTitle('');
myChart.setTooltip([constanta, '1/A', 'first_line']);
myChart.draw();
for(var i=0;i<arrayTmp1.length-1;i++){
arrayTmp1[i][0]=i;
}
function pcmA(x){
var y=0.0;
if(x<constanta){
y=constantA*x/cy;
}else{
y=(1+Math.log(constantA*x))/cy;
}
return y;
}
}catch(e){alert("输入的值只能包含数字和小数点!");}
}
function codePCM(value){
try{
var str="1";
var u=value/document.getElementById("constantU").value;
var isOK=false;
var result=0;
var tmp=0;
if(u<0){
str="0";
u=Math.abs(u);
}if(u==0){
str="0000000";
}else{
for(var i=0;i<arrayTmp1.length&&isOK==false;i++){
if(arrayTmp1[i][1]>u){
result=i;
isOK=true;
}
}
if(isOK==false)result=127;
for(var i=6;i>-1;i--){
tmp=Math.pow(2,i);
if(tmp<=result){
result-=tmp;
str+="1";
}else{
str+="0";
}
}
}
document.getElementById("decode1").value=str;
}catch(e){document.getElementById("decode1").value="";}
}
function decodePCM(value){
try{
var str=value;
var i=0;
var tmp="";
if(str.length<8)str+="00000000";
if(str.substr(0,1)=="0")tmp="-";
str=str.substr(1,7);
i=parseInt(str,2);
tmp+=(arrayTmp1[i][1]*(document.getElementById("constantU").value)).toFixed(5);
document.getElementById("code2").value=tmp;
}catch(e){document.getElementById("code2").value="";}
}
</script>
<body onload="drawLine(87.6)"><div class="divImg"><h1>A压缩律对应公式</h1><div class="divL"><img src="A.gif" width="170px" /></div><div class="divR"><br />x,归一化输入电压;<br />y,归一化输出电压;<br />A,压缩系数.</div></div>
<div class="divMain">自定义压缩系数A:<input type="text" value="87.6" id="constantA" onchange="drawLine(this.value)" style="width:40px;" />,允许最大输入电压U:<input type="text" value="5" id="constantU" style="width:20px;" />V</div>
<div class="divMain">PCM编码:<input type="text" value="3.4" id="code1" onchange="codePCM(this.value)" style="width:40px;" />>><input type="text" value="" id="decode1" disabled="disabled" style="width:60px;" />PCM译码:<input type="text" value="" id="code2" disabled="disabled" style="width:40px;" /><< <input type="text" value="10101010" id="decode2" onchange="decodePCM(this.value)" style="width:60px;" /></div><div class="divMain" id="chartcontainer"></div>
<div class="divMain">图中的红线表示理想压缩特性曲线,蓝线表示13折线压缩特性曲线。</div>
</body>
</html>