Advertisement
Korotkodul

Задача №1. Сигналы.

Mar 28th, 2025 (edited)
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. from functools import partial
  2.  
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5.  
  6. from IPython.display import HTML
  7. from matplotlib.animation import FuncAnimation
  8. plt.style.use("ggplot")
  9.  
  10.  
  11. def put_labels(axis):
  12.     axis.set_title("Анимация модулированного сигнала",
  13.                    fontsize=16,
  14.                    fontweight="bold",
  15.                    c="black")
  16.  
  17.     axis.set_ylabel("Амплитуда",
  18.                     fontsize=13,
  19.                     fontweight="bold",
  20.                     c="dimgray")
  21.  
  22.     axis.set_xlabel("Время (секунды)",
  23.                     fontsize=13,
  24.                     fontweight="bold",
  25.                     c="dimgray"
  26.                     )
  27.  
  28.     plt.legend(handles=[plt.Line2D([0], [0], color='red', label='Сигнал')],
  29.                bbox_to_anchor=(1, 1), loc="upper right")
  30.  
  31.  
  32. def create_ordinates(abscissa, modulation, fc, frame_id):
  33.     if not modulation:
  34.         ordinates = np.sin(2 * np.pi * (abscissa + frame_id * animation_step) * fc)
  35.     else:
  36.         ordinates = modulation(abscissa + frame_id * animation_step) * np.sin(
  37.               2 * np.pi * (abscissa + frame_id * animation_step) * fc)
  38.     return ordinates
  39.  
  40.  
  41. def save_file(path, animation):
  42.     if path == "":
  43.         pass
  44.     else:
  45.         animation.save(path, writer="pillow", fps=30)
  46.  
  47.  
  48. def create_modulation_animation(
  49.     modulation,
  50.     fc,
  51.     num_frames,
  52.     plot_duration,
  53.     time_step=0.001,
  54.     animation_step=0.01,
  55.     save_path=""
  56. ) -> FuncAnimation:
  57.  
  58.     def update_frame(
  59.         frame_id: int,
  60.         *,
  61.         line: plt.Line2D,
  62.         abscissa: np.ndarray,
  63.     ) -> tuple[plt.Line2D]:
  64.         ordinates = create_ordinates(abscissa, modulation, fc, frame_id)
  65.         line.set_data(abscissa + frame_id * animation_step, ordinates)
  66.         axis.set_xlim((abscissa + frame_id * animation_step)[0], (abscissa + frame_id * animation_step)[-1])
  67.         return line,
  68.  
  69.     abscissa = np.arange(0, plot_duration, time_step)
  70.     figure, axis = plt.subplots(figsize=(16, 9))
  71.     axis: plt.Axes
  72.     put_labels(axis)
  73.     axis.set_xlim(abscissa.min(), abscissa.max())
  74.     line, *_ = axis.plot(
  75.         abscissa,
  76.         create_ordinates(abscissa, modulation, fc, 0),
  77.         c="red",
  78.     )
  79.  
  80.     animation = FuncAnimation(
  81.         figure,
  82.         partial(update_frame, line=line, abscissa=abscissa),
  83.         frames=num_frames,
  84.         interval=50,
  85.         blit=True,
  86.     )
  87.  
  88.     save_file(save_path, animation)
  89.  
  90.     return animation
  91.  
  92.  
  93. def modulation_function(t):
  94.     return np.sin(t)
  95.  
  96.  
  97. num_frames = 100  
  98. plot_duration = np.pi / 2
  99. time_step = 0.001  
  100. animation_step = np.pi / 200
  101. fc = 50
  102. save_path_with_modulation = "modulated_signal.gif"
  103.  
  104. animation = create_modulation_animation(
  105.     modulation=modulation_function,
  106.     fc=fc,
  107.     num_frames=num_frames,
  108.     plot_duration=plot_duration,
  109.     time_step=time_step,
  110.     animation_step=animation_step,
  111.     save_path=save_path_with_modulation
  112. )
  113. HTML(animation.to_jshtml())
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement