Please provide me the matlab code for 8-bit integer arithmetic coding and decoding.

I am currently using 8-bit integer arithmetic encoding to compress a binary vector of approximately 7000 bits in MATLAB. However, after decoding the arithmetic encoding, the reconstructed binary vector does not match the original input. Could you please provide MATLAB code for 8-bit integer arithmetic encoding and decoding that ensures full recovery of the original data?

댓글 수: 2

See:
If you want the output stream to be represented bytewise instead of binary, then there are a number of ways to do that conversion. Within the same toolbox, bi2de(), de2bi(), bit2int(), int2bit().
% input parameters
alphabet = [1 2];
prob = [0.99 0.01];
len = 10E3;
% generate input sequence
seq = randsrc(1,len,[alphabet; prob]);
% encode
% output is a binary stream represented as numeric 0,1
B = arithenco(seq,round(prob*100));
nb = numel(B) % hopefully shorter than seq
nb = 889
% arrange into bytes, convert to dec (uint8)
Bp = zeros(8,ceil(nb/8));
Bp(1:nb) = B;
u8 = uint8(bi2de(Bp.'));
% convert back to a binary vector
Br = double(de2bi(u8,8));
Br = reshape(Br.',[],1).';
% decode
seqr = arithdeco(Br,round(prob*100),length(seq));
isequal(seq,seqr) % should be identical
ans = logical
1
I've never used these tools for anything before, and I may be misinterpreting the problem, but that's what I threw together.

댓글을 달려면 로그인하십시오.

답변 (1개)

Umar
Umar 2025년 9월 11일

Hi @Harpreet,

I understand you're encountering issues with reconstructing a binary vector after arithmetic encoding and decoding. Below is a brief implementation for 8-bit integer arithmetic encoding and decoding, which should ensure that the original data is fully recovered.

Pseudo-Code for Arithmetic Encoding and Decoding:

Arithmetic Encoding

function [encoded_value] = arithmetic_encode(binary_vector, prob_0, prob_1)
  low = 0;
  high = 1;
    for i = 1:length(binary_vector)
        range = high - low;
        if binary_vector(i) == 0
            high = low + range * prob_0;
        else
            low = low + range * prob_0;
        end
    end
    encoded_value = (low + high) / 2;
  end

Arithmetic Decoding

function [decoded_vector] = arithmetic_decode(encoded_value, prob_0, 
prob_1, length_of_vector)
  low = 0;
  high = 1;
  decoded_vector = zeros(1, length_of_vector);
    for i = 1:length_of_vector
        range = high - low;
        if encoded_value < low + range * prob_0
            decoded_vector(i) = 0;
            high = low + range * prob_0;
        else
            decoded_vector(i) = 1;
            low = low + range * prob_0;
        end
        high = low + range * prob_1;
        low = low + range * prob_0;
    end
  end

Example Usage:

binary_vector = [1, 0, 1, 1, 0];  % Example vector
prob_0 = 0.4;  % Probability of 0
prob_1 = 0.6;  % Probability of 1
% Encode
encoded_value = arithmetic_encode(binary_vector, prob_0, prob_1);
% Decode
decoded_vector = arithmetic_decode(encoded_value, prob_0, prob_1,   
length(binary_vector));
% Check
disp('Original Vector:');
disp(binary_vector);
disp('Decoded Vector:');
disp(decoded_vector);

Notes:

  • This code assumes fixed symbol probabilities (`prob_0` and `prob_1`), typically calculated from the symbol frequency.
  • Precision is crucial when working with large data; ensure floating-point operations are handled carefully.

For further details, I recommend reviewing the MathWorks documentation:

Please feel free to reach out if you have any further questions.

댓글 수: 4

Harpreet
Harpreet 2025년 9월 11일
이동: DGM 2025년 9월 11일
Thank you for the answere. But this is standard arithmetic coding using floating-point precision not 8-bit integer arithmetic encoding that can be used to avoid floating-point errors.

Hi @Harpreet,

Thank you for your valuable comment! You are absolutely correct that the original approach I shared uses floating-point arithmetic for encoding and decoding, which can lead to precision errors, particularly when handling long sequences or when high accuracy is needed. Floating-point operations can accumulate rounding errors over multiple iterations, which may affect the reliability of the decoded data.

To address this issue, I’ve made a modification to the original implementation that uses 8-bit integer arithmetic for both encoding and decoding. This change ensures that all operations are done using integer math, specifically within the 8-bit range (0-255), which prevents any floating-point errors.

What’s Changed?

Integer Arithmetic: Instead of relying on floating-point numbers, the encoding and decoding calculations now use integer-based math, which avoids rounding errors. 8-bit Range: The `low` and `high` values are constrained to the 8-bit range (0-255), providing a clear and predictable range for encoding and decoding operations. No Floating-Point Operations: By removing floating-point division and multiplication, this approach guarantees that the encoding process will be precise, especially for longer sequences.

Key Benefits of the 8-bit Integer Approach:

Avoid Floating-Point Precision Errors: Integer arithmetic ensures that encoding and decoding are exact, with no risk of accumulating errors due to floating-point limitations. Memory Efficiency: Working with fixed-size 8-bit integers reduces memory usage, making this method more suitable for systems with limited precision or memory resources. Exact Recovery: Since there are no floating-point operations, the original data is fully recovered without any loss of accuracy.

I’ve provided the updated version of the algorithm below, which demonstrates how integer-based arithmetic works in practice:

Updated Integer-Based Encoding:

function [encoded_value] = integer_arithmetic_encode(binary_vector, prob_0, 
 prob_1)
  low = 0;
  high = 255;  % 8-bit range
  for i = 1:length(binary_vector)
      range = high - low;
      if binary_vector(i) == 0
          high = low + floor(range * prob_0);
      else
          low = low + floor(range * prob_0);
      end
  end
  encoded_value = floor((low + high) / 2);  % Middle of the final range
end
 *Updated Integer-Based Decoding:*
function [decoded_vector] = integer_arithmetic_decode(encoded_value, 
prob_0, prob_1, length_of_vector)
  low = 0;
  high = 255;  % 8-bit range
  decoded_vector = zeros(1, length_of_vector);
  for i = 1:length_of_vector
      range = high - low;
      if encoded_value < low + floor(range * prob_0)
          decoded_vector(i) = 0;
          high = low + floor(range * prob_0);
      else
          decoded_vector(i) = 1;
          low = low + floor(range * prob_0);
      end
      high = low + floor(range * prob_1);
      low = low + floor(range * prob_0);
  end
end

How to Use:

binary_vector = [1, 0, 1, 1, 0];  % Example binary vector
prob_0 = 0.4;  % Probability of 0
prob_1 = 0.6;  % Probability of 1
% Encode the binary vector
encoded_value = integer_arithmetic_encode(binary_vector, prob_0, prob_1);
% Decode the encoded value
decoded_vector = integer_arithmetic_decode(encoded_value, prob_0, prob_1, 
length(binary_vector));
% Display the original and decoded vectors
disp('Original Vector:');
disp(binary_vector);
disp('Decoded Vector:');
disp(decoded_vector);

Why This Solution Works:

Integer Arithmetic: This avoids the accumulation of rounding errors that are common with floating-point arithmetic.

High Precision: The integer-based method ensures the encoding and decoding are exact, especially important for applications requiring high precision.

Memory Efficient: By using fixed-size 8-bit integers, we can efficiently handle memory in environments where floating-point operations might be costly.

I hope this solution resolves the precision concerns you raised. Please feel free to reach out if you have any further questions or need more clarification!

Thank you Mr. Umar for your valuable feedback.

You're welcome! I'm glad I could help

댓글을 달려면 로그인하십시오.

카테고리

질문:

2025년 9월 11일

댓글:

2025년 9월 12일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by