我写了一种用于奇偶校验交换和二进制搜索的算法。 X和Y是两个随机序列。第一步,X和Y将字符串划分为块,这意味着将1000位分成100 * 10个子集,每个子集具有100位。如果奇偶校验错配,X和Y将交换有关子集的奇偶校验信息,它们调用二进制搜索方法来定位错误并进行纠正。这一切都发生在第一次迭代中。在第二次迭代中(最多需要进行4次迭代),X和Y会随机对其1000位进行随机混洗,重复相同的奇偶校验交换和二进制搜索算法。每当X和Y对其1000位进行混洗时,算法都会显示校正后的附加位(changeindex)。我确实注意到了,但是这个错误的结果在以后的改组中开始出现。我尝试解决该问题,该问题不允许重复相同的索引或考虑错误的差异作为答案,但我不能。例:如果X和Y位置的差异大约为5 3 110 521 891 974,但是在校正后会显示两次,例如5 3 110 110 521 521 891 974 974。
clear all; clc; close all;
X = randi([0,1],1000,1);
Y = bsc(X,0.02);
X_Original = X; Y_Original = Y;
diff_ind = [];
for i = 1:length(X)
if X_Original(i) ~= Y_Original(i)
diff_ind = [diff_ind i];
end
end
changeindex = [];
Iteration = 1;
ind = [1:length(X)]';
while length(changeindex) < length(diff_ind)
block_dim = Iteration*36;
%% defining the length
for div = 1:floor(length(X)/block_dim)
tX = X(1+(div-1)*block_dim:div*block_dim);
tY = Y(1+(div-1)*block_dim:div*block_dim);
tind = ind(1+(div-1)*block_dim:div*block_dim);
len = length(tX);
backup_tX = [];
backup_tY = [];
backup_ind = [];
pass = 0;
while len > 1
pass = pass + 1;
% fprintf('Parity Number: %d\n',pass);
if mod(len,2) == 1
x = [tX(1:(len-1)/2) tX(1+(len-1)/2:len-1)];
y = [tY(1:(len-1)/2) tY(1+(len-1)/2:len-1)];
tind = [tind(1:(len-1)/2) tind(1+(len-1)/2:len-1)];
else
x = [tX(1:(len)/2) tX(1+(len)/2:len)];
y = [tY(1:(len)/2) tY(1+(len)/2:len)];
tind = [tind(1:(len)/2) tind(1+(len)/2:len)];
end
%% parity exchange
b1 = sum(x(:,1)) == sum(y(:,1));
b2 = sum(x(:,2)) == sum(y(:,2));
if (b1 == false) && (b2 == true)
tX = x(:,1);
tY = y(:,1);
tind = tind(:,1);
elseif (b2 == false) && (b1 == true)
tX = x(:,2);
tY = y(:,2);
tind = tind(:,2);
elseif (b1 == false) && (b2 == false)
tX = x(:,1);
tY = y(:,1);
if isempty(backup_tX)
backup_tX = x(:,2);
backup_tY = y(:,2);
backup_ind = tind(:,2);
else
backup_tX = [x(:,2); backup_tX];
backup_tY = [y(:,2); backup_tY];
backup_ind = [tind(:,2); backup_ind];
end
tind = tind(:,1);
else
break;
end
len = length(tX);
if len == 1
tY = tX;
Y(tind) = tX;
% fprintf('Change made at index: %d\n',tind);
changeindex = [changeindex tind];
if ~isempty(backup_tX)
tX = backup_tX;
tY = backup_tY;
tind = backup_ind;
len = length(tX);
backup_tX = [];
backup_tY = [];
backup_ind = [];
end
end
end
end
if block_dim > length(X)
break;
else
%% Shuffle
chng = randperm(length(X));
X = X(chng);
Y = Y(chng);
ind = ind(chng);
Iteration = Iteration + 1;
end
end
disp('------------------------');
disp('Original difference index:');
disp(sort(diff_ind));
disp('Changes made at index:');
disp(sort(changeindex));