Esempio n. 1
0
def ber_calculator(x_bits, Rb, nc, h, msk, alpha, snr):
    signal, _ = gmsk_modulator(x_bits, Rb, nc, h, fs, alpha, msk)

    # Transmitter definition
    x_raw = Signal(fs, signal)
    x_start = np.array([15, 0,
                        15])  # Start coordinate for the transmitter in m
    v = np.array([1, 0, 0])  # Transmitter velocity in m/s
    t = 0
    fc = 436 * MHz
    tx0 = Transmitter(x_start, v, t, fc, x_raw)
    txs = []
    txs.append(tx0)

    # Phased array definition
    mx = 4  # Number of sensors in direction X
    my = 4  # Number of sensors in direction Y
    origin = np.array([0, 0, 0])  # Axis origin
    rx = PhasedArray(mx, my, txs[0].fc, origin)

    # Simulation parameters
    n = 1000  # Snapshots number
    d = len(txs)  # Number of signals/transmitters
    simulation = Simulation(n, d, snr)

    [s, x] = doa_samplesgen(txs, rx, simulation)

    x_rs, _ = random_sampler(x, simulation.n)
    doa = doaesprit_estimation(x_rs, rx)
    x_beamformer = np.real(beamformer(x, rx, doa, fc))

    x_det = gmsk_detector(x_beamformer, fs, Rb, nc)
    n_errors = np.cumsum(np.abs(x_bits - x_det))[-1]
    ber = n_errors / len(x_bits)
    return ber
mx = M  # Number of sensors in direction X
my = M  # Number of sensors in direction Y
origin = np.array([0, 0, 0])  # Axis origin
rx = PhasedArray(mx, my, txs[0].fc, origin)

# Simulation parameters
n = 124  # Snapshots number
d = len(txs)  # Number of signals/transmitters
fs = 64 * MHz  # Sampling frequency
fc = rx.fc
sampling_time = 5 * ms  # Sampling time
snr = 7  # SNR in dB
simulation = Simulation(n, d, snr)

[s, x] = doamusic_samples(txs, rx, simulation)
np.degrees(doaesprit_estimation(x, rx))

#%%
np.degrees(tx0.doa.el)
# %%
k = np.pi / rx.d
m = x.shape[0]

# Singular value decomposition of matrix "X"
[u, s, v] = np.linalg.svd(x, full_matrices=True)

# Get multiplicity 'q' of the minimun eigenvalue using histogram method
aval_min = s[0]
bins_n = 100
bins_aval = np.histogram(s, bins=bins_n)
count = 0
# snr = 7
n = 1000  # Snapshots number
d = len(txs)  # Number of signals/transmitters
simulation = Simulation(n, d, snr)

#%% Carrier Est vs SNR
error_array = np.zeros(snr_array.size)
fc_est = np.zeros(snr_array.size)
for i in range(snr_array.size):
    n_error = 10
    error = 0
    snr = snr_array[i]
    simulation = Simulation(n, d, snr)
    _, x = doa_samplesgen(txs, rx, simulation)
    x_rs, idx = random_sampler(x, simulation.n)
    doa = doaesprit_estimation(x_rs, rx, [], simulation.d)
    print(np.degrees(doa))
    x_beamformer = beamformer(x, rx, doa, fc)
    for j in range(n_error):
        m = 30
        x_rs, _ = random_sampler(x_beamformer, simulation.n, m)
        fc_est[i] = carrieresprit_estimation(x_rs, fs1)
        error = error + (1 / n_error) * (fc_rem - fc_est[i])**2
        print(fc_est[i])
    # print(error)
    error_array[i] = error / (fc_rem)**2
    print(error_array[i])

#%%
plt.figure(figsize=(16, 9), dpi=100)
plt.subplot(211)
Esempio n. 4
0
                    36.74234614])  # Start coordinate for the transmitter in m
v = np.array([1, 0, 0])  # Transmitter velocity in m/s
t = 0
fc = 436 * MHz
amp = 10
freq = 400
s = Sine_Wave(amp, freq)
tx0 = Transmitter(x_start, v, t, fc, s)

