--[[ ================================================================ BidirectionalHoldMerge (x8 pairs) (with timestamp propagation) ---------------------------------------------------------------- Total Inputs = 16 (paired) Total Outputs = 8 Pair mapping out1 = input1 , input2 out2 = input3 , input4 ... out8 = input15, input16 ---------------------------------------------------------------- Output length = max(#vecA, #vecB) Value = sampleA + sampleB (binary → double point encoding) 0 = 00 1 = 01 2 = 10 3 = 11 Timestamp = (held side ignored) one side held -> non-held side's timestamp both fresh -> max(tsA, tsB) Quality = bitwise OR (qA | qB) ---------------------------------------------------------------- Length behaviour (per pair) #vecA == #vecB direct 1:1, both always fresh -> max ts #vecA > #vecB vecB front-held, take vecA ts there #vecA < #vecB vecA front-held, take vecB ts there ================================================================ ]] function Binary16ToDoublePoint8(input) if input == nil then return {} end local out = {} -- right-aligned sampler -- returns: value, held flag, timestamp, quality local function sample(vec, L, n, i) if L == 0 then return 0, true, 0, 0 end local idx = i - (n - L) local held = (idx < 1) if held then idx = 1 end local e = vec[idx] if e == nil then return 0, true, 0, 0 end return e.value or 0, held, e.timestamp or 0, e.quality or 0 end ---------------------------------------------------------------- -- process 8 output pairs ---------------------------------------------------------------- for k = 1, 8 do -- input indices (1-based, string keys) local idxA = tostring((k - 1) * 2 + 1) local idxB = tostring((k - 1) * 2 + 2) local vecA = input[idxA] or {} local vecB = input[idxB] or {} local nA, nB = #vecA, #vecB local n = math.max(nA, nB) out[tostring(k)] = {} for i = 1, n do local vA, hA, tA, qA = sample(vecA, nA, n, i) local vB, hB, tB, qB = sample(vecB, nB, n, i) -------------------------------------------------------- -- timestamp selection -------------------------------------------------------- local ts if hA and not hB then ts = tB -- vecA held elseif hB and not hA then ts = tA -- vecB held else ts = (tA > tB) and tA or tB -- both fresh end -------------------------------------------------------- -- output construction -------------------------------------------------------- out[tostring(k)][i] = { value = (vA ~= 0 and 1 or 0) + ((vB ~= 0 and 1 or 0) * 2), quality = (qA or 0) | (qB or 0), timestamp = ts, } end end return out end