-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle_filter.m
More file actions
78 lines (48 loc) · 1.44 KB
/
Copy pathParticle_filter.m
File metadata and controls
78 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
% Hamiltonian-based Noisy SHO Estimation with Particle Filter
clear all; close all;
simulate_data;
% Parameters
N = 10000;
Q = 0.05; % variance of the initial state preparation
meas_related{1} = Y;
meas_related{2} = cp;
meas_related{3} = 2;
meas_related{4} = -2;
meas_related{5} = R;
m0 = ket_0;
% Initial sample set
SX = [];
for iter = 1:N
Phi = gauss_rnd(0,Q,1);
St = [cos(Phi);sin(Phi)];
SX=[SX,St];
end
% Particle Filtering
MM = zeros(1, length(Y));
for k = 1:length(Y)
[SX, W] = importance_sampling( SX, N, U_DT, meas_related, k);
% Resampling
[SX, new_weight] = resample(W,SX);
Y_est = zeros(1,N);
for iter = 1:N
Y_est(iter) = SX(:,iter)'* sx * SX(:,iter);
end
% Mean estimate
m = mean(Y_est);
MM(k) = m;
end
% plotting
figure('Position', [100, 100, 800, 400]);
plot(T, Y, 'k.', 'MarkerSize', 10, 'DisplayName', 'Measurement data');
hold on;
plot(T, Theoretical_pred, 'r-', 'LineWidth', 2, 'DisplayName', 'Observable Value');
plot(T, MM, 'b-.', 'LineWidth', 3, 'DisplayName', 'PF Estimate');
xlabel('Time (s)');
ylabel('Oscillation Amplitude (rad)');
title('Tracking Spin observable with Particle Filter');
grid on;
legend('show', 'Location', 'best');
fprintf('PF estimate.\n');
rmse = sqrt(mean((Theoretical_pred - MM).^2));
fprintf('RMSE of Particle Filter: %.2f\n', rmse);
saveas(gcf, 'SpinObservable_0_1w0.svg');