-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualizer.py
83 lines (67 loc) · 2.66 KB
/
Visualizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import matplotlib.pyplot as plt
def impulse_response(t):
return np.heaviside(t, 1) * np.exp(-t)
def calculate_output(input_signal, impulse_response):
output_signal = np.convolve(input_signal, impulse_response, mode='full')
return output_signal[:len(input_signal)]
def calculate_fourier_series(input_signal, t, T, num_harmonics):
coefficients = []
for n in range(-num_harmonics, num_harmonics + 1):
coefficient = np.sum(input_signal * np.exp(-1j * 2 * np.pi * n * t / T)) / len(t)
coefficients.append(coefficient)
return np.array(coefficients)
def calculate_fourier_transform(input_signal, t):
frequency = np.fft.fftfreq(len(t), t[1] - t[0])
transform = np.fft.fft(input_signal)
return frequency, transform
def log_data(filename, data_dict):
with open(filename, "w") as log_file:
for key, value in data_dict.items():
log_file.write(f"{key}:\n{value}\n\n")
def plot_results(t, input_signal, output_signal, fourier_series, frequency, fourier_transform):
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.plot(t, input_signal)
plt.title('Input Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(2, 2, 2)
plt.plot(t, output_signal)
plt.title('Output Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(2, 2, 3)
harmonics_range = range(-len(fourier_series) // 2, len(fourier_series) // 2)
plt.stem(harmonics_range, np.abs(fourier_series), basefmt=" ")
plt.title('Fourier Series Coefficients')
plt.xlabel('Harmonics')
plt.ylabel('Magnitude')
plt.subplot(2, 2, 4)
plt.plot(frequency, np.abs(fourier_transform))
plt.title('Fourier Transform')
plt.xlabel('Frequency')
plt.ylabel('Magnitude')
plt.tight_layout()
plt.show()
def main():
t = np.linspace(0, 5, 500) # Time vector
input_signal = np.sin(2 * np.pi * t) # Example input signal
# Calculate output signal and transforms
impulse_resp = impulse_response(t)
output_signal = calculate_output(input_signal, impulse_resp)
fourier_series = calculate_fourier_series(input_signal, t, t[-1], 10)
frequency, fourier_transform = calculate_fourier_transform(input_signal, t)
# Log the results
data_to_log = {
"Input Signal": input_signal,
"Output Signal": output_signal,
"Fourier Series Coefficients": fourier_series,
"Frequency": frequency,
"Fourier Transform": fourier_transform
}
log_data("log.txt", data_to_log)
# Plot the results
plot_results(t, input_signal, output_signal, fourier_series, frequency, fourier_transform)
if __name__ == "__main__":
main()