import numpy as np
from matplotlib import pyplot as plt


def wg_ofdm():

    tx_sample_rate = 750e6
    rx_decimation = 16
    rx_sample_rate = tx_sample_rate / rx_decimation
    n_rx_samp_per_chip = 16   # Keep at >= x2 of num_chips and start phase will always be 0, if num chips is power of 2?
    num_chips = 8
    n_tx_samp_per_chip = n_rx_samp_per_chip * rx_decimation
    n_tx_samp_per_pulse = n_tx_samp_per_chip * num_chips

    print(n_tx_samp_per_chip / 4)

    chip_freq = (np.arange(num_chips) - num_chips//2 + 0.5)/num_chips * rx_sample_rate

    # First, generate simple in order waveform
    freq = np.repeat(chip_freq, n_tx_samp_per_chip)

    phase = freq / tx_sample_rate
    phase = np.cumsum(np.concatenate([[0], phase]))
    phase = phase[0:-1]

    # Extract the start phases of each chip, ideally these are always 0
    chip_start_phase = phase[0::n_tx_samp_per_chip] % 1
    print(chip_start_phase)
    print(chip_start_phase * 2**16)
    print(chip_freq)

    x = np.exp(1j * 2 * np.pi * phase)

    plt.figure()
    plt.plot(x.real)
    plt.plot(x.imag)

    plt.figure()
    plt.plot(np.unwrap(np.angle(x)))

    plt.show()


    return

if __name__ == '__main__':
    wg_ofdm()