如何在Matlab中绘制彩色直方图类型的星座图

How to plot colorful histogram type constellation diagram in Matlab

我想绘制类似下图的星座图。

我的方法是这样的

 clc;
 clear all;
 close all;
 N=30000;                            
 M=16;                               
 Sr=randint(N,1,[0,(M-1)]);          
 S=qammod(Sr,16,0,'gray'); S=S(:);   
 Noisy_Data=awgn(S,20,'measured');       % Add AWGN
 figure(2)
 subplot(1,2,1)
 plot(S,'o','markersize',10);
 grid on
 subplot(1,2,2)
 plot(Noisy_Data,'.');
 grid on

请您协助我进行必要的修改以获得类似于上图的图形。谢谢你。

首先要做的是计算数据的二维直方图。这可以通过以下方式完成:

% Size of the histogram matrix
Nx   = 160;
Ny   = 160;

% Choose the bounds of the histogram to match min/max of data samples.
% (you could alternatively use fixed bound, e.g. +/- 4)
ValMaxX = max(real(Noisy_Data));
ValMinX = min(real(Noisy_Data));
ValMaxY = max(imag(Noisy_Data));
ValMinY = min(imag(Noisy_Data));
dX = (ValMaxX-ValMinX)/(Nx-1);
dY = (ValMaxY-ValMinY)/(Ny-1);

% Figure out which bin each data sample fall into
IdxX = 1+floor((real(Noisy_Data)-ValMinX)/dX);
IdxY = 1+floor((imag(Noisy_Data)-ValMinY)/dY);
H = zeros(Ny,Nx);
for i=1:N
  if (IdxX(i) >= 1 && IdxX(i) <= Nx && IdxY(i) >= 1 && IdxY(i) <= Ny)
    % Increment histogram count
    H(IdxY(i),IdxX(i)) = H(IdxY(i),IdxX(i)) + 1;
  end
end

请注意,您可以使用参数 NxNy 调整所需的绘图分辨率。请记住,直方图越大,数据样本越多(由模拟的参数 N 控制),您需要在直方图箱中有足够的数据以避免出现斑点图。

然后您可以根据 this answer 将直方图绘制为彩色图。这样做时,您可能希望向直方图的所有非零区间添加一个常量,以便为零值区间保留白色带。这将提供与散点图更好的相关性。这可以通过以下方式完成:

% Colormap that approximate the sample figures you've posted
map = [1 1 1;0 0 1;0 1 1;1 1 0;1 0 0];

% Boost histogram values greater than zero so they don't fall in the
% white band of the colormap.
S    = size(map,1);
Hmax = max(max(H));
bias = (Hmax-S)/(S-1);
idx = find(H>0);
H(idx) = H(idx) + bias;

% Plot the histogram
pcolor([0:Nx-1]*dX+ValMinX, [0:Ny-1]*dY+ValMinY, H);
shading flat;
colormap(map);

N 增加到 1000000 后,根据您的示例生成的数据如下图所示: