[Animation_ChatGPT] updated
This commit is contained in:
parent
2515472d32
commit
d2444240f2
@ -29,40 +29,7 @@ axs[1].set_xlabel('Frequency [Hz]')
|
||||
axs[1].set_ylabel('Power [1/Hz]')
|
||||
axs[1].set_xlim(0, 1500)
|
||||
|
||||
# Function to compute and plot the power spectrum
|
||||
def plot_powerspectrum(i):
|
||||
# Clear the previous plots
|
||||
axs[0].cla()
|
||||
axs[1].cla()
|
||||
|
||||
# Generate the signal
|
||||
x = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2[i]*t)
|
||||
x[x < 0] = 0 # Apply half-wave rectification
|
||||
|
||||
# Plot the signal (first 20 ms for clarity)
|
||||
axs[0].plot(t[t < 0.02], x[t < 0.02])
|
||||
axs[0].set_title(f"Signal (f2={f2[i]} Hz)")
|
||||
axs[0].set_xlabel('Time [s]')
|
||||
axs[0].set_ylabel('Amplitude')
|
||||
axs[0].set_ylim(-2, 2)
|
||||
|
||||
# Compute power spectrum
|
||||
freq, power = welch(x, fs=1/dt, nperseg=2**16)
|
||||
|
||||
# Plot the power spectrum
|
||||
axs[1].plot(freq, power)
|
||||
axs[1].set_xlim(0, 1500)
|
||||
axs[1].set_ylim(0, 0.05)
|
||||
axs[1].set_title(f'Power Spectrum (f2={f2[i]} Hz)')
|
||||
axs[1].set_xlabel('Frequency [Hz]')
|
||||
axs[1].set_ylabel('Power [1/Hz]')
|
||||
|
||||
# # Create the animation
|
||||
# ani = FuncAnimation(fig, plot_powerspectrum, frames=len(distances), interval=500)
|
||||
|
||||
# # Display the animation
|
||||
# ani.save("signal_animation.gif", writer=PillowWriter(fps=30))
|
||||
# plt.show()
|
||||
|
||||
# Generate the signal as a sum of two sine waves
|
||||
def plot_powerspectrum_2(i):
|
||||
@ -93,7 +60,7 @@ def plot_powerspectrum_2(i):
|
||||
axs[1].set_title(f'Power Spectrum (f2={f2[i]} Hz)')
|
||||
axs[1].set_xlabel('Frequency [Hz]')
|
||||
axs[1].set_ylabel('Power [1/Hz]')
|
||||
axs[1].set_ylim(0, 0.00007)
|
||||
#axs[1].set_ylim(0, 0.00007)
|
||||
axs[1].plot(f1, power[np.argmin(np.abs(freq-f1))], 'o')
|
||||
axs[1].plot(f2[i], power[np.argmin(np.abs(freq-f2[i]))], 'd')
|
||||
axs[1].plot(AM, power[np.argmin(np.abs(freq-AM))], '*')
|
||||
|
Loading…
Reference in New Issue
Block a user