Python计算误码率,输入是0-1比特流矩阵和小数矩阵

简介: 本文提供了一个Python函数calculate_ber,用于计算两个NumPy矩阵表示的二进制信号和接收信号之间的误码率(BER),其中包括信号与接收信号的比较、误差计数以及BER的计算过程,并给出了具体的使用示例。

第二维度输入矩阵,是模型预测出来的概率,是小数值,大于0.5 的判断为1,小于0.5的判断为0.

import numpy as np

def calculate_ber(signal, received):
    """
    Calculates the bit error rate (BER) of two NumPy matrices representing
    a binary signal and the received signal, respectively.

    Parameters:
    -----------
    signal : numpy.ndarray
        A matrix of shape (m, n) representing the binary signal.
    received : numpy.ndarray
        A matrix of shape (m, n) representing the received signal.

    Returns:
    --------
    ber : float
        The bit error rate between the two signals.
    """
    # Ensure the two matrices have the same shape
    assert signal.shape == received.shape, "Error: matrices must have the same shape."

    # Calculate the number of bit errors
    num_errors = np.count_nonzero(signal != (received > 0.5))

    # Calculate the total number of bits
    total_bits = signal.size

    # Calculate the bit error rate (BER)
    ber = num_errors / total_bits

    return ber

例子

import numpy as np

# Define the two matrices
signal = np.array([[0, 1, 1, 0],
                   [1, 0, 1, 1],
                   [0, 1, 0, 0]])

received = np.array([[0.2, 0.8, 0.9, 0.3],
                     [0.7, 0.4, 0.6, 0.8],
                     [0.1, 0.6, 0.4, 0.2]])

# Calculate the BER
ber = calculate_ber(signal, received)

# Print the result
print("Bit error rate:", ber)
目录
相关文章
|
7天前
|
Python
【10月更文挑战第10天】「Mac上学Python 19」小学奥数篇5 - 圆和矩形的面积计算
本篇将通过 Python 和 Cangjie 双语解决简单的几何问题:计算圆的面积和矩形的面积。通过这道题,学生将掌握如何使用公式解决几何问题,并学会用编程实现数学公式。
128 60
|
17天前
|
Python
Datetime模块应用:Python计算上周周几对应的日期
Datetime模块应用:Python计算上周周几对应的日期
42 1
|
2月前
|
Python
python保存两位小数的几种方法,python2保留小数
python保存两位小数的几种方法,python2保留小数
99 2
|
1天前
|
Python
【10月更文挑战第15天】「Mac上学Python 26」小学奥数篇12 - 图形变换与坐标计算
本篇将通过 Python 和 Cangjie 双语实现图形变换与坐标计算。这个题目帮助学生理解平面几何中的旋转、平移和对称变换,并学会用编程实现坐标变化。
28 1
|
5天前
|
机器学习/深度学习 移动开发 Python
【10月更文挑战第11天】「Mac上学Python 22」小学奥数篇8 - 排列组合计算
本篇将通过 Python 和 Cangjie 双语讲解如何计算排列与组合。这道题目旨在让学生学会使用排列组合公式解决实际问题,并加深对数学知识和编程逻辑的理解。
41 4
|
5天前
|
数据可视化 Python
【10月更文挑战第12天】「Mac上学Python 23」小学奥数篇9 - 基础概率计算
本篇将通过 Python 和 Cangjie 双语实现基础概率的计算,帮助学生学习如何解决简单的概率问题,并培养逻辑推理和编程思维。
31 1
|
18天前
|
机器学习/深度学习 并行计算 大数据
【Python篇】NumPy完整指南(上篇):掌握数组、矩阵与高效计算的核心技巧2
【Python篇】NumPy完整指南(上篇):掌握数组、矩阵与高效计算的核心技巧
44 10
|
17天前
|
数据挖掘 iOS开发 MacOS
利用Python计算农历日期
利用Python计算农历日期
34 4
|
16天前
|
存储 自然语言处理 数据处理
使用Python计算多个集合的交集详解
使用Python计算多个集合的交集详解
16 1
|
18天前
|
索引 Python
【Python篇】NumPy完整指南(上篇):掌握数组、矩阵与高效计算的核心技巧1
【Python篇】NumPy完整指南(上篇):掌握数组、矩阵与高效计算的核心技巧
71 4