Clear Filters
Clear Filters

Arithenco and arithdeco not working with zeros

1 view (last 30 days)
Why does arithenco/arithdeco not work with an input sequence including zeros and is there a way to work around this?
Example) Input message would be [2,1,2,1,2,0,0] and the decoded sequence is [3,2,3,2,3,1,2]. Whereas if I have an input message of [2,1,2,1,2,1,2], my decoded sequence is identical to the input as [2,1,2,1,2,1,2].
Below is my testing code. I believe you'll need the Signal Processing ToolBox for the arithenco/arithdeco functions.
clc;
clear all;
close all;
message = [2,1,2,1,2,0,1];
%% Encoder
table_size = height(message);
rows = table_size(1);
encodedY = [];
for row = 1:rows
coeff_row = message(row,:);
% Calculate unique values
source = unique(coeff_row);
for i=1:length(source)
counts(i)=length(strfind(coeff_row,source(i)));
end
seq=zeros(1,length(coeff_row));
for i=1:length(coeff_row)
seq(i)=strfind(source,coeff_row(i));
end
% Get arithmetic code for the block
code = arithenco(seq, counts);
% Store the block's code
encodedY = [encodedY; {code}];
end
%% Decoder
decodedY = [];
for row = 1 : size(encodedY)
temp = cell2mat(encodedY(row,:));
dseq = arithdeco(temp,counts,length(seq));
decodedY = [decodedY;dseq];
end
%%

Answers (1)

Abhimenyu
Abhimenyu on 4 Oct 2023
Hi Bernard,
I understand that you want to decode the messages containing zeros using “arithenco” and “arithdeco” functions of MATLAB. You can modify the code to map the “decodedY” values back to the “source” as you have used “unique” function to map the “message” to “seq. Here is the modified code:
clearvars;
close all;
message = [2,1,2,1,2,0,2];
%% Encoder
table_size = height(message);
rows = table_size(1);
encodedY = [];
for row = 1:rows
coeff_row = message(row,:);
% Calculate unique values
source = unique(coeff_row);
for i=1:length(source)
counts(i)=length(strfind(coeff_row,source(i)));
end
seq=zeros(1,length(coeff_row));
for i=1:length(coeff_row)
seq(i)=strfind(source,coeff_row(i));
end
% Get arithmetic code for the block
code = arithenco(seq, counts);
% Store the block's code
encodedY = [encodedY; {code}];
end
%% Decoder
decodedY = [];
for row = 1 : size(encodedY)
temp = cell2mat(encodedY(row,:));
dseq = arithdeco(temp,counts,length(seq));
decodedY = [decodedY;dseq];
end
%modified here
decoded_message = zeros(size(decodedY));
for i = 1:length(decodedY)
decoded_message(i) = source(decodedY(i));
end
disp("message")
message
disp(message)
2 1 2 1 2 0 2
disp("decoded message")
decoded message
disp(decoded_message)
2 1 2 1 2 0 2
%%
This can properly encode and decode any message containing zeros.
I hope this helps!
Thank you,
Abhimenyu.

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!