Fourier Basis Function¶
Example created by Wilson Rocha Lacerda Junior
This example shows how changing or adding a new basis function could improve the model
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from sysidentpy.model_structure_selection import FROLS
from sysidentpy.basis_function import Polynomial, Fourier
from sysidentpy.utils.plotting import plot_results
from sysidentpy.metrics import root_relative_squared_error
np.seterr(all="ignore")
np.random.seed(1)
%matplotlib inline
import numpy as np import matplotlib.pyplot as plt from sysidentpy.model_structure_selection import FROLS from sysidentpy.basis_function import Polynomial, Fourier from sysidentpy.utils.plotting import plot_results from sysidentpy.metrics import root_relative_squared_error np.seterr(all="ignore") np.random.seed(1) %matplotlib inline
Defining the system¶
In [2]:
Copied!
# Simulated system
def system_equation(y, u):
yk = (
(0.2 - 0.75 * np.cos(-y[0] ** 2)) * np.cos(y[0])
- (0.15 + 0.45 * np.cos(-y[0] ** 2)) * np.cos(y[1])
+ np.cos(u[0])
+ 0.2 * u[1]
+ 0.7 * u[0] * u[1]
)
return yk
repetition = 5
random_samples = 200
total_time = repetition * random_samples
n = np.arange(0, total_time)
# Generating input
x = np.random.normal(size=(random_samples,)).repeat(repetition)
_, ax = plt.subplots(figsize=(12, 6))
ax.step(n, x)
ax.set_xlabel("$n$", fontsize=18)
ax.set_ylabel("$x[n]$", fontsize=18)
plt.show()
# Simulated system def system_equation(y, u): yk = ( (0.2 - 0.75 * np.cos(-y[0] ** 2)) * np.cos(y[0]) - (0.15 + 0.45 * np.cos(-y[0] ** 2)) * np.cos(y[1]) + np.cos(u[0]) + 0.2 * u[1] + 0.7 * u[0] * u[1] ) return yk repetition = 5 random_samples = 200 total_time = repetition * random_samples n = np.arange(0, total_time) # Generating input x = np.random.normal(size=(random_samples,)).repeat(repetition) _, ax = plt.subplots(figsize=(12, 6)) ax.step(n, x) ax.set_xlabel("$n$", fontsize=18) ax.set_ylabel("$x[n]$", fontsize=18) plt.show()
Simulate the system¶
In [3]:
Copied!
y = np.empty_like(x)
# Initial Conditions
y0 = [0, 0]
# Simulate it
y[0:2] = y0
for i in range(2, len(y)):
y[i] = system_equation(
[y[i - 1], y[i - 2]], [x[i - 1], x[i - 2]]
) + np.random.normal(scale=0.1)
# Plot
_, ax = plt.subplots(figsize=(12, 6))
ax.plot(n, y)
ax.set_xlabel("$n$", fontsize=18)
ax.set_ylabel("$y[n]$", fontsize=18)
ax.grid()
plt.show()
y = np.empty_like(x) # Initial Conditions y0 = [0, 0] # Simulate it y[0:2] = y0 for i in range(2, len(y)): y[i] = system_equation( [y[i - 1], y[i - 2]], [x[i - 1], x[i - 2]] ) + np.random.normal(scale=0.1) # Plot _, ax = plt.subplots(figsize=(12, 6)) ax.plot(n, y) ax.set_xlabel("$n$", fontsize=18) ax.set_ylabel("$y[n]$", fontsize=18) ax.grid() plt.show()
Adding noise to the system¶
In [4]:
Copied!
# Noise free data
ynoise_free = y.copy()
# Generate noise
v = np.random.normal(scale=0.5, size=y.shape)
# Data corrupted with noise
ynoisy = ynoise_free + v
# Plot
_, ax = plt.subplots(figsize=(14, 8))
ax.plot(n, ynoise_free, label="Noise-free data")
ax.plot(n, ynoisy, label="Corrupted data")
ax.set_xlabel("$n$", fontsize=18)
ax.set_ylabel("$y[n]$", fontsize=18)
ax.legend(fontsize=18)
plt.show()
# Noise free data ynoise_free = y.copy() # Generate noise v = np.random.normal(scale=0.5, size=y.shape) # Data corrupted with noise ynoisy = ynoise_free + v # Plot _, ax = plt.subplots(figsize=(14, 8)) ax.plot(n, ynoise_free, label="Noise-free data") ax.plot(n, ynoisy, label="Corrupted data") ax.set_xlabel("$n$", fontsize=18) ax.set_ylabel("$y[n]$", fontsize=18) ax.legend(fontsize=18) plt.show()