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

Posted Better Bench

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python计算误码率,输入是0-1比特流矩阵和小数矩阵相关的知识,希望对你有一定的参考价值。

由chatGPT 生成,第二维度输入矩阵,是模型预测出来的概率,是小数值,大于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)

以上是关于Python计算误码率,输入是0-1比特流矩阵和小数矩阵的主要内容,如果未能解决你的问题,请参考以下文章

计算机网络概述---数据链路层

采用交织技术的目的以及实现方式

RSRS编译码理论与误码率性能matlab仿真和分析

RSRS编译码理论与误码率性能matlab仿真和分析

大端模式和小端模式

帧率码率比特率