目录
一、什么是KNN
二、为什么要在OSI的物理层中引入KNN
三、基于KNN算法的调制识别技术建模
四、数据生成部分仿真设计
4.0、摘要及项目文件结构
4.0.1、摘要
4.0.2、项目文件结构
4.1、各调制信号的调制器
4.1.1、BPSK调制器
4.1.2、QPSK调制器
4.1.3、MQAM调制器
4.2、生成训练样本集
4.3、高阶累积量特征提取
4.4、生成各调制方式的测试样本
4.4.1、生成 BPSK 测试样本
4.4.2、QPSK生成+提取高阶累积量
4.4.3、16QAM生成+提取高阶累积量
4.5、生成数据
五、调制识别部分仿真设计
六、变参对比
七、高阶累积量下的训练
一、什么是KNN
北邮 AI无线通信 | 基于KNN的调制模式识别(1)理论基础+仿真建模
二、为什么要在OSI的物理层中引入KNN
北邮 AI无线通信 | 基于KNN的调制模式识别(1)理论基础+仿真建模
三、基于KNN算法的调制识别技术建模
北邮 AI无线通信 | 基于KNN的调制模式识别(1)理论基础+仿真建模
四、数据生成部分仿真设计
本模块采用MATLAB实现,数据生成部分包括BPSK、QPSK、16QAM信号的生成及提取他们的高阶累积量特征值。
4.0、摘要及项目文件结构
4.0.1、摘要
针对调制识别实验的数据需求,本文基于MATLAB设计了一套完整的信号生成与特征提取流程。数据生成分为训练集与测试集两部分:训练集对BPSK、QPSK、16QAM和64QAM四种调制方式各生成十组随机比特序列,经调制映射后得到五百符号的复基带信号;测试集在此基础上引入加性高斯白噪声信道,按差异化信噪比范围分别生成各千组样本。特征提取环节统一计算信号的高阶累积量,包括二阶与四阶混合矩,进而推导出五个归一化累积量幅度作为特征值,以消除信号功率对特征尺度的影响。BPSK与QPSK采用MATLAB通信工具箱中的专用调制器对象,16QAM与64QAM则以qammod函数实现星座映射,且均启用单位平均功率约束以保证特征的可比性。最终数据以制表符分隔的文本格式存储,供下游分类器直接读取使用。
4.0.2、项目文件结构
data_generation/ │ ├── 📄 BPSKModulator.m # BPSK 调制器封装 ├── 📄 QPSKModulator.m # QPSK 调制器封装 ├── 📄 func_get_cumulants.m # 高阶累积量特征提取(核心) ├── 📄 getSample.m # 生成训练样本集 ├── 📄 getTestBPSK.m # 生成 BPSK 测试样本 ├── 📄 getTestQPSK.m # 生成 QPSK 测试样本 ├── 📄 getTest16QAM.m # 生成 16QAM 测试样本 │ └── 📁 digits/ # 生成的数据文件(27个) ├── sample.dat # 训练集:4类 × 10条 = 40行 │ ├── testBPSK--4.dat # BPSK 测试,SNR=-4dB ├── testBPSK--2.dat # BPSK 测试,SNR=-2dB ├── testBPSK-0.dat # BPSK 测试,SNR=0dB ├── testBPSK-2.dat # BPSK 测试,SNR=2dB ├── testBPSK-4.dat # BPSK 测试,SNR=4dB ├── testBPSK-6.dat # BPSK 测试,SNR=6dB ├── testBPSK-8.dat # BPSK 测试,SNR=8dB ├── testBPSK-10.dat # BPSK 测试,SNR=10dB │ ├── testQPSK--4.dat # QPSK 测试,SNR=-4dB ├── testQPSK--2.dat # QPSK 测试,SNR=-2dB ├── testQPSK-0.dat # QPSK 测试,SNR=0dB ├── testQPSK-2.dat # QPSK 测试,SNR=2dB ├── testQPSK-4.dat # QPSK 测试,SNR=4dB ├── testQPSK-6.dat # QPSK 测试,SNR=6dB ├── testQPSK-8.dat # QPSK 测试,SNR=8dB ├── testQPSK-10.dat # QPSK 测试,SNR=10dB │ ├── test16QAM-0.dat # 16QAM 测试,SNR=0dB ├── test16QAM-5.dat # 16QAM 测试,SNR=5dB ├── test16QAM-10.dat # 16QAM 测试,SNR=10dB ├── test16QAM-15.dat # 16QAM 测试,SNR=15dB ├── test16QAM-20.dat # 16QAM 测试,SNR=20dB ├── test16QAM-25.dat # 16QAM 测试,SNR=25dB ├── test16QAM-30.dat # 16QAM 测试,SNR=30dB ├── test16QAM-35.dat # 16QAM 测试,SNR=35dB └── test16QAM-40.dat # 16QAM 测试,SNR=40dB4.1、各调制信号的调制器
4.1.1、BPSK调制器
file name : BPSKModulator.m
function [out] = BPSKModulator(in) persistent Modulator if isempty(Modulator) Modulator=comm.BPSKModulator; %Modulator=comm.PSKModulator(2,'BitInput',true); end out = Modulator(in); end4.1.2、QPSK调制器
file name : QPSKModulator.m
function [out] = QPSKModulator(in) persistent Modulator if isempty(Modulator) Modulator=comm.QPSKModulator('BitInput',true); %demodLLR=comm.QPSKDemodulator('BitOutput',true,'DecisionMethod','Log-likelihood ratio','VarianceSource','Input port'); end out = Modulator(in); end4.1.3、MQAM调制器
16QAM 和 64QAM 没有单独的调制器.m文件,而是直接调用了 MATLAB 内置的qammod函数:
qammod(x, 16, 'InputType', 'bit', 'UnitAveragePower', true) % 16QAM4.2、生成训练样本集
file name : getSample.m
具体流程如下:
①对每种调制方式(BPSK、QPSK、16QAM、64QAM),各生成 rows=10 组随机比特,每组 length=500 个符号;
②通过各自的调制器(BPSKModulator / QPSKModulator / qammod)将比特映射为复基带调制符号;
③调用 func_get_cumulants.m 对每组 500 个符号计算 5 维四阶累积量特征向量;
④最后一列附加调制类型标签(BPSK=2, QPSK=4, 16QAM=16, 64QAM=64,写入 digits\sample.dat,供后续 Python 端 KNN 的 fit() 训练使用。
最终生成的sample.dat是一个40行 × 6列的矩阵(4类 × 10组/类 = 40行),每行格式为:
|C20| |C21| |C40| |C41| |C42| 标签代码如下:
%% Clean up clear all; clc; rows = 10; length = 500; NFeatures = 5; sample_data = zeros(rows, NFeatures + 1); %% Simulation parameters %% Simulate %BPSK % Generate random data data = randi([0 1], rows, length); % QAM mapping [data1] = data'; data_cell = mat2cell(data1,length,ones(1,rows)); signalData = cellfun(@(x) BPSKModulator(x), data_cell, 'UniformOutput',false); %signalData = cellfun(@(x) qammod(x, 2, 'InputType', 'bit', 'UnitAveragePower', true), data_cell, 'UniformOutput',false); signalData1 = cell2mat(signalData); signalData2 = mat2cell(signalData1',ones(1,rows),length); % get cumulants cumulants = cellfun(@(x) func_get_cumulants(x), signalData2, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); for row = 1: rows for i = 1:NFeatures sample_data(row, i) = cumulantsMat(row, i); end sample_data(row, NFeatures + 1) = 2; end %QPSK % Generate random data data = randi([0 1], rows, 2*length); % QAM mapping [data1] = data'; data_cell = mat2cell(data1,2*length,ones(1,rows)); signalData = cellfun(@(x) QPSKModulator(x), data_cell, 'UniformOutput',false); %signalData = cellfun(@(x) qammod(x, 4, 'InputType', 'bit', 'UnitAveragePower', true), data_cell, 'UniformOutput',false); signalData1 = cell2mat(signalData); signalData2 = mat2cell(signalData1',ones(1,rows),length); % get cumulants cumulants = cellfun(@(x) func_get_cumulants(x), signalData2, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); for row = 1: rows row_r = rows + row; for i = 1:NFeatures sample_data(row_r, i) = cumulantsMat(row, i); end sample_data(row_r, NFeatures + 1) = 4; end %16QAM % Generate random data data = randi([0 1], rows, 4*length); % QAM mapping [data1] = data'; data_cell = mat2cell(data1,4*length,ones(1,rows)); signalData = cellfun(@(x) qammod(x, 16, 'InputType', 'bit', 'UnitAveragePower', true), data_cell, 'UniformOutput',false); signalData1 = cell2mat(signalData); signalData2 = mat2cell(signalData1',ones(1,rows),length); % get cumulants cumulants = cellfun(@(x) func_get_cumulants(x), signalData2, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); for row = 1: rows row_r = 2*rows + row; for i = 1:NFeatures sample_data(row_r, i) = cumulantsMat(row, i); end sample_data(row_r, NFeatures + 1) = 16; end %64QAM % Generate random data data = randi([0 1], rows, 6*length); % QAM mapping [data1] = data'; data_cell = mat2cell(data1,6*length,ones(1,rows)); signalData = cellfun(@(x) qammod(x, 64, 'InputType', 'bit', 'UnitAveragePower', true), data_cell, 'UniformOutput',false); signalData1 = cell2mat(signalData); signalData2 = mat2cell(signalData1',ones(1,rows),length); % get cumulants cumulants = cellfun(@(x) func_get_cumulants(x), signalData2, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); for row = 1: rows row_r = 3*rows + row; for i = 1:NFeatures sample_data(row_r, i) = cumulantsMat(row, i); end sample_data(row_r, NFeatures + 1) = 64; end filename=['digits\sample.dat']; dlmwrite(filename,sample_data,'delimiter','\t','newline','pc'); %csvwrite('sample.txt', sample_data);4.3、高阶累积量特征提取
file_name : func_get_cumulants.m
作用:生成了5个四阶及以下的累积量。
function [cumulants] = func_get_cumulants(signalData) shape = size(signalData); H = shape(1); L = shape(2); cumulants = zeros(H, L); for row = 1:H M20 = sum(signalData(row,:).^2)/L; M21 = sum(abs(signalData(row,:)).^2)/L; M40 = sum(signalData(row,:).^4)/L; M41 = sum(abs(signalData(row,:)).^2.*signalData(row,:).^2)/L; M42 = sum(abs(signalData(row,:)).^4)/L; C20 = M20; C21 = M21; C40 = M40 - 3*M20^2; C41 = M41 - 3*M20*M21; C42 = M42 - abs(M20)^2 - 2*M21^2; C21_modify = C21; C20_norm = C20/(C21_modify^2); C21_norm = C21/(C21_modify^2); C40_norm = C40/(C21_modify^2); C41_norm = C41/(C21_modify^2); C42_norm = C42/(C21_modify^2); cumulants(row, 1) = abs(C20_norm); cumulants(row, 2) = abs(C21_norm); cumulants(row, 3) = abs(C40_norm); cumulants(row, 4) = abs(C41_norm); cumulants(row, 5) = abs(C42_norm); end4.4、生成各调制方式的测试样本
4.4.1、生成 BPSK 测试样本
①生成1000路信号流,每一路500个符号,提取四阶累积量的5个特征,仿真噪声
%% Clean up clear all; clc; rows = 1000; cols = 500; NFeatures = 5; test_data = zeros(rows, NFeatures); SNR = [-4:2:10];②映射、加噪、获取高阶累积量
for i = 1:length(SNR) %% Simulation parameters M = 2; snr = SNR(i); %% Simulate %BPSK % Generate random data data = randi([0 1], rows, cols); % PSK 映射 [data1] = data'; data_cell = mat2cell(data1,cols,ones(1,rows)); signalData = cellfun(@(x) BPSKModulator(x), data_cell, 'UniformOutput',false); signalDataMat = cell2mat(signalData); dataMod = mat2cell(signalDataMat',ones(1,rows),cols); % AWGN 加噪 dataRx = cellfun(@(x) awgn(x, snr), dataMod, 'UniformOutput',false); % 获取高阶累积量 cumulants = cellfun(@(x) func_get_cumulants(x), dataRx, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); test_data = cumulantsMat;③保存在digits文件夹里
%% save filename=['digits\testBPSK-',num2str(snr),'.dat']; dlmwrite(filename,test_data,'delimiter','\t','newline','pc'); end④完整代码
%% Clean up clear all; clc; rows = 1000; cols = 500; NFeatures = 5; test_data = zeros(rows, NFeatures); SNR = [-4:2:10]; for i = 1:length(SNR) %% Simulation parameters M = 2; snr = SNR(i); %% Simulate %BPSK % Generate random data data = randi([0 1], rows, cols); % PSK 映射 [data1] = data'; data_cell = mat2cell(data1,cols,ones(1,rows)); signalData = cellfun(@(x) BPSKModulator(x), data_cell, 'UniformOutput',false); signalDataMat = cell2mat(signalData); dataMod = mat2cell(signalDataMat',ones(1,rows),cols); % AWGN 加噪 dataRx = cellfun(@(x) awgn(x, snr), dataMod, 'UniformOutput',false); % 获取高阶累积量 cumulants = cellfun(@(x) func_get_cumulants(x), dataRx, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); test_data = cumulantsMat; %% save filename=['digits\testBPSK-',num2str(snr),'.dat']; dlmwrite(filename,test_data,'delimiter','\t','newline','pc'); end4.4.2、QPSK生成+提取高阶累积量
%% Clean up clear all; clc; rows = 1000; cols = 500*2; NFeatures = 5; test_data = zeros(rows, NFeatures); SNR = [-4:2:10]; for i = 1:length(SNR) %% Simulation parameters M = 4; snr = SNR(i); %% Simulate %QPSK % Generate random data data = randi([0 1], rows, cols); % PSK 映射 [data1] = data'; data_cell = mat2cell(data1,cols,ones(1,rows)); signalData = cellfun(@(x) QPSKModulator(x), data_cell, 'UniformOutput',false); %signalData = cellfun(@(x) qammod(x, M, 'InputType', 'bit', 'UnitAveragePower', true), data_cell, 'UniformOutput',false); signalDataMat = cell2mat(signalData); dataMod = mat2cell(signalDataMat',ones(1,rows),cols/2); % AWGN 加噪 dataRx = cellfun(@(x) awgn(x, snr), dataMod, 'UniformOutput',false); % 获取高阶累积量 cumulants = cellfun(@(x) func_get_cumulants(x), dataRx, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); test_data = cumulantsMat; %% save filename=['digits\testQPSK-',num2str(snr),'.dat']; dlmwrite(filename,test_data,'delimiter','\t','newline','pc'); end4.4.3、16QAM生成+提取高阶累积量
%% Clean up clear all; clc; rows = 1000; cols = 500*4; NFeatures = 5; test_data = zeros(rows, NFeatures); SNR = [0:5:40]; for i = 1:length(SNR) %% Simulation parameters M = 16; snr = SNR(i); %% Simulate %16QAM % Generate random data data = randi([0 1], rows, cols); % QAM 映射 [data1] = data'; data_cell = mat2cell(data1,cols,ones(1,rows)); signalData = cellfun(@(x) qammod(x, M, 'InputType', 'bit', 'UnitAveragePower', true), data_cell, 'UniformOutput',false); signalDataMat = cell2mat(signalData); dataMod = mat2cell(signalDataMat',ones(1,rows),cols/4); % AWGN 加噪 dataRx = cellfun(@(x) awgn(x, snr), dataMod, 'UniformOutput',false); % 获取高阶累积量 cumulants = cellfun(@(x) func_get_cumulants(x), dataRx, 'UniformOutput',false); cumulantsMat = cell2mat(cumulants); test_data = cumulantsMat; %% save filename=['digits\test16QAM-',num2str(snr),'.dat']; dlmwrite(filename,test_data,'delimiter','\t','newline','pc'); end4.5、生成数据
运行getSample.m文件,生成原始训练数据,保存在digits文件夹中。运行getTestBPSK.m、getTestQPSK.m、getTest16QAM.m三个文件,其自动生成的高阶累积量会保存在digits文件夹中。
其中,sample.dat中,基于每种调制方式(BPSK、QPSK、16QAM、64QAM)的10组随机比特计算出来的10组六维特征向量(五维累积量+一维调制方式标签)的数据集展示如下:
1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 1 1 2 2 2 2 0.02 1 0.9988 0.04 1.0004 4 0.036 1 0.99611 0.072 1.0013 4 0.008 1 0.99981 0.016 1.0001 4 0.032 1 0.99693 0.064 1.001 4 0.032 1 0.99693 0.064 1.001 4 0.036 1 0.99611 0.072 1.0013 4 0.068 1 0.98613 0.136 1.0046 4 0.032 1 0.99693 0.064 1.001 4 0.084 1 0.97883 0.168 1.0071 4 0.028 1 0.99765 0.056 1.0008 4 0.028479 0.99364 0.69829 0.03193 0.67986 16 0.044295 0.99522 0.69803 0.084636 0.68125 16 0.10501 0.97962 0.68429 0.17417 0.70604 16 0.013941 1.0196 0.61954 0.027917 0.68389 16 0.08545 1.0016 0.67724 0.10325 0.67983 16 0.062631 0.99681 0.61456 0.095507 0.70635 16 0.084522 1.0179 0.64446 0.12043 0.67963 16 0.012512 1.0452 0.68527 0.02295 0.64285 16 0.017658 1.0032 0.71747 0.038962 0.66538 16 0.047345 0.99364 0.64234 0.071206 0.69901 16 0.040133 0.98907 0.49595 0.022389 0.67857 64 0.060916 0.95908 0.64488 0.093486 0.6597 64 0.017047 1.0649 0.62845 0.053931 0.57328 64 0.093459 0.9715 0.51114 0.13754 0.67127 64 0.072024 1.0392 0.65878 0.066526 0.59016 64 0.093335 0.9412 0.54988 0.13972 0.68534 64 0.039449 1.0274 0.58778 0.057334 0.61528 64 0.058832 0.95663 0.66257 0.11553 0.64621 64 0.023588 1.0042 0.63779 0.0062768 0.60082 64 0.021052 0.97222 0.6135 0.041741 0.62874 64五、调制识别部分仿真设计
北邮 AI无线通信 | 基于KNN的调制模式识别(3)依托于Jupyter Notebook对样本信号调制模式识别的仿真设计(modulation_recognition_module)
六、变参对比
北邮 AI无线通信 | 基于KNN的调制模式识别(4)样本信号调制模式识别仿真设计的参数探究
七、高阶累积量下的训练
北邮 AI无线通信 | 基于KNN的调制模式识别(4)样本信号调制模式识别仿真设计的参数探究