Exponential smoothing or exponential moving average
import numpy as np
import matplotlib.pyplot as plt
# Generate noisy data
np.random.seed(42) # For reproducibility
time = np.arange(1, 101)
values = 50 + np.cumsum(np.random.randn(100) * 10) # Large noise
# Calculate EMA
alpha = 0.3 # Smoothing factor
ema_direct = []
alpha = 0.3
for i, value in enumerate(values):
if i == 0:
ema_direct.append(value) # Initialize EMA with the first value
else:
ema_direct.append(alpha * value + (1 - alpha) * ema_direct[-1])
# Plot the noisy data and EMA
plt.figure(figsize=(10, 6))
plt.plot(time, values, label='Noisy Data', alpha=0.7)
plt.plot(time, ema_direct, label='Exponential Moving Average (EMA)', linewidth=2, color='orange')
plt.title('Noisy Data with Exponential Moving Average')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
EMA applies more weight to the recent data exponentially for interpolation