Build out MATLAB test framework and core integration coverage

This commit is contained in:
Silas Oettinghaus
2026-03-24 17:23:02 +01:00
parent 9446dfb888
commit a592ebefda
154 changed files with 2281 additions and 1305 deletions

View File

@@ -1,74 +1,69 @@
classdef PAMmapper_test < matlab.unittest.TestCase
classdef PAMmapper_test < IMDDTestCase
methods (Test, TestTags = {'unit', 'fast', 'transmit', 'pammapper'})
properties
Tx_bits
PAMmapper
end
function roundTripBitMappingForSupportedPamOrders(testCase)
modulationOrders = [2, 4, 8];
properties (MethodSetupParameter)
% Define method-level parameters for PRBS and bit pattern
useprbs = struct('false', 0, 'true', 1); % variations: {0, 1}
M = struct('M2', 2, 'M4', 4, 'M6', 6, 'M8', 8); % variations: {2, 4, 6, 8}
O = struct('O10', 10, 'O15', 15, 'O17', 17); % variations: {10, 15, 17}
end
for M = modulationOrders
txBits = Informationsignal(allBitPatternsForOrder(M));
properties (TestParameter)
% Define test-level parameters for M and O
mapper = PAMmapper(M, 0);
mapped = mapper.map(txBits);
demapped = mapper.demap(mapped);
end
methods (TestMethodSetup)
function setupModulation(testCase, useprbs, M, O)
% Setup PRBS and bit pattern for the test case using parameters
% Setup PRBS parameters
N = 2^(O-1); % Length of PRBS
randkey = 1; % Random key for random stream
[~, seed] = prbs(O, 1); % Initialize first seed of PRBS
bitpattern = [];
if useprbs
for i = 1:log2(M)
[bitpattern(:, i), seed] = prbs(O, N, seed);
end
else
s = RandStream('twister', 'Seed', randkey);
for i = 1:log2(M)
bitpattern(:, i) = randi(s, [0 1], N, 1);
end
testCase.verifyEqual(double(demapped.signal), double(txBits.signal), ...
sprintf("Roundtrip mapping failed for PAM-%d.", M));
end
if M == 6
bitpattern = reshape(bitpattern, [], 1);
bitpattern = bitpattern(1:end-mod(length(bitpattern), 5));
end
testCase.Tx_bits = Informationsignal(bitpattern);
testCase.PAMmapper = PAMmapper(M, 0);
end
end
function roundTripPam6ClassicMapping(testCase)
txBits = Informationsignal(pam6BitStream());
methods (Test, ParameterCombination = 'sequential')
% Test with sequential combination of parameters
function testBackToBackMapping(testCase, M, useprbs, O)
% Test Bits -> Symbols -> Bits (Back-to-Back Mapping)
mapper = PAMmapper(6, 0);
mapped = mapper.map(txBits);
demapped = mapper.demap(mapped);
% Map bits to symbols
Symbols = testCase.PAMmapper.map(testCase.Tx_bits);
testCase.verifyEqual(demapped.signal, txBits.signal, ...
"PAM-6 classic mapping should be lossless for aligned inputs.");
end
% Demap symbols back to bits
Rx_bits = testCase.PAMmapper.demap(Symbols);
function roundTripPam6EthMapping(testCase)
txBits = Informationsignal(pam6BitStream());
% Validate BER is zero
[~, error_num, ber, ~] = calc_ber(testCase.Tx_bits.signal, Rx_bits.signal, ...
"skip_front", 0, "skip_end", 0, "returnErrorLocation", 1);
mapper = PAMmapper(6, 0, "eth_style", 1);
mapped = mapper.map(txBits);
demapped = mapper.demap(mapped);
% Assert that BER is zero
testCase.verifyEqual(ber, 0, 'BER should be zero');
testCase.verifyEqual(error_num, 0, 'No errors should occur in the mapping process');
expectedBits = txBits.signal(1:length(demapped.signal));
testCase.verifyEqual(demapped.signal, expectedBits, ...
"PAM-6 ETH mapping should be lossless for aligned inputs.");
end
function quantizeMapsToNearestConstellationLevel(testCase)
mapper = PAMmapper(4, 0);
signalIn = Electricalsignal([-4.0; -0.2; 0.4; 2.9], "fs", 1);
quantized = mapper.quantize(signalIn);
expected = [-3; -1; 1; 3] ./ sqrt(5);
testCase.verifyEqual(quantized.signal, expected, "AbsTol", 1e-12);
end
end
end
function bits = allBitPatternsForOrder(M)
bitWidth = log2(M);
symbols = (0:M-1).';
bits = zeros(M, bitWidth);
for bitIdx = 1:bitWidth
bits(:, bitIdx) = bitget(symbols, bitWidth - bitIdx + 1);
end
end
function bits = pam6BitStream()
combinations = dec2bin(0:31, 5) - '0';
bits = reshape(combinations.', [], 1);
end