├── BPSK_OFDM_rician_OneDimension.m ├── OFDM_rician_OneDimension.m ├── README.md └── helperModClassFrameStoreTest.m /BPSK_OFDM_rician_OneDimension.m: -------------------------------------------------------------------------------- 1 | clear; 2 | clc; 3 | close all; 4 | %10是信噪比,100是自定义产生的帧的数量 5 | BERTool_QPSK_OFDM_RicianChannel_LSEstimation(10, 10000) 6 | function [BER, numBits] = BERTool_QPSK_OFDM_RicianChannel_LSEstimation(EbNo, Nframes) 7 | 8 | %%%%%%%%%%%%%%%%%%%%%%%%% 9 | %子载波数量256 10 | %循环前缀64 11 | %一帧数据就是2×(256+64)=2×320大小 12 | %QPSK的调制方式 13 | persistent FullOperatingTime 14 | 15 | % Display Line on the Start of Imitation Modeling 16 | disp('======================================'); 17 | % Start Time 18 | tStart = clock; 19 | % Total Duration of Imitation Modeling 20 | % Saving for each trials. To restart need 'clear all' command. 21 | if isempty(FullOperatingTime) 22 | FullOperatingTime = 0; 23 | end 24 | 25 | 26 | %%%%% Initial Information Source %%%%% 27 | 28 | % Symbol Rate 29 | Rs = 100e3; 30 | % Symbol Duration 31 | % Ts = 1/Rs; 32 | 33 | %%%%% QPSK Modulation %%%%% 34 | 35 | % Number of Bits in QPSK Symbol by definition 36 | k = 1; 37 | 38 | 39 | % QPSK Modulator Object 40 | % QPSKModulator = comm.QPSKModulator( ... 41 | % 'PhaseOffset', pi/4, ... 42 | % 'BitInput', true, ... 43 | % 'SymbolMapping', 'Gray' ... 44 | % ); 45 | % 46 | % % QPSK Demodulator Object 47 | % QPSKDemodulator = comm.QPSKDemodulator( ... 48 | % 'PhaseOffset', QPSKModulator.PhaseOffset, ... 49 | % 'BitOutput', QPSKModulator.BitInput, ... 50 | % 'SymbolMapping', QPSKModulator.SymbolMapping, ... 51 | % 'DecisionMethod', 'Hard decision' ... 52 | % ); 53 | BPSKModulator = comm.BPSKModulator('PhaseOffset',pi/4); 54 | 55 | BPSKDemodulator = comm.BPSKDemodulator( 'PhaseOffset', BPSKModulator.PhaseOffset, ... 56 | 'DecisionMethod', 'Hard decision' ... 57 | ); 58 | %%%%% OFDM Modulation %%%%% 59 | 60 | % Number of Subcarriers (equal to Number of FFT points) 61 | numSC = 256; 62 | 63 | % Guard Bands Subcarriers 64 | GuardBandSC = [10; 10]; 65 | 66 | % Central Null Subcarrier 67 | DCNull = true; 68 | DCNullSC = numSC/2 + 1; 69 | 70 | % Number of Pilot Subcarriers 71 | numPilotSC = 10; 72 | % Location of Pilot Subcarriers 73 | PilotSC = round(linspace(GuardBandSC(1) + 5, numSC - GuardBandSC(2) - 6, numPilotSC))'; 74 | 75 | % Length of Cyclic Prefix 76 | lenCP = numSC/4; 77 | 78 | 79 | % OFDM Modulator Object 80 | OFDMModulator = comm.OFDMModulator( ... 81 | 'FFTLength', numSC, ... 82 | 'NumGuardBandCarriers', GuardBandSC, ... 83 | 'InsertDCNull', DCNull, ... 84 | 'PilotInputPort', true, ... 85 | 'PilotCarrierIndices', PilotSC, ... 86 | 'CyclicPrefixLength', lenCP ... 87 | ); 88 | 89 | % OFDM Demodulator Object 90 | OFDMDemodulator = comm.OFDMDemodulator(OFDMModulator); 91 | 92 | 93 | % Number of Data Subcarriers 94 | numDataSC = info(OFDMModulator).DataInputSize(1); 95 | 96 | % Size of Data Frame 97 | szDataFrame = [k*numDataSC 1]; 98 | % Size of Pilot Frame 99 | szPilotFrame = info(OFDMModulator).PilotInputSize; 100 | 101 | 102 | %%%%% Transionospheric Communication Channel %%%%% 103 | 104 | % Discrete Paths Relative Delays 105 | PathDelays = [0 0.01 0.02 0.03]; 106 | % Discrete Paths Average Gains 107 | PathAvGains = [0 -6 -9 -12]; 108 | % Discrete Paths K Factors 109 | K = 10; 110 | % Max Doppler Frequency Shift 111 | fD = 200; 112 | 113 | % Rician Channel Object 114 | RicianChannel = comm.RicianChannel( ... 115 | 'SampleRate', Rs, ...%采样率 116 | 'PathDelays', PathDelays, ...%路径延迟 117 | 'AveragePathGains', PathAvGains, ...%路径增益 118 | 'NormalizePathGains', true, ... 119 | 'KFactor', K, ...%K因子 120 | 'MaximumDopplerShift', fD, ...%最大多普勒频移 121 | 'DirectPathDopplerShift', zeros(size(K)), ... 122 | 'DirectPathInitialPhase', zeros(size(K)), ... 123 | 'DopplerSpectrum', doppler('Jakes') ...%采用的Jakes信道模型 124 | ); 125 | 126 | % Delay in Rician Channel Object 127 | ChanDelay = info(RicianChannel).ChannelFilterDelay; 128 | 129 | % AWGN Channel Object 130 | AWGNChannel = comm.AWGNChannel( ... 131 | 'NoiseMethod', 'Signal to noise ratio (SNR)', ... 132 | 'SNR', EbNo + 10*log10(k) + 10*log10(numDataSC/numSC) ... 133 | ); 134 | 135 | 136 | %%%%% Imitation Modeling %%%%% 137 | 138 | % Import Java class for BERTool 139 | % import com.mathworks.toolbox.comm.BERTool; 140 | 141 | % BER Calculator Object 142 | BERCalculater = comm.ErrorRate; 143 | % BER Intermediate Variable 144 | BERIm = zeros(3,1); 145 | 146 | perFrameLength = numSC+ lenCP; 147 | %% 初始化一个存储帧数据的容器 148 | % 初始化一个训练集label的容量 149 | initialLabel = zeros(1,k*numDataSC); 150 | % 初始化容器 151 | frameStore = helperModClassFrameStoreTest(... 152 | Nframes,perFrameLength,initialLabel); 153 | 154 | 155 | %% 仿真发送和接收 156 | tLoop1 = clock; 157 | % 一个循环一帧数据 158 | for frameNum = 1:Nframes 159 | % >>> Transmitter >>> 160 | 161 | % Generation of Data Bits 162 | BitsTx = randi([0 1], szDataFrame); 163 | 164 | % QPSK Modulation 165 | % SignalTx1 = QPSKModulator(BitsTx); 166 | 167 | SignalTx1 = BPSKModulator(BitsTx); 168 | 169 | % Generation of Pilot Signals 170 | PilotSignalTx = complex(ones(szPilotFrame), zeros(szPilotFrame)); 171 | % OFDM Modulation 172 | SignalTx2 = OFDMModulator(SignalTx1, PilotSignalTx); 173 | 174 | % Power of Transmitted Signal 175 | SignalTxPower = var(SignalTx2); 176 | 177 | 178 | % >>> Transionospheric Communication Channel >>> 179 | 180 | % Adding zero samples to the end of Transmitted Signal 181 | % to not lose shifted samples caused by delay after Rician Channel 182 | SignalTx2 = [SignalTx2; zeros(ChanDelay, 1)]; 183 | % Rician Channel 184 | SignalChan1 = RicianChannel(SignalTx2); 185 | % Removing first ChanDelay samples and 186 | % selection of Channel's Signal related to Transmitted Signal 187 | SignalChan1 = SignalChan1(ChanDelay + 1 : end); 188 | 189 | % AWGN Channel 190 | AWGNChannel.SignalPower = SignalTxPower; 191 | SignalChan2 = AWGNChannel(SignalChan1); 192 | 193 | % 存储发送帧 194 | frame = reshape(SignalChan2,[],1);%转换成一列的矩阵,方便进行计算 195 | perFrameLabel = BitsTx; 196 | 197 | %把发送帧添加到帧容器 198 | add(frameStore, frame, perFrameLabel);%modulationTypes(modType)); 199 | 200 | % >>> Receiver >>> 201 | 202 | % OFDM Demodulation 203 | [SignalRx1, PilotSignalRx] = OFDMDemodulator(SignalChan2); 204 | 205 | % LS Channel Estimation 206 | % Channel Frequency Response 207 | ChanFR_dp = PilotSignalRx ./ PilotSignalTx; 208 | ChanFR_int = interp1( ... 209 | PilotSC, ... 210 | ChanFR_dp, ... 211 | GuardBandSC(1) + 1 : numSC - GuardBandSC(2), ... 212 | 'pchip' ... 213 | ); 214 | ChanFR_int([PilotSC; DCNullSC] - GuardBandSC(1)) = []; 215 | % LS Solution 216 | SignalRx2 = SignalRx1 ./ ChanFR_int.'; 217 | 218 | % QPSK Demodulation 219 | % BitsRx = QPSKDemodulator(SignalRx2); 220 | BitsRx = BPSKDemodulator(SignalRx2); 221 | 222 | % BER Calculation 223 | BERIm = BERCalculater(BitsTx, BitsRx); 224 | 225 | end 226 | 227 | %% 划分数据集为训练集、验证集、测试集 228 | % 训练集百分之占比 229 | percentTrainingSamples = 80; 230 | % 验证集百分之占比 231 | percentValidationSamples = 10; 232 | % 测试集百分之占比 233 | percentTestSamples = 10; 234 | % 划分数据集 235 | [mcfsTraining,mcfsValidation,mcfsTest] = splitData(frameStore,... 236 | [percentTrainingSamples,percentValidationSamples,percentTestSamples]); 237 | 238 | % 从训练集中返回存储的帧和相应的标签。 239 | [rxTraining,rxTrainingLabel] = get(mcfsTraining); 240 | 241 | % 从验证集中返回存储的帧和相应的标签。 242 | [rxValidation,rxValidationLabel] = get(mcfsValidation); 243 | 244 | % 从测试集中返回存储的帧和相应的标签。 245 | [rxTest,rxTestLabel] = get(mcfsTest); 246 | % 存储划分的数据为.mat文件 247 | save bpsk-data10000 rxTraining rxTrainingLabel rxValidation rxValidationLabel rxTest rxTestLabel; 248 | 249 | 250 | tLoop2 = clock; 251 | 252 | %% 误码率计算 253 | BER = BERIm(1); 254 | numBits = BERIm(3); 255 | disp(['BER = ', num2str(BERIm(1), '%.5g'), ' at Eb/No = ', num2str(EbNo), ' dB']); 256 | disp(['Number of bits = ', num2str(BERIm(3))]); 257 | disp(['Number of errors = ', num2str(BERIm(2))]); 258 | 259 | 260 | % Performance of Imitation Modeling 261 | Perfomance = BERIm(3) / etime(tLoop2, tLoop1); 262 | disp(['Perfomance = ', num2str(Perfomance), ' bit/sec']); 263 | 264 | % Duration of this Imitation Modeling 265 | duration = etime(clock, tStart); 266 | disp(['Operating time = ', num2str(duration), ' sec']); 267 | 268 | % Total Duration of Imitation Modeling 269 | FullOperatingTime = FullOperatingTime + duration; 270 | assignin('base', 'FullOperatingTime', FullOperatingTime); 271 | 272 | end -------------------------------------------------------------------------------- /OFDM_rician_OneDimension.m: -------------------------------------------------------------------------------- 1 | clear; 2 | clc; 3 | close all; 4 | %10是信噪比,100是自定义产生的帧的数量 5 | BERTool_QPSK_OFDM_RicianChannel_LSEstimation(10, 100) 6 | function [BER, numBits] = BERTool_QPSK_OFDM_RicianChannel_LSEstimation(EbNo, Nframes) 7 | 8 | %%%%%%%%%%%%%%%%%%%%%%%%% 9 | %子载波数量256 10 | %一个OFDM2个符号 11 | %循环前缀64 12 | %一帧数据就是2×(256+64)=2×320大小 13 | %QPSK的调制方式 14 | persistent FullOperatingTime 15 | 16 | % Display Line on the Start of Imitation Modeling 17 | disp('======================================'); 18 | % Start Time 19 | tStart = clock; 20 | % Total Duration of Imitation Modeling 21 | % Saving for each trials. To restart need 'clear all' command. 22 | if isempty(FullOperatingTime) 23 | FullOperatingTime = 0; 24 | end 25 | 26 | 27 | %%%%% Initial Information Source %%%%% 28 | 29 | % Symbol Rate 30 | Rs = 100e3; 31 | % Symbol Duration 32 | Ts = 1/Rs; 33 | 34 | %%%%% QPSK Modulation %%%%% 35 | 36 | % Number of Bits in QPSK Symbol by definition 37 | k = 2; 38 | 39 | % QPSK Modulator Object 40 | QPSKModulator = comm.QPSKModulator( ... 41 | 'PhaseOffset', pi/4, ... 42 | 'BitInput', true, ... 43 | 'SymbolMapping', 'Gray' ... 44 | ); 45 | 46 | % QPSK Demodulator Object 47 | QPSKDemodulator = comm.QPSKDemodulator( ... 48 | 'PhaseOffset', QPSKModulator.PhaseOffset, ... 49 | 'BitOutput', QPSKModulator.BitInput, ... 50 | 'SymbolMapping', QPSKModulator.SymbolMapping, ... 51 | 'DecisionMethod', 'Hard decision' ... 52 | ); 53 | 54 | 55 | %%%%% OFDM Modulation %%%%% 56 | 57 | % Number of Subcarriers (equal to Number of FFT points) 58 | numSC = 256; 59 | 60 | % Guard Bands Subcarriers 61 | GuardBandSC = [10; 10]; 62 | 63 | % Central Null Subcarrier 64 | DCNull = true; 65 | DCNullSC = numSC/2 + 1; 66 | 67 | % Number of Pilot Subcarriers 68 | numPilotSC = 10; 69 | % Location of Pilot Subcarriers 70 | PilotSC = round(linspace(GuardBandSC(1) + 5, numSC - GuardBandSC(2) - 6, numPilotSC))'; 71 | 72 | % Length of Cyclic Prefix 73 | lenCP = numSC/4; 74 | 75 | 76 | % OFDM Modulator Object 77 | OFDMModulator = comm.OFDMModulator( ... 78 | 'FFTLength', numSC, ... 79 | 'NumGuardBandCarriers', GuardBandSC, ... 80 | 'InsertDCNull', DCNull, ... 81 | 'PilotInputPort', true, ... 82 | 'PilotCarrierIndices', PilotSC, ... 83 | 'CyclicPrefixLength', lenCP ... 84 | ); 85 | 86 | % OFDM Demodulator Object 87 | OFDMDemodulator = comm.OFDMDemodulator(OFDMModulator); 88 | 89 | 90 | % Number of Data Subcarriers 91 | numDataSC = info(OFDMModulator).DataInputSize(1); 92 | 93 | % Size of Data Frame 94 | szDataFrame = [k*numDataSC 1]; 95 | % Size of Pilot Frame 96 | szPilotFrame = info(OFDMModulator).PilotInputSize; 97 | 98 | 99 | %%%%% Transionospheric Communication Channel %%%%% 100 | 101 | % Discrete Paths Relative Delays 102 | PathDelays = [0 0.01 0.02 0.03]; 103 | % Discrete Paths Average Gains 104 | PathAvGains = [0 -6 -9 -12]; 105 | % Discrete Paths K Factors 106 | K = 10; 107 | % Max Doppler Frequency Shift 108 | fD = 200; 109 | 110 | % Rician Channel Object 111 | RicianChannel = comm.RicianChannel( ... 112 | 'SampleRate', Rs, ...%采样率 113 | 'PathDelays', PathDelays, ...%路径延迟 114 | 'AveragePathGains', PathAvGains, ...%路径增益 115 | 'NormalizePathGains', true, ... 116 | 'KFactor', K, ...%K因子 117 | 'MaximumDopplerShift', fD, ...%最大多普勒频移 118 | 'DirectPathDopplerShift', zeros(size(K)), ... 119 | 'DirectPathInitialPhase', zeros(size(K)), ... 120 | 'DopplerSpectrum', doppler('Jakes') ...%采用的Jakes信道模型 121 | ); 122 | 123 | % Delay in Rician Channel Object 124 | ChanDelay = info(RicianChannel).ChannelFilterDelay; 125 | 126 | % AWGN Channel Object 127 | AWGNChannel = comm.AWGNChannel( ... 128 | 'NoiseMethod', 'Signal to noise ratio (SNR)', ... 129 | 'SNR', EbNo + 10*log10(k) + 10*log10(numDataSC/numSC) ... 130 | ); 131 | 132 | 133 | %%%%% Imitation Modeling %%%%% 134 | 135 | % Import Java class for BERTool 136 | % import com.mathworks.toolbox.comm.BERTool; 137 | 138 | % BER Calculator Object 139 | BERCalculater = comm.ErrorRate; 140 | % BER Intermediate Variable 141 | BERIm = zeros(3,1); 142 | 143 | perFrameLength = numSC+ lenCP; 144 | %% 初始化一个存储帧数据的容器 145 | % 初始化一个训练集label的容量 146 | initialLabel = zeros(1,k*numDataSC); 147 | % 初始化容器 148 | frameStore = helperModClassFrameStoreTest(... 149 | Nframes,perFrameLength,initialLabel); 150 | 151 | 152 | %% 仿真发送和接收 153 | tLoop1 = clock; 154 | % 一个循环一帧数据 155 | for frameNum = 1:Nframes 156 | % >>> Transmitter >>> 157 | 158 | % Generation of Data Bits 159 | BitsTx = randi([0 1], szDataFrame); 160 | 161 | % QPSK Modulation 162 | SignalTx1 = QPSKModulator(BitsTx); 163 | 164 | % Generation of Pilot Signals 165 | PilotSignalTx = complex(ones(szPilotFrame), zeros(szPilotFrame)); 166 | % OFDM Modulation 167 | SignalTx2 = OFDMModulator(SignalTx1, PilotSignalTx); 168 | 169 | % Power of Transmitted Signal 170 | SignalTxPower = var(SignalTx2); 171 | 172 | 173 | % >>> Transionospheric Communication Channel >>> 174 | 175 | % Adding zero samples to the end of Transmitted Signal 176 | % to not lose shifted samples caused by delay after Rician Channel 177 | SignalTx2 = [SignalTx2; zeros(ChanDelay, 1)]; 178 | % Rician Channel 179 | SignalChan1 = RicianChannel(SignalTx2); 180 | % Removing first ChanDelay samples and 181 | % selection of Channel's Signal related to Transmitted Signal 182 | SignalChan1 = SignalChan1(ChanDelay + 1 : end); 183 | 184 | % AWGN Channel 185 | AWGNChannel.SignalPower = SignalTxPower; 186 | SignalChan2 = AWGNChannel(SignalChan1); 187 | 188 | % 存储发送帧 189 | frame = reshape(SignalChan2,[],1);%转换成一列的矩阵,方便进行计算 190 | perFrameLabel = BitsTx; 191 | 192 | %把发送帧添加到帧容器 193 | add(frameStore, frame, perFrameLabel);%modulationTypes(modType)); 194 | 195 | % >>> Receiver >>> 196 | 197 | % OFDM Demodulation 198 | [SignalRx1, PilotSignalRx] = OFDMDemodulator(SignalChan2); 199 | 200 | % LS Channel Estimation 201 | % Channel Frequency Response 202 | ChanFR_dp = PilotSignalRx ./ PilotSignalTx; 203 | ChanFR_int = interp1( ... 204 | PilotSC, ... 205 | ChanFR_dp, ... 206 | GuardBandSC(1) + 1 : numSC - GuardBandSC(2), ... 207 | 'pchip' ... 208 | ); 209 | ChanFR_int([PilotSC; DCNullSC] - GuardBandSC(1)) = []; 210 | % LS Solution 211 | SignalRx2 = SignalRx1 ./ ChanFR_int.'; 212 | 213 | % QPSK Demodulation 214 | BitsRx = QPSKDemodulator(SignalRx2); 215 | 216 | % BER Calculation 217 | BERIm = BERCalculater(BitsTx, BitsRx); 218 | 219 | end 220 | 221 | %% 划分数据集为训练集、验证集、测试集 222 | % 训练集百分之占比 223 | percentTrainingSamples = 80; 224 | % 验证集百分之占比 225 | percentValidationSamples = 10; 226 | % 测试集百分之占比 227 | percentTestSamples = 10; 228 | % 划分数据集 229 | [mcfsTraining,mcfsValidation,mcfsTest] = splitData(frameStore,... 230 | [percentTrainingSamples,percentValidationSamples,percentTestSamples]); 231 | 232 | % 从训练集中返回存储的帧和相应的标签。 233 | [rxTraining,rxTrainingLabel] = get(mcfsTraining); 234 | 235 | % 从验证集中返回存储的帧和相应的标签。 236 | [rxValidation,rxValidationLabel] = get(mcfsValidation); 237 | 238 | % 从测试集中返回存储的帧和相应的标签。 239 | [rxTest,rxTestLabel] = get(mcfsTest); 240 | % 存储划分的数据为.mat文件 241 | save data100 rxTraining rxTrainingLabel rxValidation rxValidationLabel rxTest rxTestLabel; 242 | 243 | 244 | tLoop2 = clock; 245 | 246 | %% 误码率计算 247 | BER = BERIm(1); 248 | numBits = BERIm(3); 249 | disp(['BER = ', num2str(BERIm(1), '%.5g'), ' at Eb/No = ', num2str(EbNo), ' dB']); 250 | disp(['Number of bits = ', num2str(BERIm(3))]); 251 | disp(['Number of errors = ', num2str(BERIm(2))]); 252 | 253 | 254 | % Performance of Imitation Modeling 255 | Perfomance = BERIm(3) / etime(tLoop2, tLoop1); 256 | disp(['Perfomance = ', num2str(Perfomance), ' bit/sec']); 257 | 258 | % Duration of this Imitation Modeling 259 | duration = etime(clock, tStart); 260 | disp(['Operating time = ', num2str(duration), ' sec']); 261 | 262 | % Total Duration of Imitation Modeling 263 | FullOperatingTime = FullOperatingTime + duration; 264 | assignin('base', 'FullOperatingTime', FullOperatingTime); 265 | 266 | end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 莱斯信道下的OFDM仿真,进行发送数据帧的存储,存储为深度学习中训练模型的训练集、测试集、验证集 2 | 3 | ## 1 OFDM参数 4 | 该目录下有两种调制方式的发送机QPSK和BPSK 5 | 6 | -- BPSK_OFDM_rician_OneDimension.m 是BPSK的调制方式 7 | 8 | -- OFDM_rician_OneDimension.m 是QPSK的调制方式 9 | 10 | 根据需求,运行程序即可 11 | 12 | 子载波数量256 13 | 一个OFDM有2个符号 14 | 循环前缀子载波的1/4,为64 15 | 16 | ## 2 数据集参数 17 | 18 | 发送端产生的一帧数据是2×225 = 450 ,作为数据集的标签 19 | 20 | 接受端一帧数据就是2×(256+64)=2*320 = 640,2表示IQ两个信号,640长度的帧数据作为数据集的每帧训练数据 21 | ## 3 莱斯信道的参数 22 | 23 | ``` 24 | RicianChannel = comm.RicianChannel( ... 25 | 'SampleRate', Rs, ...%采样率 26 | 'PathDelays', PathDelays, ...%路径延迟 27 | 'AveragePathGains', PathAvGains, ...%路径增益 28 | 'NormalizePathGains', true, ... 29 | 'KFactor', K, ...%K因子 30 | 'MaximumDopplerShift', fD, ...%最大多普勒频移 31 | 'DirectPathDopplerShift', zeros(size(K)), ... 32 | 'DirectPathInitialPhase', zeros(size(K)), ... 33 | 'DopplerSpectrum', doppler('Jakes') ...%采用的Jakes信道模型 34 | ); 35 | 36 | ``` 37 | ## 4 数据存储的思路 38 | 39 | 首先初始化一个存储所有帧数据的容器 40 | 41 | ``` 42 | % 144行 43 | frameStore = helperModClassFrameStoreTest(... 44 | Nframes,perFrameLength,initialLabel); 45 | ``` 46 | 47 | 再把每一帧数据,放入到该容器当中 48 | 49 | ``` 50 | % 193行 51 | add(frameStore, frame, perFrameLabel);%modulationTypes(modType)); 52 | ``` 53 | 54 | 最后划分容器里所有数据分为训练集、验证集、测试集 55 | 56 | ``` 57 | % 223行 58 | [mcfsTraining,mcfsValidation,mcfsTest] = splitData(frameStore,... 59 | [percentTrainingSamples,percentValidationSamples,percentTestSamples]); 60 | 61 | % 从训练集中返回存储的帧和相应的标签。 62 | [rxTraining,rxTrainingLabel] = get(mcfsTraining); 63 | 64 | % 从验证集中返回存储的帧和相应的标签。 65 | [rxValidation,rxValidationLabel] = get(mcfsValidation); 66 | 67 | % 从测试集中返回存储的帧和相应的标签。 68 | [rxTest,rxTestLabel] = get(mcfsTest); 69 | ``` 70 | 71 | ## 5 运行 72 | 73 | 实验环境:MATLAB 74 | 75 | 自定义发送帧的数量:修改第五行第二个参数 76 | 77 | 自定义信噪比:修改第五行,第一个参数 78 | 79 | 80 | 81 | ## ## 6 源码下载 82 | https://github.com/823316627bandeng/OFDM_Rician_simulation_for_DeepLearning 83 | 84 | -------------------------------------------------------------------------------- /helperModClassFrameStoreTest.m: -------------------------------------------------------------------------------- 1 | classdef helperModClassFrameStoreTest < handle 2 | 3 | 4 | properties (SetAccess=private) 5 | %NumFrames Number of frames in the frame store 6 | NumFrames = 0 7 | %MaximumNumFrames Capacity of frame store 8 | MaximumNumFrames 9 | %SamplesPerFrame Samples per frame 10 | SamplesPerFrame 11 | %Labels Set of expected labels 12 | Labels 13 | 14 | 15 | 16 | Labelnumber = 0 17 | end 18 | 19 | properties (Access=private) 20 | Frames 21 | Label 22 | 23 | end 24 | 25 | methods 26 | %% 把帧封装成一个对象 27 | function obj = helperModClassFrameStoreTest(varargin) 28 | 29 | inputs = inputParser; 30 | addRequired(inputs, 'MaximumNumFrames') 31 | addRequired(inputs, 'SamplesPerFrame') 32 | addRequired(inputs, 'Labels') 33 | parse(inputs, varargin{:}) 34 | 35 | obj.SamplesPerFrame = inputs.Results.SamplesPerFrame;%每种调制方式的数量 36 | obj.MaximumNumFrames = inputs.Results.MaximumNumFrames;%一帧数据的大小 37 | obj.Labels = inputs.Results.Labels;%标签的大小 38 | 39 | obj.Frames = ... 40 | zeros(obj.SamplesPerFrame,obj.MaximumNumFrames); 41 | obj.Label = repmat(obj.Labels,obj.MaximumNumFrames,1); 42 | 43 | 44 | end 45 | 46 | %% 添加帧 47 | function add(obj,frames,label,varargin) 48 | 49 | %一帧占一列 50 | numNewFrames = size(frames,2); 51 | 52 | 53 | FrameStartIdx = obj.NumFrames+1; 54 | LabelStartIdx = obj.Labelnumber +1; 55 | 56 | 57 | FrameEndIdx = obj.NumFrames+numNewFrames; 58 | obj.Frames(:,FrameStartIdx:FrameEndIdx) = frames;% 帧的IQ矩阵是横着存储的 59 | obj.Label(LabelStartIdx,:) = label; 60 | 61 | obj.NumFrames = obj.NumFrames + numNewFrames; 62 | obj.Labelnumber = obj.Labelnumber+ 1; 63 | end 64 | 65 | %% 获取帧数据和标签 66 | function [frames,labels] = get(obj) 67 | 68 | I = real(obj.Frames(:,1:obj.NumFrames)); 69 | Q = imag(obj.Frames(:,1:obj.NumFrames)); 70 | %第一维是帧的数量,第二维是通道数默认为1,第三维是2,表示IQ两列,第四维是一帧的长度。三四维组成一帧的长和宽。 71 | I = permute(I,[2 3 1]); 72 | Q = permute(Q,[2 3 1]); 73 | tframes = cat(3,I,Q);%3表示在第3维度进行叠加矩阵 74 | s1 = size(tframes,1); 75 | s2 = size(tframes,3); 76 | %数据集中的每一帧变成一维,转换成长度为640的一行。 77 | frames = reshape(tframes,s1,s2); 78 | size(frames) 79 | 80 | labels = obj.Label(1:obj. MaximumNumFrames,:); 81 | end 82 | 83 | %% 划分数据集成训练集、验证集、测试集 84 | function [fsTraining,fsValidation,fsTest] = ... 85 | splitData(obj,splitPercentages) 86 | 87 | 88 | numFrames = obj.Labelnumber; 89 | 90 | 91 | fsTraining = helperModClassFrameStoreTest(... 92 | ceil(obj.MaximumNumFrames*splitPercentages(1)/100), ... 93 | obj.SamplesPerFrame, obj.Labels); 94 | fsValidation = helperModClassFrameStoreTest(... 95 | ceil(obj.MaximumNumFrames*splitPercentages(2)/100), ... 96 | obj.SamplesPerFrame, obj.Labels); 97 | fsTest = helperModClassFrameStoreTest(... 98 | ceil(obj.MaximumNumFrames*splitPercentages(3)/100), ... 99 | obj.SamplesPerFrame, obj.Labels); 100 | 101 | numTrainingFrames = round(numFrames*splitPercentages(1)/100); 102 | numValidationFrames = round(numFrames*splitPercentages(2)/100); 103 | numTestFrames = round(numFrames*splitPercentages(3)/100); 104 | extraFrames = sum([numTrainingFrames,numValidationFrames,numTestFrames]) - numFrames; 105 | if (extraFrames > 0) 106 | numTestFrames = numTestFrames - extraFrames; 107 | end 108 | 109 | 110 | shuffleIdx = randperm(numFrames); 111 | frames = obj.Frames(:,shuffleIdx); 112 | TempLabel = obj.Label(shuffleIdx,:); 113 | 114 | for TraininglabelIndex = 1:numTrainingFrames 115 | add(fsTraining, ... 116 | frames(:,TraininglabelIndex), ... 117 | TempLabel(TraininglabelIndex,:)); 118 | end 119 | for ValidationlabelIndex = 1: numValidationFrames 120 | add(fsValidation, ... 121 | frames(:,numTrainingFrames+ValidationlabelIndex), ... 122 | TempLabel((numTrainingFrames+ValidationlabelIndex),:)); 123 | 124 | end 125 | for TestlabelIndex = 1 :numTestFrames 126 | add(fsTest, ... 127 | frames(:,numTrainingFrames+numValidationFrames+TestlabelIndex), ... 128 | TempLabel((numTrainingFrames+numValidationFrames+TestlabelIndex),:)); 129 | end 130 | 131 | 132 | 133 | % Shuffle new frame stores 134 | shuffle(fsTraining); 135 | shuffle(fsValidation); 136 | shuffle(fsTest); 137 | 138 | end 139 | 140 | function shuffle(obj) 141 | %shuffle Shuffle stored frames 142 | % shuffle(FS) shuffles the order of stored frames. 143 | % 打乱顺序,类似与扑克中的洗牌操作 144 | shuffleIdx = randperm(obj.NumFrames); 145 | obj.Frames = obj.Frames(:,shuffleIdx); 146 | obj.Label = obj.Label(shuffleIdx,:); 147 | end 148 | end 149 | end 150 | 151 | 152 | 153 | 154 | --------------------------------------------------------------------------------