fork download
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. # Define a symmetric time vector around 0 (essential for even/odd decomposition)
  5. t = np.linspace(-5, 5, 1000)
  6.  
  7. # Dictionary containing all 6 signals and their names
  8. signals = {
  9. 1: ('sin(t)', lambda t: np.sin(t)),
  10. 2: ('cos(t)', lambda t: np.cos(t)),
  11. 3: ('exp(t)', lambda t: np.exp(t)),
  12. 4: ('exp(-t)', lambda t: np.exp(-t)),
  13. 5: ('Unit Step mu(t)', lambda t: np.heaviside(t, 1.0)),
  14. 6: ('Ramp r(t)', lambda t: np.maximum(0, t)),
  15. }
  16.  
  17. # --- SELECT YOUR SIGNAL HERE (1 to 6) ---
  18. choice = 1
  19. signal_name, func = signals[choice]
  20.  
  21. # 1. Original signal x(t)
  22. xt = func(t)
  23.  
  24. # 2. Time reverse signal x(-t) (evaluated at -t since t is symmetric)
  25. xt_rev = func(-t)
  26.  
  27. # 3. Even part of the signal: x_e(t) = 0.5 * (x(t) + x(-t))
  28. xe_t = 0.5 * (xt + xt_rev)
  29.  
  30. # 4. Odd part of the signal: x_o(t) = 0.5 * (x(t) - x(-t))
  31. xo_t = 0.5 * (xt - xt_rev)
  32.  
  33. # 5. Reconstructed signal: x(t) = x_e(t) + x_o(t)
  34. x_reconstructed = xe_t + xo_t
  35.  
  36. # --- PLOTTING ---
  37. fig, axs = plt.subplots(5, 1, figsize=(9, 12), sharex=True)
  38. fig.suptitle(
  39. f'Signal Decomposition & Transformations for: {signal_name}',
  40. fontsize=14,
  41. fontweight='bold',
  42. )
  43.  
  44. plots_data = [
  45. (xt, '1. Original Signal x(t)', 'tab:blue'),
  46. (xt_rev, '2. Time Reversed Signal x(-t)', 'tab:orange'),
  47. (xe_t, '3. Even Part x_e(t)', 'tab:green'),
  48. (xo_t, '4. Odd Part x_o(t)', 'tab:red'),
  49. (
  50. x_reconstructed,
  51. '5. Reconstructed Signal x(t) = x_e(t) + x_o(t)',
  52. 'tab:purple',
  53. ),
  54. ]
  55.  
  56. for ax, (data, title, color) in zip(axs, plots_data):
  57. ax.plot(t, data, color=color, linewidth=2)
  58. ax.set_title(title, fontsize=10, loc='left')
  59. ax.grid(True, linestyle='--', alpha=0.6)
  60. ax.axhline(0, color='black', linewidth=0.8, linestyle='-')
  61. ax.axvline(0, color='black', linewidth=0.8, linestyle='-')
  62.  
  63. plt.xlabel('Time (t)', fontsize=11)
  64. plt.tight_layout()
  65. plt.show()
Success #stdin #stdout #stderr 3.1s 73692KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Fontconfig error: No writable cache directories