Please provide me the matlab code for 8-bit integer arithmetic coding and decoding.
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
DGM
2025-9-11
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
% 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
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
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
endencoded_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
endExample 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:
- [Arithmetic Encoding]( https://www.mathworks.com/help/matlab/ref/arithmeticencoder.html )
- [Floating-Point Numbers]( https://www.mathworks.com/help/matlab/matlab-mathematics/working-with-floating-point-numbers.html )
Please feel free to reach out if you have any further questions.
4 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!