# -*- coding: utf-8 -*-
"""
Created on Fri Nov  7 18:57:40 2025

@author: AKourgli
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import firwin, lfilter, hilbert

# Paramètres
Fe = 100e3     # fréquence d'échantillonnage
Te = 1/Fe
N = 4096
t = np.arange(0, N*Te, Te)
f_c = 10e3     # porteuse

# Signal message : fréquences bien espacées
f1, f2, f3 = 500, 1500, 2500  # en Hz
m = np.cos(2*np.pi*f1*t) + 0.7*np.cos(2*np.pi*f2*t) + 0.5*np.cos(2*np.pi*f3*t)

# 1. AM avec porteuse (DSB–AM)
ka = 0.5
s_AM = (1 + ka*m) * np.cos(2*np.pi*f_c*t)

# 2. AM sans porteuse (DSB–SC)
s_DSBSC = m * np.cos(2*np.pi*f_c*t)

# 3–4. BLU et BLI par transformée de Hilbert
m_h = np.imag(hilbert(m))
s_BLU = m * np.cos(2*np.pi*f_c*t) - m_h * np.sin(2*np.pi*f_c*t)  # USB
s_BLI = m * np.cos(2*np.pi*f_c*t) + m_h * np.sin(2*np.pi*f_c*t)  # LSB

# 5. VSB (Bande latérale vestigiale)
# Filtrage partiel d’une BLU à partir de DSB–SC
b = firwin(301, (f_c + 1000)/(Fe/2))  # vestige sur ~1 kHz
s_VSB = lfilter(b, 1, s_DSBSC)

# Fonction spectre
def spectrum(sig):
    N = len(sig)
    f = np.linspace(-Fe/2, Fe/2, N)
    S = np.fft.fftshift(np.fft.fft(sig)/N)
    return f, (np.abs(S)/np.max(np.abs(S)) + 1e-12)

# Tracé des spectres
plt.figure(figsize=(12,9))
signals = [s_AM, s_DSBSC, s_BLU, s_BLI, s_VSB]
titles = ['AM avec porteuse (DSB–AM)', 'AM sans porteuse (DSB–SC)',
          'BLU (USB)', 'BLI (LSB)', 'VSB']

for i, s in enumerate(signals):
    f, S = spectrum(s)
    plt.subplot(2,3,i+1)
    plt.plot(f/1e3, S)
    plt.title(titles[i])
    plt.xlabel('Fréquence (kHz)')
    plt.ylabel('Amplitude (dB)')
    plt.grid(True)
    plt.xlim([0, 20])  # centré autour de 10 kHz

plt.tight_layout()
plt.show()
