noise_generator = np.random.RandomState(0)
observation_covariance = np.eye(2) + noise_generator.randn(2, 2) * 0.1
Initial_state = [0, 0]
intial_covariance = [[1, 0.1], [-0.1, 1]]

# UKF
kf = UnscentedKalmanFilter(transition_function,
                           observation_function,
                           transition_covariance,
                           observation_covariance,
                           Initial_state,
                           intial_covariance,
                           random_state=noise_generator)

sample = 200
states, observations = kf.sample(sample, Initial_state)
# estimate state with filtering and smoothing
filtered_state_estimates = kf.filter(observations)[0]
smoothed_state_estimates = kf.smooth(observations)[0]

# True line
t = np.linspace(0, sample * 0.1, sample)
y = np.sin(t)

plt.plot(filtered_state_estimates[:, 0],
         filtered_state_estimates[:, 1],
         color='r',
         ls='-',
         label='UKF')
plt.plot(smoothed_state_estimates[:, 0],
         smoothed_state_estimates[:, 1],
Exemple #2
0
initial_state_mean = [0, 0]
initial_state_covariance = [[1, 0.1], [-0.1, 1]]

# sample from model
ukf = UnscentedKalmanFilter(
    transition_function, observation_function,
    transition_covariance, observation_covariance,
    initial_state_mean, initial_state_covariance,
    random_state=random_state
)
akf = AdditiveUnscentedKalmanFilter(
    additive_transition_function, additive_observation_function,
    transition_covariance, observation_covariance,
    initial_state_mean, initial_state_covariance
)
states, observations = ukf.sample(50, initial_state_mean)

# estimate state with filtering
ukf_state_estimates = ukf.filter(observations)[0]
akf_state_estimates = akf.filter(observations)[0]

# draw estimates
pl.figure()
lines_true = pl.plot(states, color='b')
lines_ukf = pl.plot(ukf_state_estimates, color='r')
lines_akf = pl.plot(akf_state_estimates, color='g')
pl.legend((lines_true[0], lines_ukf[0], lines_akf[0]),
          ('true', 'UKF', 'AddUKF'),
          loc='upper left'
)
pl.show()
Exemple #3
0
initial_state_covariance = [[1, 0.1], [0.1, 1]]

# sample from model
ukf = UnscentedKalmanFilter(transition_function,
                            observation_function,
                            transition_covariance,
                            observation_covariance,
                            initial_state_mean,
                            initial_state_covariance,
                            random_state=random_state)
akf = AdditiveUnscentedKalmanFilter(additive_transition_function,
                                    additive_observation_function,
                                    transition_covariance,
                                    observation_covariance, initial_state_mean,
                                    initial_state_covariance)
states, observations = ukf.sample(50, initial_state_mean)

# estimate state with filtering
ukf_state_estimates = ukf.filter(observations)[0]
akf_state_estimates = akf.filter(observations)[0]

# draw estimates
pl.figure()
lines_true = pl.plot(states, color='b')
lines_ukf = pl.plot(ukf_state_estimates, color='r')
lines_akf = pl.plot(akf_state_estimates, color='g')
pl.legend((lines_true[0], lines_ukf[0], lines_akf[0]),
          ('true', 'UKF', 'AddUKF'),
          loc='upper left')
pl.show()
transition_covariance = np.eye(2)
random_state = np.random.RandomState(0)
observation_covariance = np.eye(2) + random_state.randn(2, 2) * 0.1
initial_state_mean = [0, 0]
initial_state_covariance = [[1, 0.1], [-0.1, 1]]

# sample from model
kf = UnscentedKalmanFilter(transition_function,
                           observation_function,
                           transition_covariance,
                           observation_covariance,
                           initial_state_mean,
                           initial_state_covariance,
                           random_state=random_state)
states, observations = kf.sample(50, initial_state_mean)
print(type(observations))
print(observations[0, 0])
print(observations.shape)
# estimate state with filtering and smoothing
filtered_state_estimates = kf.filter(observations)[0]
smoothed_state_estimates = kf.smooth(observations)[0]

# draw estimates
pl.figure()
lines_true = pl.plot(states, color='b')
lines_filt = pl.plot(filtered_state_estimates, color='r', ls='-')
lines_smooth = pl.plot(smoothed_state_estimates, color='g', ls='-.')
pl.legend((lines_true[0], lines_filt[0], lines_smooth[0]),
          ('true', 'filt', 'smooth'),
          loc='lower left')