<?php
    
//曼彻斯特解编码函数,参数$code为码元,$decode判断此动作是否为解码
    
function Manchester($code,$decode=0)
    {
        
$code_result="";
        if(!
$decode){
            for(
$i=0;$i<strlen($code);$i++){
                if(
$code[$i]=="0"){
                    
$code_result.="01";
                }else if(
$code[$i]=="1"){
                    
$code_result.="10";
                }else{
                    return 
ERROR;
                }
            }
        }else{
            if(
strlen($code)%2==1){
                return 
ERROR_DECODE;
            }else{
                for(
$i=0;$i<strlen($code);$i=$i+2){
                    if(
$code[$i]=="0" && $code[$i+1]=="0" || $code[$i]=="1" && $code[$i+1]=="1"){
                        return 
ERROR_MAN;
                    }else{
                        if(
$code[$i]=="0" && $code[$i+1]=="1"){
                            
$code_result.="0";
                        }else{
                            
$code_result.="1";
                        }
                    }
                }
            }
        }
        return 
$code_result;
    }
    
    
//米勒编解码函数,参数$code为码元,$decode判断此动作是否为解码
    
function Miler($code,$decode=0)
    {
        
$code_result="";
        if(!
$decode){
            
$tmp=$special="";
            for(
$i=0;$i<strlen($code);$i++){
                if(
$code[$i]=="0"){
                    if(
$tmp==""){
                        
$special="  系统默认前一个电平为1且为11";
                        
$tmp="11";
                        
$code_result.="11";
                    }else{
                        if(
$code[$i-1]=="0"){
                            if(
$tmp=='11'){
                                
$tmp="00";
                                
$code_result.="00";
                            }else{
                                
$tmp="11";
                                
$code_result.="11";
                            }
                        }else{
                            if(
$tmp[1]=="0"){
                                
$tmp="00";
                                
$code_result.="00";
                            }else{
                                
$tmp="11";
                                
$code_result.="11";
                            }
                        }
                    }
                }else if(
$code[$i]=="1"){
                    if(
$tmp==""){
                        
$special="  系统默认前一个电平为1且为01";
                        
$tmp="10";
                        
$code_result.="10";
                    }else{
                        if(
$tmp[1]=="0"){
                            
$tmp="01";
                            
$code_result.="01";
                        }else{
                            
$tmp="10";
                            
$code_result.="10";
                        }    
                    }
                }else{
                    return 
ERROR;
                }
            }
        }else{
            if(
strlen($code)%2==1){
                return 
ERROR_DECODE;
            }else{
                for(
$i=0;$i<strlen($code);$i=$i+2){
                    if(
$code[$i]=="0" && $code[$i+1]=="0" || $code[$i]=="1" && $code[$i+1]=="1"){
                        
$code_result.="0";
                    }else{
                        
$code_result.="1";
                    }
                }
            }
        }
        return isset(
$special) ? $code_result.$special $code_result;
    }
    
    
//CMI编解码函数,参数$code为码元,$decode判断此动作是否为解码
    
function CMI($code,$decode=0)
    {
        
$code_result="";
        if(!
$decode){
            for(
$i=0,$tmp=1;$i<strlen($code);$i++){
                if(
$code[$i]=="0"){
                    
$code_result.="01";
                }else if(
$code[$i]=="1"){
                    if(
$tmp%2==0){
                        
$code_result.="00";
                    }else{
                        
$code_result.="11";
                    }
                    
$tmp++;
                }else{
                    return 
ERROR;
                }
            }
        }else{
            if(
strlen($code)%2==1){
                return 
ERROR_DECODE;
            }else{
                for(
$i=0;$i<strlen($code);$i=$i+2){
                    if(
$code[$i]=="0" && $code[$i+1]=="0" || $code[$i]=="1" && $code[$i+1]=="1"){
                        
$code_result.="1";
                    }else{
                        if(
$code[$i]=="0" && $code[$i+1]=="1"){
                            
$code_result.="0";
                        }else{
                            return 
ERROR_CMI;
                        }
                    }
                }
            }
        }
        return 
$code_result;
    }
    
?>