txs = []
txs.append(tx0)

# Simulation parameters
n = 1000  # Snapshots number
d = len(txs)  # Number of signals/transmitters
fs = 64 * MHz  # Sampling frequency
fc = rx.fc
sampling_time = 5 * ms  # Sampling time
snr = 7  # Signal-to-noise ratio in dB
simulation = Simulation(n, d, fs, fc, sampling_time, snr)

#%%
[s, x] = doamusic_samples(txs, rx, simulation)
doa = doaesprit_estimation(x, rx)
s = beamformer(x, rx, doa, fc)

#%%
plt.figure()
plt.plot(simulation.t, s, "o")
plt.show()
Esempio n. 5
0
error_el_music = np.zeros(snr_array.size)
error_az_esprit = np.zeros(snr_array.size)
error_el_esprit = np.zeros(snr_array.size)
n_error = 10

for i in range(snr_array.size):
    for j in range(n_error):
        snr = snr_array[i]
        simulation = Simulation(n, d, fs, fc, sampling_time, snr)
        [s, x] = doamusic_samples(txs, rx, simulation)
        p_mu = doamusic_estimation(s, a)
        idx = np.unravel_index(np.argmax(p_mu, axis=None), p_mu.shape)
        el_music_est[i] = np.degrees(el_music[idx[0]])
        az_music_est[i] = np.degrees(az_music[idx[1]])
        [az_esprit_est[i],
         el_esprit_est[i]] = np.degrees(doaesprit_estimation(x, rx))
        error_az_music[i] = (error_az_music[i] + (1 / n_error) *
                             (az_music_est[i] - np.degrees(txs[0].doa.az))**2)

        error_el_music[i] = (error_el_music[i] + (1 / n_error) *
                             (el_music_est[i] - np.degrees(txs[0].doa.el))**2)

        error_az_esprit[i] = (
            error_az_esprit[i] + (1 / n_error) *
            (az_esprit_est[i] - np.degrees(txs[0].doa.az))**2)

        error_el_esprit[i] = (
            error_el_esprit[i] + (1 / n_error) *
            (el_esprit_est[i] - np.degrees(txs[0].doa.el))**2)
