首页 > 解决方案 > 如何使用平滑和过滤技术从一组测量中生成估计值

问题描述

我对平滑/过滤传感器数据/计算值比较陌生。

我想在给定的时间轴上使用 pandas 的测量数据框生成类似下面的曲线。

我的数据是这样的:

charge_cycle  cumulative_chargetime_Ah  calculated_res
        
1   0.002199    0.075790
2   0.003123    0.071475
3   0.007699    0.097084
4   0.012086    0.050456
5   0.016609    0.077575
... ... ...
123169  478.228427  0.110583
123170  478.236834  0.139948
123171  478.239822  0.121189
123172  478.242608  0.144464
123173  478.251933  0.115232

我想要得到的输出如下所示。蓝色嘈杂的 computed_res 类似变量是我目前拥有的,它显然非常嘈杂,我需要对此进行某种形式的过滤以生成更有用的变量。红色图,我知道我可以使用内插法生成或通过在其上拟合一维多项式。

但是我真的不确定如何生成估计值,这是覆盖原始数据图的蓝色粗散点图。我可以就如何获得这个“估计值”获得一些建议吗?

我认为它确实与过滤有关,但我不确定如何将其应用于此用例。

估计值图

标签: pythonpandasfilteringsmoothingkalman-filter

解决方案


这确实看起来像是应用于测量值的某种过滤。这是一个使用savgol_filterfrom的示例SciPy

import numpy as np
from scipy.signal import savgol_filter
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_context("paper", font_scale=2.5)
sns.set_style('whitegrid')
sns.set_palette("Set1")

N = 200
T = 1/200

rnd = np.random.RandomState(12345)
x = np.linspace(0.0, 2*N*T, N)
signal = np.sin(np.pi*x)
noisy_signal = signal  + rnd.normal(0, 1, N)
filtered = savgol_filter(noisy_signal, window_length=31, polyorder=1)


plt.figure(figsize=(8, 4))
sns.lineplot(x, noisy_signal, label='Noisy', lw=0.5)
sns.lineplot(x, filtered, label='Filtered', lw=2);
ax = sns.regplot(x, noisy_signal, order=3, label='Best fit', scatter_kws={'s':2})
ax.legend(loc="best");

与所有过滤和拟合方法一样,它需要对问题有一些了解,并且可能需要一些试验和错误才能使参数正确。 噪声、过滤和最佳拟合信号


推荐阅读