clear
rand( 'state', 7654 );
randn( 'state', 7654 );

% Suppose we want a [ 7, 4 ] Hamming code.
% Q1: Based on the following expressions, what must m be?  +++
m = 3; 
n = 2^m-1
k = n-m

% Produce the parity-check matrix.
H = hammgen(m); 
G = gen2par(H)';
% Q2: How do H and G differ from the ones discussed on the wikipedia page?
% Q3: Is the code still systematic?  How can you tell?

% Generate some data.
N_target = 80; %1e6;
N = k * floor( N_target/k );
data = round( rand( N, 1 ) );
d = reshape( data, k, N/k );

% Encode the data with the generator matrix.
c = rem( G * d, 2 );
% Q4: Can you see the encoded data in the coded data stream?

% Map to antipodal signaling (+1, -1)
x = 1 - 2*c;

% run the simulation at a particular EbNo level
EbNo_dB = 3;
ii = 1;
% -----------------------
% EbNos = 4:12;
% for ii = 1:length( EbNos ),
% EbNo_dB = EbNos(ii);
% -----------------------

    % add noise to the coded data
    EbNo = 10^(EbNo_dB/10);
    
    % Q5: what is the code rate, R_fec, in terms of k and n?  +++
    R_fec = ;
    % Q6: How is EsNo related to EbNo and R_fec?   +++
    EsNo = EbNo  R_fec;
    Es = 1;  % 0, 1 centered around 0.5
    sigma = sqrt( Es / EsNo / 2 );  % sigma^2 = No/2
    noise = sigma * randn( size( x ) );
    y = x + noise;

    % Do hard decision decoding
    % Q8: what decision rule involving y will give a hard decoding?  +++
    hd = ;
    % Q7: Do you think this signal should make any hard decision errors?
    % figure(1); hist( y(:), 20 );

    % Q8: Compute syndromes from the parity matrix
    syn = rem( H * hd, 2 );
    % Convert syndromes from binary to decimal to easily use the lookup
    % table
    syn2dec = 2.^[ m-1:-1:0 ];
    syn_loc = syn2dec * syn;

    % Create a look up table to convert syndromes to correction vectors
    correction = syndtable(H)';
    
    % Apply the correction vectors via modulo 2 addition:
    decoded = rem( hd + correction( :, 1+syn_loc ) , 2 );

    errors = decoded(m+1:end,:) ~= d;
    errors_uc = hd(m+1:end,:) ~= d;
    errors_hamm = decoded ~= c;

    % display some of the decoding steps for small data sets:
    if N < 81,
        d = d
        c = c
        hd = hd
        syn_loc = syn_loc
        decoded = decoded
        errors = errors
        errors_hamm = errors_hamm
    end

    BER(ii) = sum( errors(:) ) / N;
    BER_uc(ii) = sum( errors_uc(:) ) / N;
    theory(ii) = berawgn( EbNo_dB, 'psk', 2, 'nondiff' );

%-----------------    
% end
% 
% EbNos = EbNos
%-----------------    


BER = BER
BER_uc = BER_uc
theory = theory

%-----------------    
% figure(m); semilogy( EbNos, theory, 'b', EbNos, BER_uc, 'r', EbNos, BER, 'k' );
% legend( 'theory', 'symbol error rate', 'hamming BER' ); grid on; axis( [ EbNos(1) EbNos(end) 1e-6 0.1 ] )
%-----------------    


% Now, set:
%    N_target = 1e6;
% 
% Then, uncomment lines of code that fall between lines of: %--------------
%    Run the code for m = 3, 4, 5, 6
%
% Q9: What is the code rate in each case?
%
% Q10: Assuming an uncoded data rate of Rbit = 1 Mbps, what symbol rate
% does each of these choices represent?
% 
% Q11: Assuming the uncoded signal required 1 MHz of bandwidth, what
% signalling bandwidth does each of these choices require?
%
% Q12: What is the coding gain you can measure at BER = 1e-5 for each of
% these choices?
%
% Q13: Does Hamming code performance (coding gain) increase with block size
% (n)?  NOTE: this is *representative* of most FEC approaches.
% 
% Q14: Does Hamming code performance (coding gain) increase with coding
% rate (k/n)?  NOTE: this behavior is *opposite* that of most FEC approaches.
%
% Q15: Would you expect improved performance if a decoding algorithm worked
% with soft decision data rather than hard decision as used above?
%
% Q16: Suppose you had the following received data sequence.  Which
% three recieved values are least reliable and most reasonable for a soft
% decision algorithm to invert?
%       1.3, -0.4, 0.1, -1.1, 0.2, -0.1, 0.8, -0.9
 

% Suppose you have the following data:
d = [ 1:100 ]';

% Q17: if exactly four consecutive bits are inverted by noise in the 
% channel, would the Hamming codes examined above be able to correct all
% four bit inversions?
%
% Q18: Could they correct any of them under certain conditions?
%
% Q19: Give matlab code to represent feeding this data into a 10 x 10
% interleaver and reading it back out interleaved.
temp = ;
interleaved = 

% Q20: What are the first twenty values of the interleaved sequence?

% Assume the data is (1) interleaved after coding, (2) sent through the 
% channel which caused exactly four consecutive bit inversions, (3)
% deinterleaved, and (4) passed through the Hamming decoder.
% Q21: Will the hamming code be able to correct all of the bit inversions?
%
% Q22: What is the maximum length of a consecutive string of bit inversions
% that could be corrected through using this interleaver together with a 
% single-error correcting Hamming code?

