版权声明:本文为博主原创文章,未经博主允许不得转载。
利用MATLAB计算三维坐标序列距离误差程序
1.三维坐标储存在文件中,格式如下:
各坐标间的距离真值是一定值,计算相邻距离的标准差。
2.MATLAB程序如下:
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % 名称:caculateAccuracy.m
- % 功能:读取三维世界坐标,计算精度
- % 作者:LYC
- % 单位:中科院苏州医工所
- % 日期:2014.5.5
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %% 清理各变量
- clc, clear
- %% 设定值
- dataFile = 'v20131114-2.txt'; % 设定文件名称
- errorFile = 'v20131114-2_error.txt';
- realValue = 1.00; % 设定真值
- %% 读取文件
- fid = fopen(dataFile, 'r'); % 打开文件
- k = 0; % 循环计数变量
- curr = [0, 0, 0]; % 临时变量
- a = []; % 数据变量
- while ~feof(fid) % 判断是否到了文件末尾
- curr = fscanf(fid, '(%f, %f, %f)\n', 3);
- if ~isempty(curr)
- k = k+1;
- a = [a, curr];
- end
- end
- fclose(fid);
- %% 计算各量之间的欧氏距离
- dist = []; % 储存欧氏距离
- for i = 1:k-1
- curr2 = (a(1,i)-a(1,i+1))^2 + (a(2,i)-a(2,i+1))^2 + (a(3,i)-a(3,i+1))^2; % 计算当前距离的平方
- curr3 = sqrt(curr2); %计算当前距离
- dist = [dist, curr3];
- end
- %% 各距离减去距离真值
- distError = dist - realValue; % 储存距离误差
- %% 利用贝塞尔公式计算标准差
- distError2 = distError.^2; % 计算误差平方
- sumError2 = sum(distError2);
- std = sqrt(sumError2/(k-1))
- %% 将结果写入文件
- fid2 = fopen(errorFile, 'w'); % 打开文件
- fprintf(fid2, 'standard deviation = %f\r\n', std);
- fprintf(fid2, 'distance errors are:\r\n');
- for i = 1:k-1
- fprintf(fid2, '%f\r\n', distError(i)); %输入当前数据与一空格
- end
- fclose(fid2);
- disp('误差写入文件成功!');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 名称:caculateAccuracy.m
% 功能:读取三维世界坐标,计算精度
% 作者:LYC
% 单位:中科院苏州医工所
% 日期:2014.5.5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 清理各变量
clc, clear
%% 设定值
dataFile = 'v20131114-2.txt'; % 设定文件名称
errorFile = 'v20131114-2_error.txt';
realValue = 1.00; % 设定真值
%% 读取文件
fid = fopen(dataFile, 'r'); % 打开文件
k = 0; % 循环计数变量
curr = [0, 0, 0]; % 临时变量
a = []; % 数据变量
while ~feof(fid) % 判断是否到了文件末尾
curr = fscanf(fid, '(%f, %f, %f)\n', 3);
if ~isempty(curr)
k = k+1;
a = [a, curr];
end
end
fclose(fid);
%% 计算各量之间的欧氏距离
dist = []; % 储存欧氏距离
for i = 1:k-1
curr2 = (a(1,i)-a(1,i+1))^2 + (a(2,i)-a(2,i+1))^2 + (a(3,i)-a(3,i+1))^2; % 计算当前距离的平方
curr3 = sqrt(curr2); %计算当前距离
dist = [dist, curr3];
end
%% 各距离减去距离真值
distError = dist - realValue; % 储存距离误差
%% 利用贝塞尔公式计算标准差
distError2 = distError.^2; % 计算误差平方
sumError2 = sum(distError2);
std = sqrt(sumError2/(k-1))
%% 将结果写入文件
fid2 = fopen(errorFile, 'w'); % 打开文件
fprintf(fid2, 'standard deviation = %f\r\n', std);
fprintf(fid2, 'distance errors are:\r\n');
for i = 1:k-1
fprintf(fid2, '%f\r\n', distError(i)); %输入当前数据与一空格
end
fclose(fid2);
disp('误差写入文件成功!');
3.误差分析结果储存在文件中,如下所示