# -*- coding: utf-8 -*-
"""
Created on Sun Oct 26 17:05:11 2025

@author: AKourgli
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import hilbert

# Simulation parameters
fs = 100000        # Sampling frequency (Hz)
T = 0.1            # Duration of signal (seconds)
t = np.linspace(0, T, int(T*fs), endpoint=False) # Time vector

# Message Signal: Sum of two sinusoids (300 Hz and 600 Hz)
f_m1 = 300
f_m2 = 600
m_t = np.sin(2 * np.pi * f_m1 * t) + 0.5 * np.sin(2 * np.pi * f_m2 * t)

# Carrier Signal
f_c = 10000        # Carrier frequency (Hz)
carrier = np.cos(2 * np.pi * f_c * t)

# 1. Generate the Hilbert Transform of the message signal (≈ -90° phase shift)
analytic_signal = hilbert(m_t)      # This creates the analytic signal
m_h_t = np.imag(analytic_signal)    # The imaginary part is the Hilbert transform

# 2. Generate the SSB signal using the Phasing Method (We'll generate USB)
ssb_usb_t = m_t * carrier - m_h_t * np.sin(2 * np.pi * f_c * t)

# 3. For comparison, let's also generate a DSB-SC signal
dsb_sc_t = m_t * carrier

# --- Plotting ---
plt.figure(figsize=(12, 10))

# Plot 1: Message Signal in Time Domain
plt.subplot(3, 2, 1)
plt.plot(t, m_t)
plt.title('Message Signal m(t)')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.xlim(0, 0.02) # Zoom in to see the waves

# Plot 2: SSB (USB) Signal in Time Domain
plt.subplot(3, 2, 2)
plt.plot(t, ssb_usb_t)
plt.title('SSB (USB) Signal s_USB(t)')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.xlim(0, 0.02)

# Plot 3: Spectrum of Message Signal
plt.subplot(3, 2, 3)
M_f = np.fft.fft(m_t)
freqs = np.fft.fftfreq(len(m_t), 1/fs)
plt.plot(freqs, np.abs(M_f))
plt.title('Spectrum of Message Signal')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Magnitude')
plt.xlim(-1500, 1500)

# Plot 4: Spectrum of DSB-SC Signal
plt.subplot(3, 2, 4)
DSB_f = np.fft.fft(dsb_sc_t)
plt.plot(freqs, np.abs(DSB_f))
plt.title('Spectrum of DSB-SC Signal')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Magnitude')
plt.xlim(f_c - 1500, f_c + 1500) # Zoom around the carrier

# Plot 5: Spectrum of SSB (USB) Signal
plt.subplot(3, 2, 5)
SSB_f = np.fft.fft(ssb_usb_t)
plt.plot(freqs, np.abs(SSB_f))
plt.title('Spectrum of SSB (USB) Signal')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Magnitude')
plt.xlim(f_c - 1500, f_c + 1500) # Zoom around the carrier

plt.tight_layout()
plt.show()