% x4 - data setup for data values (-1,1) and time-varying Gauss-Markov FIR
% 

if( K == 1)

  G = [];

% SNR would be time-varying with fixed var_noise.
% To get constant SNR, var_noise must be time varying...

% set SNR, var_noise, N, avg_h, etc. before calling x4

  LOADED = 4;

  FIR_save = zeros(FIR_length,N);

if( GM_H)	% Gauss-Markov
  %
  % initial FIR with steady-state statistics
  %
  %  FIR_save(:,1) = avg_h/(1-alpha) + inv_T * ((stdev_h/sqrt(1-alpha2)) * randn(FIR_length,1));
  %
  % initial FIR equal to the steady-state average
  %
  %  FIR_save(:,1) = avg_h/(1-alpha);
  %
  % initial FIR all 1's
  %
  %  FIR_save(:,1) = ones(FIR_length,1);
  %
  % initial FIR = [1 0 -1]'
  %
  %  FIR_save(:,1) = [1 1 1]';
  %
  % succeeding FIR's have Markov statistics
  %
  % testing alpha > 1
  %
  %  FIR_save(:,1) = ones(FIR_length,1);
  %  alpha_save = alpha;
  %  alpha = 1.0;
  %  alpha2 = alpha*alpha;
  %
    FIR_save(:,1) = alpha*FIR_0 + avg_h + inv_T * (stdev_h * randn(FIR_length,1));

    for i=2:N
      FIR_save(:,i) = alpha*FIR_save(:,i-1) + avg_h + inv_T * (stdev_h * randn(FIR_length,1));
    end
  
else	% Time-varying Gaussian

  for i=1:N
    FIR_save(:,i) = avg_h(:,i) + inv_T * (stdev_h * randn(FIR_length,1));
  end

end

  Memory = FIR_length - 1;     % channel memory size

  states = FSM( [-1 1], 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 = 2*(rand(1,N) > 0.5) - 1;	% input sequence

  xt = [fliplr(states(:,zstate)'), x];

  y = zeros(1,N);

  for i=1:N
    y(i) = xt(i+Memory:-1:i) * FIR_save(:,i);
  end

% for fixed var_noise and varying SNR
%
%  noise = sqrt(var_noise)*randn(1,N);	% random noise
%
% for fixed SNR and varying var_noise
%
%  var_noise = 10^(-SNR/10) * sum(FIR_save.*FIR_save);   % noise variance
%
%  noise = sqrt(var_noise).*randn(1,N);  % random noise
%
% for fixed SNR and fixed average noise. <<-- cost_init(), cost_update(), etc. expect this now
%
  var_noise = 10^(-SNR/10) * sum( sum(FIR_save.*FIR_save)) / N;

  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
