I have a very noisy function that I can smooth (following the answer given here How to smooth a curve in the right way?) using scipy.signal.savgol_filter. The original and smoothed data look like this (blue and red, respectively):
The issue is that I need to sample from the smoothed data, that is: I need to evaluate the red curve at arbitrary x
values. The savgol_filter
function only gives me back an array of values, not a function that I can evaluate.
What is the fastest way (it will be sampled millions of times) to do this?
MVCE
import numpy as np
from scipy.signal import savgol_filter
import matplotlib.pyplot as plt
# Noisy data
y = np.array([-5715.75, -5592.3 , -5548.33, -5638.97, -5586.43, -5703.21,
-5660.6 , -5714.96, -5637.59, -5599.72, -5631.14, -5684.31,
-5586.08, -5617.43, -5629.58, -5530.08, -5540.53, -5475.53,
-5505.21, -5500.96, -5500.58, -5474.65, -5462.45, -5443.82,
-5441.77, -5463.53, -5512.18, -5395.85, -5389.87, -5432.94,
-5366.31, -5284.45, -5176.52, -5221.89, -5182.52, -5084.92,
-5084.3 , -4972.78, -4968.32, -4818.19, -4789.56, -4872.02,
-4809.45, -4855.06, -4806.77, -4717.93, -4741.29, -4822.45,
-4760.51, -4698.31, -4744.1 , -4797.08, -4777.43, -4785.02,
-4687.61, -4820.73, -4753.5 , -4777.99, -4812.5 , -4856.53,
-4859.69, -4905.37, -4838.71, -5058.49, -5053.58, -5057. ,
-5159.58, -5155.03, -5079.21, -5228.57, -5257.26, -5409.64,
-5505.87, -5511.82, -5471.4 , -5478.47, -5530.9 , -5578.88,
-5705.87, -5633.66, -5740.72, -5760.05, -5801.39, -5808.52,
-5803.22, -5832.76, -5867.51, -5837.56, -5923.97, -5933.75,
-5945.04, -5932.16, -5909.68, -5951.29, -5958.6 , -5958.07,
-5970.75, -5931.93, -5947.53, -5956.36])
x = np.linspace(0., 6, 100)
# Smoothed data
yhat = savgol_filter(y, 51, 3)
plt.plot(x, y)
plt.plot(x, yhat, color='r')
plt.show()
Aucun commentaire:
Enregistrer un commentaire