Skip to content

Commit

Permalink
vitbidecoder modify
Browse files Browse the repository at this point in the history
vitbidecoder has been changed. Now, it can decode M/N rates
convolutional code. But the computing runtime increased because of the
deep loop.
  • Loading branch information
cea-wind committed Oct 14, 2015
1 parent 8b78184 commit deac813
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 87 deletions.
2 changes: 1 addition & 1 deletion CC_Sim.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
SNR_dB = EbN0_dB((nEbN0)) + 10*log10(2)+10*log10(RATE);
SNR = 10^(SNR_dB/10);
noise = randn(1,length(transmitSignal));
noise = noise/sqrt(SNR);
noise = noise/sqrt(SNR);
receiveSignal = transmitSignal + noise;

%%
Expand Down
13 changes: 13 additions & 0 deletions test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
G = [1 1 1;1 0 1];
[N,L] = size(G);
M = L-1;Ns = 2^M;
for state_i = 1:Ns
state_b = dec2bin(state_i-1,M)-48;
for input_bit = 0:1
d_k = input_bit;
a_k = rem(G(1,:)*[d_k state_b]',2);
out(input_bit+1,:) = [d_k rem(G(2,:)*[a_k state_b]',2)];
state(input_bit+1,:) = [ak state_b(1:M-1)];
end
nout(state_i,:) = 2*[out(1,:) out(2,:)]-1;
end
119 changes: 33 additions & 86 deletions vitbiDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,43 @@
% Traceback depth largest.
K = log2(trel.numInputSymbols);
N = log2(trel.numOutputSymbols);
code = code(:).';
codeReshape = reshape(code,N,[])';
savedStates= zeros(trel.numStates,size(codeReshape,1)+1);
statesFlag = zeros(trel.numStates,size(codeReshape,1)+1);
decoded = zeros(trel.numStates,size(codeReshape,1));
metric = zeros(1,trel.numStates);
metric = zeros(trel.numStates,trel.numStates + 1);
% initial state is 0, some states can't reach if m < log2(trel.numStates)/K
for m = 1:min(log2(trel.numStates)/K,size(codeReshape,1))

selected = reshape(trel.nextStates(1+savedStates(:,m),:)',1,[]);
outputs = reshape(trel.outputs(1+savedStates(:,m),:)',1,[]);
inputs = repmat(0:2^K-1,1,trel.numStates)';
outputs = dec2bin(outputs) - 48;
metric_add = repmat(codeReshape(m,:),size(outputs,1),1).*(1-2*outputs);
metric_add = sum(metric_add,2);
selectedMetric = reshape(repmat(metric,2^K,1),1,[]) + metric_add';

savedStates(:,1:m) = savedStates(floor((0:trel.numStates-1)/2^K)+1,1:m);
decoded(:,1:m-1) = decoded(floor((0:trel.numStates-1)/2^K)+1,1:m-1);
savedStates(:,m+1) = selected(1:trel.numStates);
decoded(:,m) = inputs(1:trel.numStates);
metric = selectedMetric(1:trel.numStates);

statesFlag(1,1) = 1;
for m = 1:size(codeReshape,1)
for n = 1:trel.numStates
if(statesFlag(n,m)>0)
for c = 1:trel.numInputSymbols
selected = trel.nextStates(n,c);
outputs = trel.outputs(n,c);
outputs = dec2bin(outputs,N) - 48;
metric_add = codeReshape(m,:)*(1-2*outputs)';
if(statesFlag(selected+1,m+1)==0)
metric(selected+1,m+1) = metric(n,m)+metric_add;
decoded(selected+1,m) = c-1;
statesFlag(selected+1,m+1) = n;
else
if(metric(n,m)+metric_add>metric(selected+1,m+1))
metric(selected+1,m+1) = metric(n,m)+metric_add;
decoded(selected+1,m) = c-1;
statesFlag(selected+1,m+1) = n;
end
end
end
end
end
end

if(ceil(log2(trel.numStates)/K)==log2(trel.numStates)/K)
[~,initSort] = sort(savedStates(:,m+1));
savedStates = savedStates(initSort,:);
decoded = decoded(initSort,:);
metric = metric(initSort);
else
savedStatesTemp = savedStates;
metricFlag = zeros(size(metric));
metricTemp = metricFlag;
decodedTemp = decoded;
for n = 1:K^(N*floor((log2(trel.numStates)/K)))
nextState = trel.nextStates(1+savedStates(n,floor(log2(trel.numStates)/K)),:);
outputs = trel.outputs(1+savedStates(n,floor(log2(trel.numStates)/K)),:);
outputs = dec2bin(outputs) - 48;
metric_add = repmat(codeReshape(floor(log2(trel.numStates)/K)...
,:),size(outputs,1),1).*(1-2*outputs);
for k = 1:length(nextState)
if(metricFlag(nextState(k)+1)==0)
savedStatesTemp(nextState(k)+1,:) = savedStates(n,:);
metricTemp(nextState(k)+1) = metric(n) + metric_add(k);
decodedTemp(nextState(k)+1,:) = decoded(n,:);
decodedTemp(nextState(k)+1,floor(log2(trel.numStates)/K)+1) = k;
metricFlag(nextState(k)+1) = 1;
elseif(metric(n) + metric_add(k)> metricTemp(nextState(k)+1))
savedStatesTemp(nextState(k)+1,:) = savedStates(n,:);
metricTemp(nextState(k)+1) = metric(n) + metric_add(k);
decodedTemp(nextState(k)+1,:) = decoded(n,:);
decodedTemp(nextState(k)+1,floor(log2(trel.numStates)/K)+1) = k;
end
end
end
savedStates = savedStatesTemp;
metric = metricTemp;
decoded = decodedTemp;
savedStates(:,floor(log2(trel.numStates)/K)+1) = 0:trel.numStates-1;
[~,m] = max(metric(:,end));
decoded_seq = [];
for n=size(codeReshape,1):-1:1
decoded_seq = [dec2bin(decoded(m,n),K)-48 decoded_seq];
m = statesFlag(m,n+1);
end

% next states , output and metric for every possible inputs
% the order of current states is 0,1,2,...
selected = reshape(trel.nextStates(1:trel.numStates,:)',1,[]);
currentStates = reshape(repmat(0:trel.numStates-1,2^K,1),1,[]);
[~,stateSort] = sort(selected);
currentStates = currentStates(stateSort);
outputs = reshape(trel.outputs(1:trel.numStates,:)',1,[]);
inputs = repmat(0:2^K-1,1,trel.numStates)';
inputs = inputs(stateSort);
outputs = dec2bin(outputs) - 48;

% survival path, include every states.
for m = ceil(log2(trel.numStates)/K)+1: size(codeReshape,1)
% In AWGN channel,LLR = C1(r*v) + C2.
metric_add = repmat(codeReshape(m,:),size(outputs,1),1).*(1-2*outputs);
metric_add = sum(metric_add,2);
selectedMetric = reshape(repmat(metric,2^K,1),1,[]) + metric_add';
% order selectedMetric by state, and choose the maximum metric
selectedMetricsort = selectedMetric(stateSort);
selectedMetricsort = reshape(selectedMetricsort,2^K,[]);
[metric_update,survior] = max(selectedMetricsort);
survior = survior + (0:trel.numStates-1)*2^K;

savedStates(:,1:m) = savedStates(currentStates(survior)+1,1:m);
decoded(:,1:m-1) = decoded(currentStates(survior)+1,1:m-1);
savedStates(:,m+1) = 0:trel.numStates-1;
decoded(:,m) = inputs(survior);
metric = metric_update;
end
[~,maxMetricIndex] = max(metric);
decoded = [zeros(1,tblen) decoded(maxMetricIndex,1:end-tblen)];
end

decoded = [zeros(1,tblen) decoded_seq(1:end-tblen)];
end

0 comments on commit deac813

Please sign in to comment.