- your list of probabilities does not sum to unity
- your list of symbols is not the same length as the corresponding list of probabilites
Help on how to achieve lossless image compression using arithmetic encoding and decoding.
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Had this error after passing the coefficients of wavelet image tranform, symbols and probability to the package
Arithmetic Encoding & Decoding
Error:
[Tag_bits]= Arithmetic_enc(sym,p,c);
Plese enter proper values!!!
Elapsed time is 0.000439 seconds.
Output argument "Tag_bits" (and possibly others) not assigned a value in the execution with "Arithmetic_enc" function.
code:
function Tag_bits =Arithmetic_enc(sym,p,seq)
tic; % TIC-TOC commands are used to measure the simulation time of the program.
format long g;
%% ARGUMENTS OF THE FUNCTION
% SYM is a string of symbols of information from the source.
% P represents the array of Probabilities of the corresponding symbols in
% the SYM string.
% SEQ is the string of sequence of symbols to be encoded by arithmetic
% coding.
if(length(sym)==length(p) && sum(p)==1)
%% ALGORITHM IMPLEMENTATION
% Calculate the Symbol Intervals.
Fx=zeros(1,length(sym));
for i=1:length(sym)
if i==1
Fx(i)=p(i);
else
Fx(i)=Fx(i-1)+p(i);
end
end
% Encoding the Sequence of Symbols.
L=0;U=1; % Initial Lower and Upper Intervals.
Tag_bits=zeros(1,0); % Initializing the Tag Bits.
for i=1:length(seq)
j=find(seq(i)==sym); % Finds the Index of the sequence symbol in the symbol string.
if(j==1)
L_new=L;
else
L_new=L+(U-L)*Fx(j-1);
end
U_new=L+(U-L)*Fx(j);
L=L_new;
U=U_new;
while((L<0.5 && U<0.5) ||(L>=0.5 && U>0.5))
if(L<0.5 && U<0.5)
Tag_bits=[Tag_bits,'0'];
L=2*L;
U=2*U;
else
Tag_bits=[Tag_bits,'1'];
L=2*(L-0.5);
U=2*(U-0.5);
end
end
end
tag=(L+U)/2;
% Embedding the Final Tag Value.
bits=zeros(1,0);
if(2*tag>1)
tag=2*tag-1;
bits=[bits,'1'];
else
tag=2*tag;
bits=[bits,'0'];
end
while(bin2dec(bits)/2^length(bits)<L)
if(2*tag>1)
tag=2*tag-1;
bits=[bits,'1'];
else
tag=2*tag;
bits=[bits,'0'];
end
end
Tag_bits=[Tag_bits,bits];
% Padding of zeros is done to keep the TAG BITS size multiple of 16 bits.
Tag_bits=[Tag_bits,dec2bin(0,16-rem(length(Tag_bits),16))];
display('Tag Value is:');
disp(bin2dec(Tag_bits)/2^length(Tag_bits));
display('Tag Word is:');
Tag_bits=[Tag_bits,dec2bin(length(seq),16)];
else
display('Plese enter proper values!!!');
end
toc;
workspace values for
seq = c
c= 1x349184 double
p = 1x349118 double
sym = 1x349118 double
sum(p)
ans = 0.999999999999999
Help on how to achieve lossless image compression with any other package or this package for arithmetic encoding and decoding.
2 comentarios
DGM
el 18 de Sept. de 2023
Editada: DGM
el 18 de Sept. de 2023
Since we don't know how you tried to call the function, we don't know whether any other suggestion would work either. All we know is that one of the following is true:
If it's the first case, there might be room for float rounding errors to cause problems there.
Documentation on this FEX submission could be better.
Respuestas (1)
Rahul
el 12 de Ag. de 2025
Hi enyawson,
I understand that you are attempting to perform lossless image compression using the 'Arithmetic_enc' function from the referenced File Exchange package, but an error occurs because the function’s initial validation check is not being satisfied.
In the provided implementation, execution proceeds only if both of the following conditions hold:
- 'length(sym)' is equal to 'length(p)'.
- 'sum(p)' is exactly equal to 1.
From your provided workspace data, 'length(sym)' and 'length(p)' differ (349118 vs. 349184 in the case of 'seq') and 'sum(p)' is reported as 0.999999999999999 rather than exactly 1. The second case is a typical floating-point precision issue that can occur when probabilities are computed from large datasets.
In MATLAB, such numerical discrepancies can be handled by normalizing or tolerating small deviations. For example:
% Normalize probabilities to sum exactly to 1
'p' = 'p' / sum('p');
% OR: Allow tolerance in the condition
if length('sym') == length('p') && abs(sum('p') - 1) < 1e-12
% Proceed with encoding
else
disp('Please enter proper values!');
end
It is also important that 'sym' contains all unique symbols from your sequence and 'p' contains their associated probabilities in corresponding order. If the data originates from a wavelet transform of an image, mapping the transform coefficients into discrete symbols is a prerequisite for using this arithmetic encoding function.
If the goal is to achieve lossless image compression without modifying the original File Exchange code, MATLAB provides other options such as:
- Communications Toolbox: functions like 'arithenco' and 'arithdeco' for arithmetic coding.
- Image Processing Toolbox: lossless formats ('imwrite' with 'png' or 'tiff' in compression mode).
- Wavelet Toolbox: functions for quantization and entropy coding after wavelet decomposition.
You can refer to the following documentation links and examples regarding various optimization techniques:
- Arithmetic Encoding and Decoding in MATLAB: https://www.mathworks.com/help/comm/ref/arithenco.html
- Lossless Image Compression Workflow: https://www.mathworks.com/matlabcentral/answers/121768-matlab-source-code-for-image-compression-algorithm
This approach should ensure that the probabilities are consistent, symbols align correctly, and the encoding process can proceed without the error message.
0 comentarios
Ver también
Categorías
Más información sobre Denoising and Compression en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!