-
Notifications
You must be signed in to change notification settings - Fork 0
/
np_series.py
64 lines (51 loc) · 2.25 KB
/
np_series.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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MultipleLocator
# Streamlit App
import streamlit as st
st.title("Fourier Series Plotter")
h = st.number_input("Number of Harmonics", min_value=1, max_value=100, value=10, step=1)
# Generate x values
N = 1000
x = np.linspace(-np.pi, np.pi, N)
# Define the function to be approximated with its Fourier series
original_function = lambda x: np.array([x_i / np.pi if x_i >= 0 else (1 + x_i / np.pi) for x_i in x])
# Fourier coefficients Definition
# a0 = 0
# a = lambda n: 4 * (1j ** n).imag / (n * np.pi)
# b = lambda n: 0
# coefficients = [a0] + [(a(n), b(n)) for n in range(1, h + 1)]
# Calculate Fourier series coefficients for the given function
def fourier_coefficients(func, n_terms=10):
a0 = np.trapz(func, dx=2 * np.pi / N) / (2 * np.pi)
coefficients = [a0]
for n in range(1, n_terms + 1):
an = np.trapz(func * np.cos(n * x), dx=2 * np.pi / N) / np.pi
bn = np.trapz(func * np.sin(n * x), dx=2 * np.pi / N) / np.pi
coefficients.append((an, bn))
return coefficients
coefficients = fourier_coefficients(original_function(x), n_terms=h)
# Reconstruct the function using its Fourier series expansion
def fourier_series(coefficients, x):
series = coefficients[0]
for n, (an, bn) in enumerate(coefficients[1:], start=1):
series += an * np.cos(n * x) + bn * np.sin(n * x)
return series
reconstructed_function = fourier_series(coefficients, x)
# Create a single figure and plot both the original function and Fourier series approximation
fig, ax = plt.subplots(figsize=(10, 5))
# Plot the original function
ax.plot(x, original_function(x), label='Original Function', color='blue')
# Plot the Fourier series approximation on the same axes
ax.plot(x, reconstructed_function, label=f'Fourier Series with {h} harmonics', color='red')
ax.set_xlabel('$\omega_1$t')
ax.set_ylabel('f($\omega_1$t)')
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.xaxis.set_major_formatter(
FuncFormatter(lambda val, pos: f'${val / np.pi: .1f}\pi$' if val != 0 else '0')
)
ax.xaxis.set_major_locator(MultipleLocator(base=np.pi / 2))
ax.xaxis.set_major_locator(MultipleLocator(base=np.pi / 2))
ax.legend()
st.pyplot(fig)