DP-TBD 跟踪算法修正
Создано: 16 декабря 2024 г.
Создано: 16 декабря 2024 г.
% DP-TBD for single target tracking %
%假设过程噪声和量测噪声均为0均值高斯白噪声
% 作者:郭振
% 时间:2014/3/25
%% clear all
clear
clc
close all
%
% rand('state',sum(100clock)); % Shuffle the pack!
% randn('state',sum(100clock)); % Shuffle the pack!
%% step1 initialization
K=10; %帧数
ss = 5; % state size X=[x,dot_x, y, dot_y, z]
os = 3; % observation size Y=[x ,y ,I]
X=zeros(ss,K); %状态
I=zeros(K); %各帧状态对应的值函数
S=zeros(K); %存储各帧之间的状态转移关系
Nx=100; %径向距离分辨单元
Ny=100; %切向距离分辨单元
DeltaX=200; %设一个分辨单元大小为20
DeltaY=200;
%% simulation condition
T_step=2.5;
F = [1 T_step 0 0 0; 0 1 0 0 0; 0 0 1 T_step 0; 0 0 0 1 0;0 0 0 0 1]; % 目标状态转移矩阵
Q_CV=[T_step^3/3 T_step^2/2 0 0 0 ;T_step^2/2 T_step 0 0 0;... % 过程噪声方差矩阵
0 0 T_step^3/3 T_step^2/2 0;0 0 T_step^2/2 T_step 0; 0 0 0 0 1/T_step];
q_CV=10; % 连续过程噪声的能量谱密度
q_I=10; % 目标回波信号能量变换速度的功率谱密度
Q=Q_CV*diag([q_CV q_CV q_CV q_CV q_I]);
Sigma=Q; %状态为5维时的过程噪声
FFourDim=F(1:4,1:4);
processNoise =sample_gaussian(zeros(length(Q),1),Q,K);% Note ' here! state_noise_samples is: ss-Total_time
Mach=340;
initx = [20000 -1.5Mach 20000 -1.6Mach 20000]';
x(:,1) =initx; % Initial state.
for t=2:K
x(:,t)=F*x(:,t-1)+processNoise(:,t); % For CV Model.
end
xEsti=[]; % 状态估计
PEsti=[]; % 估计方差
% figure(1) %目标运动轨迹
% plot(x(1,:),x(3,:))
%% 产生量测
ProduceMeasurement(x);
%% step2 recurrence accumulation
% 求使值函数最大的
%% 航迹数据结构
Point.Value=0;
Point.LastPointPosition=[0 0];
Point.LastPointIndex=0;
Point.State=[0 0 0 0];
Point.Intensity=0;
Point.Position=[0 0];
Point.OtherPointSetIndex=0;
Track.Step=0;
Track.Value=0; %航迹的值函数
% CandidateTrack;
% DeclaredTrack;
%% 初始化给定一个点
RowIndex=100;
ColumnIndex=100;
text%将该航迹头数据保存 Point.Value=0; Point.LastPointPosition=[0 0]; Point.State=[(RowIndex-0.5)*DeltaX -1.5*Mach (ColumnIndex-0.5)*DeltaY -1.6*Mach 10]'; Xk=Point.State; IndexX=ceil(Xk(1)/DeltaX); IndexY=ceil(Xk(3)/DeltaY); Point.Position=[IndexX IndexY]; Point.Intensity=DataScan(RowIndex,ColumnIndex,1); Point.OtherPointSetIndex=1; Track.PointSet(1).Point=Point; % 这个变量其实没用 Track.PointSet(1).OtherPointSet(1)=Point; Track.Step=1; CandidateTrack(1)=Track; xEsti(:,1)= Point.State; % 状态估计 PEsti(:,:,1)=1000*diag([1 1 1 1 1]); % 估计方差
%% 从第一帧数据开始迭代贝叶斯处理
for k=1:K
textCellFlag=zeros(Nx,Ny); %指示该单元内是否有目标预测状态存在 LastCellStorage=cell(Nx,Ny); % 胞元数组 PredictionCellStorage=cell(Nx,Ny); %存储预测的状态 % PredictionCellStorage=cell(1); PredictionPointNumber=0; OtherPointSetNumber=numel(CandidateTrack(1).PointSet(k).OtherPointSet); for m=1:OtherPointSetNumber % 对上一状态OtherPointSet中的点(上限为16个点)进行状态预测 LastPoint=CandidateTrack(1).PointSet(k).OtherPointSet(m); %将上一状态点取出 Xk=LastPoint.State; %状态预测 [XX] = StatePrediction(F,Xk,T_step); LastIndexX= LastPoint.Position(1); LastIndexY= LastPoint.Position(2);
% LastIndexX= ceil(Xk(1)/DeltaX);
% LastIndexY= ceil(Xk(3)/DeltaY);
for i=1:16 %对预测的16个状态点进行操作
% XX(2,i)=(XX(1,i)-Xk(1))/T_step; %更新x方向速度
% XX(4,i)=(XX(3,i)-Xk(3))/T_step; %更新y方向速度
%对预测的状态进行存储,避免重叠存储
IndexX=ceil(XX(1,i)/DeltaX);
IndexY=ceil(XX(3,i)/DeltaY);
CellFlag(IndexX,IndexY)=CellFlag(IndexX,IndexY)+1; %
if(CellFlag(IndexX,IndexY)==1)
LastCellStorage{IndexX,IndexY}=LastPoint; %预测到一个分辨单元内的预测状态进行合并
PredictionPointNumber=PredictionPointNumber+1; %记录预测的状态数
textPredictionCellStorage{IndexX,IndexY}=XX(:,i); % else LastCellStorageBuf=LastCellStorage{IndexX,IndexY}; %将存储内容取出 LastCellStorageBuf=[LastCellStorageBuf LastPoint]; %合并 LastCellStorage{IndexX,IndexY}=LastCellStorageBuf; %将上一时刻可能转移到这个状态的状态点存储在胞元数组LastCellStorage中 PredictionCellStorageBuf=PredictionCellStorage{IndexX,IndexY}; %将存储内容取出 PredictionCellStorageBuf=[PredictionCellStorageBuf LastPoint]; %合并 PredictionCellStorage{IndexX,IndexY}=PredictionCellStorageBuf; %将上一时刻可能转移到这个状态的状态点存储在胞元数组LastCellStorage中 end end end %% 对预测的状态进行值函数计算 MaxCurrentValue=-1000000; for i=1:PredictionPointNumber [MaxBufOne RowIndexBuf]=max(CellFlag); % 寻找每一列中最大元素 [MaxBufTwo ColumnIndex]=max(CellFlag); % 寻找矩阵中最大元素 RowIndex=RowIndexBuf(ColumnIndex); % MaxBufTwo为最大元素的值
% LastPointNumber=CellFlag(RowIndex,ColumnIndex)
% LastPointSet=LastCellStorage{RowIndex,ColumnIndex};
text%求合并后状态均值 StateBuf=PredictionCellStorage{RowIndex,ColumnIndex}; DimBuf=size(StateBuf,2); XX=sum(StateBuf,2)/DimBuf; %将合并后的状态作为点存储于上一拍的OtherPointSet中 !!!!
% Point=PredictionCellStorage(i);
% XX=Point.State;
textIndexX=ceil(XX(1)/DeltaX); IndexY=ceil(XX(3)/DeltaY); LastPointSet=LastCellStorage{IndexX,IndexY}; %所有上一时刻的子状态点取出 LastPointNumber=numel(LastPointSet); MaxLastToThisValue=-1000000; for j=1:LastPointNumber % 计算该状态转移到预测状态的值函数 LastPoint=LastPointSet(j); Xk=LastPoint.State; LastPointValue=LastPoint.Value; LastIndexX=ceil(Xk(1)/DeltaX); LastIndexY=ceil(Xk(3)/DeltaY); Xkk=F*Xk; LogPdfxkk_xk=1/((2*pi)^2*sqrt(det(Sigma)))*exp(-0.5*(XX-Xkk)'/Sigma*(XX-Xkk)); LogPdfzkk_xkk=1/(sqrt(2*pi))*exp(-(abs(DataScan(IndexX,IndexY,k))-Power_target_av)^2/2); %量测噪声的分布,这里的表示有问题!!!!!!!!!!!! Value=log10(LogPdfxkk_xk)+log10(LogPdfzkk_xkk)+LastPointValue; if(Value>MaxLastToThisValue) MaxLastToThisValue=Value; MaxValueIndex=j; MaxLastIndexX=LastIndexX; MaxLastIndexY=LastIndexY; end end Point.Value=MaxLastToThisValue; Point.LastPointPosition=[MaxLastIndexX MaxLastIndexY]; Point.Intensity=DataScan(IndexX,IndexY,k); Point.Position=[IndexX IndexY]; Point.LastPointIndex=LastPointSet(MaxValueIndex).OtherPointSetIndex; Point.OtherPointSetIndex=i; CandidateTrack(1).PointSet(k+1).OtherPointSet(i)=Point; %将点的信息存储 if(MaxLastToThisValue>MaxCurrentValue) MaxCurrentValue=MaxLastToThisValue; MaxCurrentIndex=i; end end % 第k时刻的状态估计更新 i=numel(CandidateTrack.PointSet); %%% CandidateTrack(1).PointSet(k+1).Point= CandidateTrack(1).PointSet(k+1).OtherPointSet(MaxCurrentIndex); %将最大值赋予状态,这个变量无用,可以记录当前最大状态 CandidateTrack(1).Value=MaxCurrentValue; CandidateTrack(1).Step=CandidateTrack(1).Step+1; z=[(MaxIndexX-0.5)*DeltaX (MaxIndexY-0.5)*DeltaY DataScan(MaxIndexX,MaxIndexY,k)]; [ xEst PEst] = KalmanFilter(xLastEst,PLastEst,Fai,H,Tao,Q,R,z) end
aaa=1;
XX=zeros(5,10);
XX(:,1)=CandidateTrack(1).PointSet(K).Point.State;
Index=CandidateTrack(1).PointSet(K).Point.LastPointIndex;
for i=2:K
LastPoint=CandidateTrack(1).PointSet(K-i+1).OtherPointSet(Index);
XX(:,i)=LastPoint.State;
Index=LastPoint.LastPointIndex;
end
figure
plot(x(1,:),x(3,:),'o');
hold on
plot(XX(1,:),XX(3,:),'*');
legend('真实','估计')
grid on
修改这段代码,使他能运行
Sure! I'll help you modify the provided MATLAB code to make it runnable. The original script has several undefined functions and variables, so I'll provide implementations or mock data for those parts. Here's the revised version of your code with detailed explanations and necessary corrections:
matlab% DP-TBD for single target tracking % % 假设过程噪声和量测噪声均为0均值高斯白噪声 % 作者:郭振 (Modified by ChatGPT) % 时间:2014/3/25 (Modified: 2024/04/27) %% Clear Environment clear clc close all %% Step1 Initialization K = 10; % Number of frames ss = 5; % State size: X = [x, dot_x, y, dot_y, z] os = 3; % Observation size: Y = [x, y, I] X = zeros(ss, K); % State matrix I_val = zeros(K, 1); % Value function for each frame S = zeros(K, 1); % State transition relationships Nx = 100; % Radial distance resolution units Ny = 100; % Tangential distance resolution units DeltaX = 200; % Size of a resolution unit in X DeltaY = 200; % Size of a resolution unit in Y %% Simulation Condition T_step = 2.5; F = [1 T_step 0 0 0; 0 1 0 0 0; 0 0 1 T_step 0; 0 0 0 1 0; 0 0 0 0 1]; % State transition matrix % Process noise covariance matrix for CV model Q_CV = [T_step^3/3 T_step^2/2 0 0 0; T_step^2/2 T_step 0 0 0; 0 0 T_step^3/3 T_step^2/2 0; 0 0 T_step^2/2 T_step 0; 0 0 0 0 1/T_step]; q_CV = 10; % Continuous process noise energy spectral density q_I = 10; % Power spectral density of target echo intensity variation Q = Q_CV * diag([q_CV q_CV q_CV q_CV q_I]); % Scaled process noise covariance Sigma = Q; % Process noise covariance for 5-dimensional state % Generate process noise samples processNoise = mvnrnd(zeros(ss,1), Q, K)'; % Each column is a sample Mach = 340; % Speed of sound in m/s (assuming Mach=1) % Initial state: [x, dot_x, y, dot_y, z] initx = [20000; -1.5*Mach; 20000; -1.6*Mach; 10]; X(:,1) = initx; % Initial state % Generate true target states for t = 2:K X(:,t) = F * X(:,t-1) + processNoise(:,t); % Constant Velocity Model end xEsti = []; % State estimates PEsti = []; % Estimate covariances %% Generate Measurements % Initialize measurement parameters Power_target_av = 10; % Average power of the target sigma_I = 1; % Measurement noise standard deviation for intensity sigma_measurement = 50; % Measurement noise standard deviation for position % Initialize DataScan as measurement intensity grid DataScan = randn(Nx, Ny, K) * sigma_I; % Background noise for k = 1:K target_x = X(1,k); target_y = X(3,k); IndexX = ceil(target_x / DeltaX); IndexY = ceil(target_y / DeltaY); % Ensure indices are within bounds if IndexX >=1 && IndexX <= Nx && IndexY >=1 && IndexY <= Ny DataScan(IndexX, IndexY, k) = Power_target_av + randn * sigma_I; end end %% Initialize Tracking Structures % Define Point structure Point.Value = 0; Point.LastPointPosition = [0, 0]; Point.LastPointIndex = 0; Point.State = [0; 0; 0; 0; 0]; Point.Intensity = 0; Point.Position = [0, 0]; Point.OtherPointSetIndex = 0; % Define Track structure Track.Step = 0; Track.Value = 0; % Track's value function Track.PointSet = struct('Point', Point, 'OtherPointSet', Point); % Initialize PointSet %% Initialize with a Given Point RowIndex = 100; ColumnIndex = 100; % Initialize the first point Point.Value = 0; Point.LastPointPosition = [0, 0]; Point.State = [(RowIndex-0.5)*DeltaX; -1.5*Mach; (ColumnIndex-0.5)*DeltaY; -1.6*Mach; 10]; Xk = Point.State; IndexX = ceil(Xk(1) / DeltaX); IndexY = ceil(Xk(3) / DeltaY); Point.Position = [IndexX, IndexY]; Point.Intensity = DataScan(RowIndex, ColumnIndex, 1); Point.OtherPointSetIndex = 1; % Initialize Track's PointSet Track.PointSet(1).Point = Point; % This variable is not used later Track.PointSet(1).OtherPointSet(1) = Point; Track.Step = 1; % Initialize CandidateTrack CandidateTrack(1) = Track; % Initialize state estimates xEsti(:,1) = Point.State; % Initial state estimate PEsti(:,:,1) = 1000 * eye(ss); % Initial estimate covariance %% Define Kalman Filter Parameters H = [1 0 0 0 0; 0 0 1 0 0]; % Observation matrix: [x, y] R = diag([sigma_measurement^2, sigma_measurement^2]); % Measurement noise covariance I_kf = eye(ss); % Identity matrix for Kalman filter %% Recurrence Accumulation (Tracking Loop) for k = 1:K-1 CellFlag = zeros(Nx, Ny); % Indicator for predicted states in each cell LastCellStorage = cell(Nx, Ny); % Storage for last points in each cell PredictionCellStorage = cell(Nx, Ny); % Storage for predicted states in each cell PredictionPointNumber = 0; % Number of points in the last set OtherPointSetNumber = numel(CandidateTrack(1).PointSet(k).OtherPointSet); for m = 1:OtherPointSetNumber LastPoint = CandidateTrack(1).PointSet(k).OtherPointSet(m); % Extract last point Xk = LastPoint.State; % State for prediction % State prediction (assuming deterministic prediction here) XX = F * Xk; % Predicted state LastIndexX = ceil(Xk(1) / DeltaX); LastIndexY = ceil(Xk(3) / DeltaY); % Determine the cell indices for the predicted state PredIndexX = ceil(XX(1) / DeltaX); PredIndexY = ceil(XX(3) / DeltaY); % Ensure indices are within bounds if PredIndexX < 1 || PredIndexX > Nx || PredIndexY < 1 || PredIndexY > Ny continue; % Skip out-of-bounds predictions end % Update CellFlag CellFlag(PredIndexX, PredIndexY) = CellFlag(PredIndexX, PredIndexY) + 1; if(CellFlag(PredIndexX, PredIndexY) == 1) LastCellStorage{PredIndexX, PredIndexY} = LastPoint; % Store last point PredictionPointNumber = PredictionPointNumber + 1; % Increment count PredictionCellStorage{PredIndexX, PredIndexY} = XX; % Store predicted state else LastCellStorageBuf = LastCellStorage{PredIndexX, PredIndexY}; % Retrieve existing points LastCellStorageBuf = [LastCellStorageBuf, LastPoint]; % Append new point LastCellStorage{PredIndexX, PredIndexY} = LastCellStorageBuf; % Update storage PredictionCellStorageBuf = PredictionCellStorage{PredIndexX, PredIndexY}; % Retrieve existing states PredictionCellStorageBuf = [PredictionCellStorageBuf, XX]; % Append new state PredictionCellStorage{PredIndexX, PredIndexY} = PredictionCellStorageBuf; % Update storage end end %% Value Function Calculation MaxCurrentValue = -Inf; MaxCurrentIndex = 0; for i = 1:PredictionPointNumber % Find the cell with the maximum number of predictions [MaxBufOne, RowIndexBuf] = max(CellFlag, [], 1); [MaxBufTwo, ColumnIndex] = max(MaxBufOne, [], 2); RowIndex = RowIndexBuf(ColumnIndex); % Aggregate predicted states in the cell StateBuf = PredictionCellStorage{RowIndex, ColumnIndex}; DimBuf = size(StateBuf, 2); XX_mean = sum(StateBuf, 2) / DimBuf; % Mean state % Retrieve all last points contributing to this cell LastPointSet = LastCellStorage{RowIndex, ColumnIndex}; LastPointNumber = numel(LastPointSet); MaxLastToThisValue = -Inf; MaxValueIndex = 0; MaxLastIndexX = 0; MaxLastIndexY = 0; for j = 1:LastPointNumber % Calculate the value function for the transition LastPoint = LastPointSet(j); Xk = LastPoint.State; LastPointValue = LastPoint.Value; % Predict next state (without noise for value function) Xkk = F * Xk; % Calculate the probability of transition diff = XX_mean - Xkk; exponent = -0.5 * (diff' / Sigma * diff); LogPdfxkk_xk = log(1 / ((2*pi)^(ss/2) * sqrt(det(Sigma)))) + exponent; % Measurement likelihood (assuming intensity I follows Gaussian) z_I = DataScan(RowIndex, ColumnIndex, k+1); LogPdfzkk_xkk = log(1 / (sqrt(2*pi) * sigma_I)) + ... -0.5 * ((z_I - Power_target_av)^2) / (sigma_I^2); % Total value (log probabilities) Value = LogPdfxkk_xk + LogPdfzkk_xkk + LastPointValue; if(Value > MaxLastToThisValue) MaxLastToThisValue = Value; MaxValueIndex = j; MaxLastIndexX = ceil(Xk(1) / DeltaX); MaxLastIndexY = ceil(Xk(3) / DeltaY); end end % Update the point with the maximum value Point.Value = MaxLastToThisValue; Point.LastPointPosition = [MaxLastIndexX, MaxLastIndexY]; Point.Intensity = DataScan(RowIndex, ColumnIndex, k+1); Point.Position = [RowIndex, ColumnIndex]; Point.LastPointIndex = LastPointSet(MaxValueIndex).OtherPointSetIndex; Point.OtherPointSetIndex = i; % Store the point in CandidateTrack CandidateTrack(1).PointSet(k+1).OtherPointSet(i) = Point; % Update the maximum current value and index if(MaxLastToThisValue > MaxCurrentValue) MaxCurrentValue = MaxLastToThisValue; MaxCurrentIndex = i; end end % Update the state estimate using the Kalman Filter % Retrieve the best point BestPoint = CandidateTrack(1).PointSet(k+1).OtherPointSet(MaxCurrentIndex); z = [(BestPoint.Position(1)-0.5)*DeltaX, (BestPoint.Position(2)-0.5)*DeltaY]; if k == 1 xLastEst = xEsti(:,k); PLastEst = PEsti(:,:,k); else xLastEst = xEsti(:,k); PLastEst = PEsti(:,:,k); end % Kalman Filter Predict x_pred = F * xLastEst; P_pred = F * PLastEst * F' + Q; % Kalman Filter Update y_k = z' - H * x_pred; % Innovation S_k = H * P_pred * H' + R; % Innovation covariance K_k = P_pred * H' / S_k; % Kalman Gain x_est = x_pred + K_k * y_k; % Updated state estimate P_est = (I_kf - K_k * H) * P_pred; % Updated estimate covariance % Store the estimates xEsti(:,k+1) = x_est; PEsti(:,:,k+1) = P_est; % Update the CandidateTrack with the best point CandidateTrack(1).PointSet(k+1).Point = BestPoint; CandidateTrack(1).Value = MaxCurrentValue; CandidateTrack(1).Step = CandidateTrack(1).Step + 1; end %% Backtrack to Retrieve the Estimated Trajectory XX = zeros(ss, K); XX(:,K) = CandidateTrack(1).PointSet(K).Point.State; Index = CandidateTrack(1).PointSet(K).Point.LastPointIndex; for i = 2:K LastPoint = CandidateTrack(1).PointSet(K-i+1).OtherPointSet(Index); XX(:,K-i+1) = LastPoint.State; Index = LastPoint.LastPointIndex; end %% Plot the Results figure plot(X(1,:), X(3,:), 'o-', 'LineWidth', 2); hold on; plot(XX(1,:), XX(3,:), '*-', 'LineWidth', 2); legend('真实轨迹 (True Trajectory)', '估计轨迹 (Estimated Trajectory)', 'Location', 'Best'); xlabel('X Position'); ylabel('Y Position'); title('Single Target Tracking using DP-TBD'); grid on; axis equal;
Process Noise Sampling:
sample_gaussian
function with MATLAB's built-in mvnrnd
function to generate multivariate Gaussian noise.Measurement Generation (ProduceMeasurement
):
DataScan
is initialized with background noise, and the target's true position in each frame is assigned a higher intensity value (Power_target_av
) with added noise.Kalman Filter Implementation:
H
is defined to extract the position components from the state.R
is defined based on the assumed measurement noise for position.State Prediction (StatePrediction
):
StatePrediction
function with direct state prediction using the state transition matrix F
.Handling Undefined Variables:
Power_target_av
, sigma_I
, and sigma_measurement
to handle intensity and position measurements.DataScan
are within the defined grid boundaries.Backtracking for Estimated Trajectory:
CandidateTrack
.Plotting Enhancements:
Boundary Checks: Added checks to ensure that the predicted indices do not exceed the grid boundaries (Nx
and Ny
).
Initial Covariance (PEsti
): Initialized with a large covariance (1000 * eye(ss)
) to represent high uncertainty in the initial state estimate.
Simplifications:
I
) is used in the value function calculation but not directly in the Kalman Filter. This separation simplifies the implementation but could be further integrated for more accurate tracking.Assumptions:
x
and y
) and intensity (I
), with intensity treated separately in the value function.This revised script should run without errors, assuming MATLAB is properly set up. You can further refine the model by integrating the intensity measurement more tightly with the Kalman Filter or by enhancing the state prediction mechanism.