Public
Edited
Aug 30, 2024
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function arithmeticEncode(input_data, probabilities, cumulativeProbs) { // Yes - lost of debugging code.
let low = 0;
let high = 1;

for (let symbol of input_data) {
const rangeSize = high - low;

// Debugging logs
console.log(`symbol: ${symbol}, low: ${low}, high: ${high}, rangeSize: ${rangeSize}`);

// Access cumulative probability before the current symbol and the probability of the symbol
const cumulativeProbBefore = cumulativeProbs[symbol];
const symbolProb = probabilities[symbol];

console.log(`cumulativeProbs[symbol]: ${cumulativeProbBefore} - probabilities[symbol]: ${symbolProb}`)
high = low + rangeSize * (cumulativeProbBefore + symbolProb);
low = low + rangeSize * cumulativeProbBefore;


console.log(`new high = ${high} new low =${low}`)
console.log('---------------------------------------------------')
}

const encodedValue = (low + high) / 2;
return encodedValue;
}

Insert cell
input_data = [16, 32, 16, 48, 16];
Insert cell
// Calculate the total number of symbols
inputSize = input_data.length;
Insert cell
encodedValue = arithmeticEncode(input_data, probabilities, cumulativeProbs)
Insert cell
function calculateProbabilities(input_data) {
// Step 1: Calculate the frequency of each symbol
let frequency = {};
for (let symbol of input_data) {
if (frequency[symbol]) {
frequency[symbol]++;
} else {
frequency[symbol] = 1;
}
}

// Step 2: Calculate the probability of each symbol
let probabilities = {};
for (let symbol in frequency) {
probabilities[symbol] = frequency[symbol] / inputSize;
}

return probabilities;
}
Insert cell
probabilities = calculateProbabilities(input_data);
Insert cell
function calculateCumulativeProbs(probabilities) {
// Step 1: Extract and sort the symbols
let symbols = Object.keys(probabilities).map(Number).sort((a, b) => a - b);
// Step 2: Calculate cumulative probabilities
let cumulativeProbs = {};
let cumulativeSum = 0;

for (let symbol of symbols) {
cumulativeProbs[symbol] = cumulativeSum;
cumulativeSum += probabilities[symbol];
}

return cumulativeProbs;
}
Insert cell
cumulativeProbs = calculateCumulativeProbs(probabilities)
Insert cell
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;

// Debugging logs
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); // Convert the key back to a number if needed
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;
}
Insert cell
orgionalData = arithmeticDecode(encodedValue, probabilities, cumulativeProbs, inputSize)
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more