# %%
plt.figure()
Esempio n. 6
0
def image_array(i):
    print(i)
    t = t_array[i]
    tx = Transmitter(x_start, v, t, fc, signal)
    track[:, i] = np.array([tx.r[0], tx.r[2]])
    _, x = doa_samplesgen(tx, rx, simulation)
    # x, idx = random_sampler(x, simulation.n)
    theta_est[i] = -np.degrees(doaesprit_estimation(x, rx, k, 1))

    fig, ax = plt.subplots(2, 1, figsize=(16, 9), dpi=150, num=0)
    img = Image.open("./images/satelite.png")
    imagebox = OffsetImage(img, zoom=0.08)
    ab = AnnotationBbox(imagebox, (track[0, i], track[1, i]), frameon=False)
    ax[1].grid()
    ax[0].add_artist(ab)
    # ax[0].arrow(0,0,0,15,color='k',width=0.1)
    ax[0].annotate("",
                   xy=(0, 0),
                   xytext=(0, 15),
                   arrowprops=dict(arrowstyle="<-", linewidth=3))
    ax[0].annotate("",
                   xy=(0, 0),
                   xytext=(10, 0),
                   arrowprops=dict(arrowstyle="<-", linewidth=3))
    ax[0].annotate("x", xy=(9.5, 0.5))
    ax[0].annotate("z", xy=(0.5, 13.5))

    ax[0].set_ylim((0, track[1, :].max() * (1 + 0.3)))
    ax[0].set_xlim(-17, 17)

    # line2, = ax[0].plot(track[0, i], track[1, i], "o")
    arrow = ax[0].arrow(
        0,
        0,
        track[0, i] * 0.9,
        track[1, i] * 0.9,
        width=0.15,
        length_includes_head=True,
        color="r",
    )

    # angle_plot = get_angle_plot(txs[i].doa.theta, 1)
    theta_deg = tx.doa.theta * 180 / np.pi
    theta_real_array[i] = theta_deg

    if theta_deg < 0:
        arc = matplotlib.patches.Arc((0, 0), 10, 10, 0, 90, 90 - theta_deg)
        ax[0].add_patch(arc)  # To display the angle arc
        ax[0].annotate(r"$\theta=$%.2f°" % theta_deg, xy=(0.5, 2.5), size=15)
    else:
        arc = matplotlib.patches.Arc((0, 0), 10, 10, 0, 90 - theta_deg, 90)
        ax[0].add_patch(arc)  # To display the angle arc
        ax[0].annotate(r"$\theta=$%.2f°" % theta_deg, xy=(-3.6, 2.5), size=15)

    ax[1].set_ylim((-50, 50))
    ax[1].set_xlim((0, 31))
    ax[1].set_ylabel(r"$\theta$ [°]", size=15)
    ax[1].set_xlabel(r"Time [s]", size=15)
    ax[1].plot(t_array[0:i],
               theta_est[0:i],
               label="ESPRIT",
               color="b",
               linewidth=3)
    ax[1].plot(
        t_array[0:i],
        theta_real_array[0:i],
        label="Real",
        linestyle="--",
        color="r",
        linewidth=2,
    )
    ax[1].legend()
    ax[1].plot([t_array[i], t_array[i]], [0, theta_est[i]],
               color="gray",
               linestyle="--")
    if theta_real_array[i] < 0:
        ax[1].annotate(r"$\hat{\theta}=$%.2f°" % theta_est[i],
                       xy=(t_array[i] + 0.2, 1),
                       size=15)
    else:
        ax[1].annotate(r"$\hat{\theta}=$%.2f°" % theta_est[i],
                       xy=(t_array[i] - 2.5, -12),
                       size=15)

    fig.canvas.draw()  # draw the canvas, cache the renderer
    image = np.frombuffer(fig.canvas.tostring_rgb(), dtype="uint8")
    image = image.reshape(fig.canvas.get_width_height()[::-1] + (3, ))

    return image
Esempio n. 7
0
v = np.array([1, 0, 0])  # Transmitter velocity in m/s
fc = 436 * MHz
amp = 10
freq = 1300
fs = 48 * kHz  # Sampling frequency
t_max = 5 * ms  # Sampling time
sine = Sine_Wave(amp, freq, fs, t_max)
signal = Signal(sine.fs, sine.data)

n_time = 200
n_theta = 200
theta_array = np.empty(n_theta)
t_array = np.linspace(0, 30, n_time)

track = np.zeros((2, n_time))
lambda_c = c / rx.fc
k = 2 * np.pi / (lambda_c)

#%%
for i in range(n_time):
    t = t_array[i]
    tx = Transmitter(x_start, v, t, fc, signal)
    track[:, i] = np.array([tx.r[0], tx.r[2]])
    _, x = doa_samplesgen(tx, rx, simulation)
    x, idx = random_sampler(x, simulation.n)
    theta_array[i] = np.degrees(doaesprit_estimation(x, rx, k, 1))
#%%
plt.figure()
plt.plot(theta_array, t_array)
plt.show()
mx = M  # Number of sensors in direction X
my = M  # Number of sensors in direction Y
origin = np.array([0, 0, 0])  # Axis origin
rx = PhasedArray(mx, my, txs[0].fc, origin)

# Simulation parameters
n = 1000  # Snapshots number
d = len(txs)  # Number of signals/transmitters
snr = 0  # SNR in dB
simulation = Simulation(n, d, snr)

[s, x] = doa_samplesgen(txs, rx, simulation)

# %%
# x_rs, _ = random_sampler(x, simulation.n)
doa = doaesprit_estimation(x, rx)
print(np.degrees(doa))
x_beamformer = beamformer(x, rx, doa, fc)
# %%

# %% Plot temporal
plt.figure()
plt.grid()
plt.plot(txs[0].x.t, txs[0].x.data)
plt.plot(txs[0].x.t, x_beamformer)
plt.xlabel("Tiempo [s]")
plt.ylabel("x(t)")
plt.show()

# %% Plot espectral
plt.figure()