% Filename: viterbi_dec_input_gen.m
% Author: MSS/AZ
% Rev 2
% Last modified: 12/8/12
% Description: generate a convolutionally encoded stream, including
% additive white gaussian noise and 4-bit soft-quantization.
%
% Rev 2 12/8/12 AZ
% corrected incomplete puncturing patterns
% message length is related to Rate
%
clc 
clear all 
%---------------------------------------
% Simulation parameters
%---------------------------------------
len = 100002;     % simulation length
Eb_N0 = 4.0;       % Eb/No in dB (converter later to SNR)

%---------------------------------------
% Select code rate
%---------------------------------------
% % K = 5 
% Rate = 1/7; constlen = 5; codegen = [35 27 25 27 33 35 37]; puncpat = [1 1 1 1 1 1 1];  
%  
% % K = 7 (CCSDS. Verified) 
% Rate = 1/2; constlen = 7; codegen = [171 133]; puncpat = [1 1];  
% Rate = 2/3; constlen = 7; codegen = [171 133]; puncpat = [1 1 0 1]; 
% Rate = 3/4; constlen = 7; codegen = [171 133]; puncpat = [1 1 0 1 1 0]; 
% Rate = 5/6; constlen = 7; codegen = [171 133]; puncpat = [1 1 0 1 1 0 0 1 1 0]; 
% Rate = 7/8; constlen = 7; codegen = [171 133]; puncpat = [1 1 0 1 0 1 0 1 1 0 0 1 1 0];
%  
% % K = 7 (Intelsat, verified) Puncturing pattern is same as CCSDS but G0/G1 flipped
 Rate = 1/2; constlen = 7; codegen = [133 171]; puncpat = [1 1];   
% Rate = 2/3; constlen = 7; codegen = [133 171]; puncpat = [1 1 1 0];
% Rate = 3/4; constlen = 7; codegen = [133 171]; puncpat = [1 1 1 0 0 1];
% Rate = 5/6; constlen = 7; codegen = [133 171]; puncpat = [1 1 1 0 0 1 1 0 0 1]; 
% Rate = 7/8; constlen = 7; codegen = [133 171]; puncpat = [1 1 1 0 1 0 1 0 0 1 1 0 0 1]; 
%  
% % K = 9 (verified)
%Rate = 1/3; constlen = 9; codegen = [557 663 711]; puncpat = [1 1 1]; 
% Rate = 1/2; constlen = 9; codegen = [753 561]; puncpat = [1 1]; 
% Rate = 2/3; constlen = 9; codegen = [753 561]; puncpat = [1 0 1 1]; 
%  
%  
% Rate = 2/3; constlen = [3 3]; codegen = [6 5 1;7 2 5]; puncpat = [1 1 1]; 
% Rate = 2/3; constlen = [5 4]; codegen = [23 35 0; 0 5 13]; puncpat = [1 1 1]; 
% Rate = 1/2; constlen = 3; codegen = [7 5]; puncpat = [1 1]; 
% Rate = 2/3; constlen = 3; codegen = [7 5]; puncpat = [1 0]; 
%
%---------------------------------------
% data source
%---------------------------------------
% Random data, array len x 1, randomly distributed integer between 0 and 1
% The input message length divided by the base code rate must be an
% integer multiple of the length of the puncture pattern parameter.
if(Rate == 2/3)
    len = len - mod(len,2);
elseif(Rate == 3/4)
    len = len - mod(len,3);
elseif(Rate == 5/6)
    len = len - mod(len,5);
elseif(Rate == 7/8)
    len = len - mod(len,7);
end;
msg = randint(len,1,2,94384); 

%---------------------------------------
% convolutional encoding
%---------------------------------------
trellis = poly2trellis(constlen,codegen); % Define trellis: constraint length, code generator polynomials
code = convenc(msg,trellis,puncpat); % Length is 2*len.
tcode = 2*code-1; % Transmit -1s and 1s.
% 
% % [trellis_transition,trellis_output] = trellisgen(constlen,codegen); 
% u = convenc(data_in,trellis,puncpat); % MATLAB 
% % u1 = conv_punc(data_in, constlen, codegen, puncpat); % (Rate = 1/n + punc) 
% u2 = punc_conv(data_in, constlen, codegen, puncpat); % (Rate = k/n+ punc) 

%---------------------------------------
% channel = AWGN + 4-bit soft-quantization at the demodulator
%---------------------------------------
% convert Eb_N0 to SNR. Account for half noise on quadrature axis (3 dB)
% and uncoded to coded rate expansion.
snr = Eb_N0 + 3.0 + 10.*log10(Rate)

%add noise. 2nd argument is snr in dBs.
ncode = awgn(tcode,snr,'measured',1234);

%4-bit quantization. symmetrical around zero
q = quantiz(7.5*ncode,[-7:1:7]);

%---------------------------------------
% convert to VHDL simulation format
%---------------------------------------
fid = fopen('fecdecin.txt','wt');
for i = 1:size(q)
fprintf(fid,'%d\n',q(i));
end;
status = fclose(fid);

