基于k-D树邻近点搜索的彩色星座图绘制[Matlab]
参考文献
- k-d树(Kd trees) https://blog.csdn.net/qq_39747794/article/details/82262576
- MATLAB创建二叉树(二维数据) http://www.freesion.com/article/477649174/
- 源代码文件上传为 https://download.csdn.net/download/ganlin410153929/12019809
- 旧版本星座图绘制的文件
https://download.csdn.net/download/ganlin410153929/9922262
https://download.csdn.net/download/ganlin410153929/9920585
使用matlab生成k-D树
1. 运行代码文件Main_Scatterplot.m
以50个点为样例,进行树的生成。依据参考文献的办法生成k-D树,其效果如下:
从第一列至第十二列的含义分别为,复数信号的实部,复数信号的虚部,父节点,左子节点,右子节点,切割方法,此点的x轴最小值,此点的x轴最大值,此点的y轴最小值,此点的y轴最大值,最邻近点的标号,距最邻近点的距离。
切割方向,0表示沿x轴进行切割,数据被分为上下两个部分;1表示沿y轴进行切割,数据被分为左右两个部分。
2. k-D树的绘制
生成树按照btree的第7至10列数据进行绘制,如下
3. k-D树的邻近点搜索算法
邻近点搜索首先是假设初始邻近点为自己的父节点,最小距离为其到父节点的距离。通过调用代码块即可获得各点的邻近点,代码如下:
function btree = UpdateMinimumDis2(btree)
% 更新每一点的最近距离
for k = 1:size(btree,1)
tag = 0;
ref = k;
idx = btree(k,3);
if idx ~= 0
dism = btree(k,12);
while tag == 0
if btree(ref,1)+dism<=btree(idx,8)&&btree(ref,1)-dism>=btree(idx,7)&&...
btree(ref,2)+dism<=btree(idx,10)&&btree(ref,2)-dism>=btree(idx,9)
%检查此节点的区域是否包含最小半径圆
tag = 1;
else
tag = 0;
if btree(idx,3) ~= 0
idx = btree(idx,3);%求其父节点的父节点
else
break;%其就是根节点
end
end
end
else
idx = k;
end
% disp(idx);
btree = UpdateMinimumDisFromChildren2(idx,k,btree);%从父节点开始寻找
end
end
而核心的子节点搜索算法为:
function btree = UpdateMinimumDisFromChildren2(idx,ref,btree)
ShowInformation = 0;
% 从idx及其子节点寻找离ref的最近点
neg = btree(idx,4);
pos = btree(idx,5);
dism = btree(ref,12);%当前的最近距离
if idx == ref%如果是自己则跳过
if neg
btree = UpdateMinimumDisFromChildren2(neg,ref,btree);%查找左节点及其子节点
end
if pos
btree = UpdateMinimumDisFromChildren2(pos,ref,btree);%查找右节点及其子节点
end
else
% 首先需要计算自己这个节点的区域是否满足要求
tag = CheckPoint(idx,ref,dism,btree);
if tag%如果可能存在更近的点,则首先计算本节点
distest = abs(complex(btree(idx,1)-btree(ref,1),btree(idx,2)-btree(ref,2)));
if distest<dism&&idx~=ref
btree(ref,12) = distest;
btree(ref,11) = idx;
if ShowInformation
disp(['Update:',num2str(ref),'; Minimum distance:',num2str(distest),'; Point:', num2str(idx)]);
end
end
if distest<btree(idx,12)&&idx~=ref
btree(idx,12) = distest;
btree(idx,11) = ref;
if ShowInformation
disp(['Update:',num2str(idx),'; Minimum distance:',num2str(distest),'; Point:', num2str(ref)]);
end
end
% 近似的距离 L;L肯定小于真实距离
% 如果所在区域存在可能,则需要计算其子节点
if neg && CheckPoint(neg,ref,dism,btree)
btree = UpdateMinimumDisFromChildren2(neg,ref,btree);%查找左节点及其子节点
end
if pos && CheckPoint(pos,ref,dism,btree)
btree = UpdateMinimumDisFromChildren2(pos,ref,btree);%查找右节点及其子节点
end
end
end
end
基于星座点距离生成彩色星座图
1. 星座图绘制
星座图绘制直接运行脚本文件 Main_Scatterplot2.m 即可,可修改绘制点数,默认为65536个点。
disdata = btree(:,12);
disdata = (disdata/max(abs(disdata))).^0.1;%将距离进行折算
Nslice = 512;
try
[conters1,centers1] = histcounts(disdata,Nslice);
for k = 1:length(centers1)-1
temp1(k) = 0.5*(centers1(k)+centers1(k+1));
end
centers1 = temp1;
catch
[conters1,centers1] = hist(disdata,Nslice);
end
cpdf = zeros(1,Nslice);
cpdf(1) = conters1(1);
for k = 2:Nslice
cpdf(k) = cpdf(k-1)+conters1(k);
end
cpdf = cpdf/cpdf(end);
indexcolor = ceil(cpdf*Nslice);
halfwide = (centers1(2)-centers1(1))/2;
factor = 0.5;%着色截取,1表示整段jet图谱
istart = round(Nslice/factor*(1-factor)/2);
cmap = jet(round(Nslice/factor));
cmap = cmap(istart:istart+Nslice-1,:);
cmap = flipud(cmap);
% obj.ComSys.NewFigure;
figure;
hold on
for k = Nslice:-1:1
plot(real(wavein((disdata>=centers1(k)-halfwide)&(disdata<=centers1(k)+halfwide))),...
imag(wavein((disdata>=centers1(k)-halfwide)&(disdata<=centers1(k)+halfwide))),...
'linewidth',2,'linestyle','none','marker','.','MarkerSize',5,...
'MarkerFaceColor',cmap(indexcolor(k),:),'MarkerEdgeColor',cmap(indexcolor(k),:));
end
xlabel('In-Phase');
ylabel('Quadrature');
grid on
set(gcf,'Unit','normalized','position',[0.1 0.1 0.4 0.4],'MenuBar','none');
set(gca,'fontname','Times New Roman','fontsize',12,'fontweight','bold');
% xlim([1.1*min(real(wavein)),1.1*max(real(wavein))]);
xlim([-1.5 1.5]);
% ylim([1.1*min(imag(wavein)),1.1*max(imag(wavein))]);
ylim([-1.5 1.5]);
% set(gca,'xtick',[],'ytick',[]);
set(gca,'xtick',-1:0.5:1,'ytick',-1:0.5:1);
box on
在获得每一点的最近距离后,按照最近距离近似为此位置的密度,对所有距离进行着色处理,此处采用jet类型配色,颜色深度为512种。通过调整着色截取,进行深浅调节。可认为修改颜色的映射关系。
2. 星座图绘制效果
则最终绘制的彩色星座图的效果如下:
来源:CSDN
作者:ganlin410153929
链接:https://blog.csdn.net/ganlin410153929/article/details/103449855