# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 23:16:38 2025

@author: AKourgli
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.special import erfc

# Définition de la fonction Q
Q = lambda x: 0.5 * erfc(x / np.sqrt(2))

# Plage de Eb/N0 (dB)
EbN0_dB = np.arange(0, 16, 1)
EbN0 = 10**(EbN0_dB/10)  # Conversion linéaire

# Calcul des BER pour chaque groupe
ber_optimal = Q(np.sqrt(2 * EbN0))  # Groupe 1
ber_3dB_loss = Q(np.sqrt(EbN0))     # Groupe 2
ber_6dB_loss = Q(np.sqrt(0.5 * EbN0))  # Groupe 3
ber_bipolar = 2* Q(np.sqrt(EbN0))   # Groupe 4

# Pour les modulations M-Aires
ber_4level = (3/4) * Q(np.sqrt((4/5) * EbN0))  # 4-Aires
ber_8level = (7/12) * Q(np.sqrt((2/7) * EbN0))  # 8-Aires

# Tracé
plt.figure(figsize=(10, 6))
plt.semilogy(EbN0_dB, ber_optimal, 'b-', linewidth=2, label='NRZ/RZ Polaire, Manchester')
plt.semilogy(EbN0_dB, ber_3dB_loss, 'r--', linewidth=2, label='NRZ/RZ Unipolaire, Miller')
plt.semilogy(EbN0_dB, ber_bipolar, 'g:', linewidth=2, label='AMI, HDBn, RZ polaire')
plt.semilogy(EbN0_dB, ber_4level, 'm-.', linewidth=2, label='4-Aires')
plt.semilogy(EbN0_dB, ber_8level, 'k-.', linewidth=2, label='8-Aires')

plt.xlabel('Eb/N0 (dB)')
plt.ylabel('Bit Error Rate (BER)')
plt.title('BER des Codes en Ligne en Fonction du SNR')
plt.grid(True, which="both", ls="--")
plt.legend()
plt.ylim(1e-6, 1)
plt.xlim(0, 15)
plt.show()

