37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as pp
|
|
|
|
|
|
def plot_funcs(ax, function, time, string_title, string_x_axis, string_y_axis):
|
|
ax.set_xlabel(string_x_axis)
|
|
ax.set_ylabel(string_y_axis)
|
|
ax.set_title(string_title)
|
|
ax.grid(visible=True, axis="both")
|
|
ax.plot(time, function)
|
|
|
|
|
|
def main():
|
|
time = np.linspace(start=0, stop=1, num=1000, endpoint=False)
|
|
signal_1 = np.sin(2 * np.pi * 50 * time)
|
|
signal_2 = 0.5 * np.sin(2 * np.pi * 120 * time)
|
|
function = signal_1 + signal_2
|
|
|
|
freqs = np.fft.fftfreq(len(time), d=time[1] - time[0])
|
|
fft = np.fft.fft(function)/len(time)
|
|
|
|
# Hier erstellen wir eine Figure mit 1 Zeile und 2 Spalten für die Plots
|
|
figs, ax = pp.subplots(1, 2, figsize=(12, 5))
|
|
|
|
# Der erste Plot in der ersten Achse (ax[0])
|
|
plot_funcs(ax[0], function, time, "time domain", "time in [s]", "amplitude in [m]")
|
|
|
|
# Der zweite Plot in der zweiten Achse (ax[1])
|
|
pos_mask = freqs > 0
|
|
plot_funcs(ax[1], abs(fft[pos_mask]), freqs[pos_mask], "frequency domain", "frequency in [Hz]", "")
|
|
|
|
pp.tight_layout() # Optimiert den Platz zwischen den Plots
|
|
pp.show()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |