function arithmeticDecode(encodedValue, probabilities, cumulativeProbs, inputSize) {
let low = 0;
let high = 1;
let decoded = [];
for (let i = 0; i < inputSize; i++) {
const rangeSize = high - low;
const scaledValue = (encodedValue - low) / rangeSize;
console.log(`i: ${i}, low: ${low}, high: ${high}, range: ${rangeSize}, scaledValue: ${scaledValue}`);
let symbol;
for (let s in cumulativeProbs) {
const cumulativeProb = cumulativeProbs[s];
const prob = probabilities[s];
const cumulativeProbAfter = cumulativeProb + prob;
if (scaledValue >= cumulativeProb && scaledValue < cumulativeProbAfter) {
symbol = parseInt(s);
high = low + rangeSize * cumulativeProbAfter;
low = low + rangeSize * cumulativeProb;
decoded.push(symbol);
break;
}
}
if (symbol === undefined) {
throw new Error("Decoding error: Unable to find matching symbol.");
}
}
return decoded;
}