### 绘制调制星座图的MATLAB代码
对于不同的相移键控(PSK)和正交幅度调制(QAM),可以使用MATLAB内置函数来创建相应的星座图。以下是针对BPSK、QPSK、8PSK以及16QAM和32QAM的具体实现方法。
#### BPSK Constellation Diagram
```matlab
% Generate random bits and map to BPSK symbols [-1, 1]
bits = randi([0 1], 1000, 1);
bpsk_symbols = 2*bits - 1;
% Plot constellation diagram
scatter(real(bpsk_symbols), imag(bpsk_symbols));
axis([-1.5 1.5 -1.5 1.5]);
grid on;
title('BPSK Constellation');
xlabel('In-phase (I)');
ylabel('Quadrature (Q)');
```
#### QPSK Constellation Diagram
```matlab
% Create Gray-coded symbol mapping table for QPSK
qpsk_table = [0+0j; 0+1j; 1+1j; 1+0j];
% Randomly select indices from the table
indices = randi(length(qpsk_table), 1000, 1);
% Map selected index values into complex numbers representing points in I/Q plane
qpsk_symbols = qpsk_table(indices);
% Display constellation plot of generated data sequence
figure();
scatter(real(qpsk_symbols), imag(qpsk_symbols));
axis equal;
grid on;
title('QPSK Constellation');
xlabel('Real Part');
ylabel('Imaginary Part');
```
#### 8PSK Constellation Diagram
```matlab
M = 8; % Number of points in signal constellation
theta = pi/M*(0:(M-1)); % Phase offset between adjacent phases
eight_psk_points = exp(1i * theta);
% Select some arbitrary set of transmitted signals as an example
transmitted_signals = eight_psk_points(randperm(M));
% Draw constellation chart with grid lines enabled
hFig = figure();
hAx = axes(hFig);
constellationDiagram(transmitted_signals,'Title','8 PSK Signal Points',...
'ReferenceConstellation',eight_psk_points,...
'GridOn',true,'AxesScaling',[false false]);
hold off;
close all;
clear hFig hAx;
```
#### 16-QAM & 32-QAM Constellation Diagrams
为了简化起见,这里提供了一个通用的方法来生成任意阶数的矩形QAM星座:
```matlab
function const_pts = rectangular_qam(m)
m_sqrt = sqrt(double(m));
if floor(m_sqrt)^2 ~= double(m)
error('m must be perfect square.');
end
real_part = (-floor((m_sqrt-1)/2):ceil((m_sqrt-1)/2))';
imag_part = (-floor((m_sqrt-1)/2):ceil((m_sqrt-1)/2))';
[real_grid,imag_grid] = meshgrid(real_part,imag_part);
const_pts = reshape(complex(real_grid(:),imag_grid(:)),[],1);
end
%% Example usage:
% Define modulation order M=16 or M=32
modulation_order = 16;
% Get corresponding constellation point locations
qam_constellations = rectangular_qam(modulation_order);
% Visualize results using scatter plots
subplot(1,2,1);
scatter(real(qam_constellations), imag(qam_constellations),'filled');
title(['Rectangular ', num2str(modulation_order), '-QAM']);
xlabel('Re'); ylabel('Im');
% Add reference labels at each plotted location
text(real(qam_constellations)+0.05, ...
imag(qam_constellations)-0.075, ...
cellstr(num2str((1:length(qam_constellations))')))
% Repeat same process but now for higher-order case where applicable
if modulation_order >= 32
subplot(1,2,2);
modulation_order_32 = 32;
qam_constellations_32 = rectangular_qam(modulation_order_32);
scatter(real(qam_constellations_32), imag(qam_constellations_32),'filled');
title(['Rectangular ', num2str(modulation_order_32), '-QAM']);
xlabel('Re'); ylabel('Im');
text(real(qam_constellations_32)+0.05, ...
imag(qam_constellations_32)-0.075, ...
cellstr(num2str((1:length(qam_constellations_32))')))
end
```
上述脚本展示了如何利用MATLAB中的基本功能绘制各种类型的数字通信系统的星座图[^1]。