% x4 - data setup for PAM values
%
% Use PAM=2 for data values (-1,1) as in:
% M. Ghosh and C. L. Weber, Maximum-likelihood blind equalization,
% Optical Engineering, June 1992, Vol. 31, No. 6, pp. 1224-1228.
% 

if( K == 1)

  G = [];

% set PAM before running
%
%  PAM = 2;	% number of signal levels, must be even

% set SNR and N before calling x4
%
%  SNR = 9;
%
%  N = 6;		% N data samples

  LOADED = 4;

% set impulse response coefficients for ISI channel
%
% from Fig. 2, H(z) = 0.407 z^-2 (z+0.952) (z+1.05)
% which is an approximate double null at the band edge
%
%  FIR = [0.407 0.815 0.407]';
%
% an exact double null at the band edge
% H(z) = 0.4 z^-2 (z+1) (z+1)
%
%%%  FIR = [0.4 0.8 0.4]';		
%
% from Fig. 6
%
%  FIR = [.227 .460 .688 .460 .227]';
%
% define FIR before running x4...

%%  FIR = FIR / norm(FIR);	% normalize so |FIR| = 1

  FIR_save = FIR;		% save copy of original FIR

  FIR_length = length(FIR);

  Memory = FIR_length - 1;	% channel memory size

  PAM_values = [1:2:(2*PAM-1)] - PAM;

  var_x = sum(PAM_values.*PAM_values)/PAM;	% input signal variance

  states = FSM( PAM_values, Memory);

  MM_max = size(states,2);      % number of symbol values

  M = MM_max;			% number of states

  zstate_0 = 0;			% NOT initial 0's in the channel

  zstate = 1;			% initial state

  x = PAM_values(1+floor(PAM*rand(1,N)));	% input sequence

  trans = FSM_trans( states, FIR);	% get state transition matrix

  y = FSM_out( x, FIR);			% get FSM output

  var_noise = 10^(-SNR/10) * var_x * sum(FIR.*FIR);	% noise variance

  noise = sqrt(var_noise)*randn(1,N);	% random noise

  z = y + noise;			% signal plus noise

else	% K > 1

  error( 'sorry... isi stuff only works with K=1');

  if( LOADED ~= 4)
    error( 'initialize with K = 1 first');
  end

end
