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

5 vues (au cours des 30 derniers jours)
Harpreet
Harpreet le 11 Sep 2025
Commenté : Umar le 12 Sep 2025
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 commentaires
DGM
DGM le 11 Sep 2025
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.

Connectez-vous pour commenter.

Réponses (1)

Umar
Umar le 11 Sep 2025

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 commentaires
Harpreet
Harpreet le 12 Sep 2025
Thank you Mr. Umar for your valuable feedback.
Umar
Umar le 12 Sep 2025

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

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by