matlab中biterr函数方法详解

作用以及用法

  • 计算二进制比特流的汉明距以及误码率, biterr Compute number of bit errors and bit error rate.
  • 语法形式如下:
    • [num,ratio] = biterr(X,Y) ,
    • num表示参数X和Y(十进制数)对应二进制比特流对应位置二进制数不相同的位数之和,称为汉明距
    • ratio表示两者的汉明距占输入参数中最大值以二进制表示的最少比特位数的比例,称为误码率。
  • 有点绕,举例如下
>>> help biterr
>>> 
    [NUMBER,RATIO] = biterr(X,Y) compares the unsigned binary representation of
    the elements in the two matrices X and Y.  X and Y must be logical or
    integer valued. The number of differences in the binary representation is
    output in NUMBER.  The ratio of NUMBER to the total number of bits used in
    the binary representation is output in RATIO. The same number of bits is
    used to represent each element in both X and Y. The number of bits used is
    the smallest number required to represent the largest element in either X or
    Y.  When one of the inputs is a matrix and the other is a vector the
    function performs either a column-wise or row-wise comparison based on the
    orientation of the vector.

  • 举例说明
    Examples 1:
>> [num, error] = biterr(85,118) ;
    num = 3
    error = 0.4286
    
    Examples 2:
  >>        A = [1 2 3; 1 2 2];
  >>        B = [1 2 0; 3 2 2];
 
  >>      [Num1, Rat1] = biterr(A,B)        
   Num1 = 3
   Rat1 = 0.25

 Examples 3:
>>  A = [1 2 3; 1 2 2];
    B = [1 2 0; 3 2 2];
>> [n,r] = biterr(A,B,4) %% 
	n = 3
	r = 0.1250

  • exam1中的参数均为scale标量,将其转为8位二进制为"01010101", “01110110” ,但以二进制表示两者参数的最大数所需要的最少比特位数为7,即"1010101", “1110110” ,对应文档中The number of bits used is the smallest number required to represent the largest element in either X or Y. 显然,两者不等的个数有3位,即汉明距=3, 误码率为3/7, 约等于0.4286
  • exam2中的参数均为向量,确定向量所有元素中最大数对应的最少二进制bit位数,最大元素3用二进制表示最少位数=2,所以A和B二进制分别表示为"01 10 11 01 10 10"、“01 10 00 11 10 10”,显然,汉明距=3,误码率为3/12 = 0.25
  • exam3中的参数为矩阵,当K未指定时,元素表示的bits位数是通过所有向量元素中最大数对应的最少bit位数确定,若指定,则等于K,因此,K=4, A和B二进制分别表示为"0001 0010 0011 0001 0010 0010"、“0001 0010 0000 0011 0010 0010”,显然,汉明距=3,误码率为3/24 = 0.125

汉明距

  • 汉明距指的是这两个数对应二进制位流在相同位置不等的总和
#utf-8
def hammingDistance(x, y):
       """
       :type x: int
       :type y: int
       :rtype: int
       """
       return bin(x^y).count("1")
   
def hanmingDist( bitstream1, bitstream2):
       """
       input binarized string 
       output distance
       """
       arr_1 = np.asarray([int(i) for i in bitstream1])
       arr_2 = np.asarray([int(i) for i in bitstream2])
       count = np.count_nonzero(arr_1 != arr_2)
       return count

if __main__ == "__name__":
	bit1= "01010101" # = 85
	bit2= "01110110" # = 118
	print(hanmingDist(bit1, bit2)) # 3
	x1 = int(bit1,2) #转十进制
	x2 = int(bit2,2) 
	print(hammingDistance(x1,x2)) #3
	
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值