Skip to content

Instantly share code, notes, and snippets.

@netsatsawat
Created June 19, 2020 12:28
Show Gist options
  • Save netsatsawat/7d6c65a1c54508e31d9e24c4dc0304fb to your computer and use it in GitHub Desktop.
Save netsatsawat/7d6c65a1c54508e31d9e24c4dc0304fb to your computer and use it in GitHub Desktop.
fig, axes = plt.subplots(2, 1, figsize=(14, 6))
plt.subplots_adjust(hspace=.5)
axes[0].set_title(f'Wave with a frequency of {signal1} Hz')
axes[0].plot(tm, ampl1)
axes[0].set_xlabel('Time')
axes[0].set_ylabel('Amplitude')
ft_ = np.fft.fft(ampl1) / len(ampl1) # Normalize amplitude and apply the FFT
ft_ = ft_[range(int(len(ampl1)/2))] # Exclude sampling frequency
tp_cnt = len(ampl1)
val_ = np.arange(int(tp_cnt / 2))
tm_period_ = tp_cnt / smpl_freq
freq_ = val_ / tm_period_
axes[1].set_title('Fourier transform depicting the frequency components')
axes[1].plot(freq_, abs(ft_))
axes[1].set_xlabel('Frequency')
axes[1].set_ylabel('Amplitude')
annot_max(freq_, abs(ft_), ax=axes[1])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment