plots.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from sklearn.preprocessing import OneHotEncoder
  2. import numpy as np
  3. from tensorflow.keras import layers
  4. from end_to_end import load_model
  5. from models.custom_layers import DigitizationLayer, OpticalChannel
  6. from matplotlib import pyplot as plt
  7. import math
  8. def plot_e2e_spectrum(model_name=None, num_samples=10000):
  9. '''
  10. Plot frequency spectrum of the output signal at the encoder
  11. @param model_name: The name of the model to import. If None, then the latest model will be imported.
  12. @param num_samples: The number of symbols to simulate when computing the spectrum.
  13. '''
  14. # Load pre-trained model
  15. ae_model, params = load_model(model_name=model_name)
  16. # Generate a list of random symbols (one hot encoded)
  17. cat = [np.arange(params["cardinality"])]
  18. enc = OneHotEncoder(handle_unknown='ignore', sparse=False, categories=cat)
  19. rand_int = np.random.randint(params["cardinality"], size=(num_samples, 1))
  20. out = enc.fit_transform(rand_int)
  21. # Encode the list of symbols using the trained encoder
  22. a = ae_model.encode_stream(out).flatten()
  23. # Pass the output of the encoder through LPF
  24. lpf = DigitizationLayer(fs=params["fs"],
  25. num_of_samples=params["cardinality"] * num_samples,
  26. sig_avg=0)(a).numpy()
  27. # Plot the frequency spectrum of the signal
  28. freq = np.fft.fftfreq(lpf.shape[-1], d=1 / params["fs"])
  29. plt.plot(freq, np.fft.fft(lpf), 'x')
  30. plt.ylim((-500, 500))
  31. plt.xlim((-5e10, 5e10))
  32. plt.show()
  33. def plot_e2e_encoded_output(model_name=None):
  34. '''
  35. Plots the raw outputs of the encoder neural network as well as the voltage potential that modulates the laser.
  36. The distorted DD received signal is also plotted.
  37. @param model_name: The name of the model to import. If None, then the latest model will be imported.
  38. '''
  39. # Load pre-trained model
  40. ae_model, params = load_model(model_name=model_name)
  41. # Generate a random block of messages
  42. val, inp, _ = ae_model.generate_random_inputs(num_of_blocks=1, return_vals=True)
  43. # Encode and flatten the messages
  44. enc = ae_model.encoder(inp)
  45. flat_enc = layers.Flatten()(enc)
  46. chan_out = ae_model.channel.layers[1](flat_enc)
  47. # Instantiate LPF layer
  48. lpf = DigitizationLayer(fs=params["fs"],
  49. num_of_samples=params["messages_per_block"] * params["samples_per_symbol"],
  50. sig_avg=0)
  51. # Apply LPF
  52. lpf_out = lpf(flat_enc)
  53. # Time axis
  54. t = np.arange(params["messages_per_block"] * params["samples_per_symbol"])
  55. if isinstance(ae_model.channel.layers[1], OpticalChannel):
  56. t = t / params["fs"]
  57. # Plot the concatenated symbols before and after LPF
  58. plt.figure(figsize=(2 * params["messages_per_block"], 6))
  59. for i in range(1, params["messages_per_block"]):
  60. plt.axvline(x=t[i * params["samples_per_symbol"]], color='black')
  61. plt.plot(t, flat_enc.numpy().T, 'x', label='output of encNN')
  62. plt.plot(t, lpf_out.numpy().T, label='optical field at tx')
  63. plt.plot(t, chan_out.numpy().flatten(), label='optical field at rx')
  64. plt.xlim((t.min(), t.max()))
  65. plt.title(str(val[0, :, 0]))
  66. plt.legend(loc='upper right')
  67. plt.show()
  68. if __name__ == '__main__':
  69. plot_e2e_spectrum()
  70. plot_e2e_encoded_output()