% ////////////////////////////////////////////////////////////////////
% FILE,     HBF2LInit.m
% PROJECT,  HBF2L
% AUTHOR,   weitao.du
% DATE,     2007.Jun.11th
% VERSION,  0.1
% ////////////////////////////////////////////////////////////////////
% DECRIPTION
% 2 Level Half Band Fir Filter Design and Simulation, data initial
% ////////////////////////////////////////////////////////////////////

function simData = HBF2LInit(cfg);
  fprintf(1, 'HBF2LInit(), run\n');
  simData.NOUSE = 'NOUSE';
  simData = FilterInit(cfg, simData);
  simData = GenStim(cfg, simData);
  

end  % function HBF2LInit();

% ////////////////////////////////////////////////////////////////////
% FilterInit(), Init the filter data
% ////////////////////////////////////////////////////////////////////

function simData = FilterInit(cfg, simData)

  wNoise    = cfg.wNoise       ;
  sine      = cfg.sine         ;
  stepUp    = cfg.stepUp       ;
  stimType  = cfg.stimType     ;
  stimLen   = cfg.stimLen      ;
                               ;
  inputWL   = cfg.inputWL      ;
  coeffWL   = cfg.coeffWL      ;
  outputWL  = cfg.outputWL     ;
                               ;
  K1        = cfg.K1           ;
  K2        = cfg.K2           ;
             
  passBand1 = cfg.passBand1    ;
  passBand2 = cfg.passBand2    ;

  tapHbf1   = 4*K1 -2          ; % for Matlab 'firhalfband()'
  tapHbf2   = 4*K2 -2          ;              

  % get coefficents 
  coeff1All = firhalfband(tapHbf1, passBand1) .';
  coeff2All = firhalfband(tapHbf2, passBand2) .';
  
  % Fixed Point Coeff
    % all taps coeff
  coeff1AllFxp = coeff1All ./ max(coeff1All);
  coeff2AllFxp = coeff2All ./ max(coeff2All);
  maxCoeffVal  = 2^(coeffWL-1) - 1;     % coeffWL include the sign bit 
  coeff1AllFxp = floor(coeff1AllFxp*maxCoeffVal+0.5);
  coeff2AllFxp = floor(coeff2AllFxp*maxCoeffVal+0.5);
    % the condense coefficents, not include 0 and max value
  coeff1CdsFxp = coeff1AllFxp(1:2:2*K1);
  coeff2CdsFxp = coeff2AllFxp(1:2:2*K2);

  hHfbFxp = fvtool(coeff1AllFxp,1, coeff2AllFxp, 1, 'Grid', 'on', 'Legend', 'on');
  legend(hHfbFxp, 'LEVEL1', 'LEVEL2');

  filterInit.coeff1All    =  coeff1All    ;
  filterInit.coeff2All    =  coeff2All    ;
  filterInit.coeff1AllFxp =  coeff1AllFxp ;
  filterInit.coeff2AllFxp =  coeff2AllFxp ;
  filterInit.coeff1CdsFxp =  coeff1CdsFxp ; 
  filterInit.coeff2CdsFxp =  coeff2CdsFxp ;  
  filterInit.hHfbFxp      =  hHfbFxp      ;

  simData.filterInit      =  filterInit   ;

end  % fucntion FiterInit()

% ////////////////////////////////////////////////////////////////////
% GenStim(), generate the stimulation data
% ////////////////////////////////////////////////////////////////////

function simData = GenStim(cfg, simData)
  inputBw   = cfg.inputBw      ;
  inputFs   = cfg.inputFs      ;
  wNoise    = cfg.wNoise       ;
  sine      = cfg.sine         ;
  stepUp    = cfg.stepUp       ;
  stimType  = cfg.stimType     ;
  stimLen   = cfg.stimLen      ;
                               ;
  inputWL   = cfg.inputWL      ;
  coeffWL   = cfg.coeffWL      ;
  outputWL  = cfg.outputWL     ;
                               ;
  K1        = cfg.K1           ;
  K2        = cfg.K2           ;
             
  passBand1 = cfg.passBand1    ;
  passBand2 = cfg.passBand2    ;

  inputData = 0;
  if(stimType == wNoise) 
    inputData = BandNoise(stimLen, inputBw, inputFs );
    inputData = inputData*(2^(inputWL-1)-1); % to fixed point
    inputData = floor(inputData + 0.5);
  end
  if(stimType == stepUp)
    inputData = [0:stimLen-1] .';
    inputData = mod(inputData, (2^inputWL-1)-1);
  end

  simData.inputData = inputData;

end % function GenStim()

% ////////////////////////////////////////////////////////////////////
% function BandNoise(), generate band noise signal, that 
% amplitude rescale to [-1, 1] 
% ////////////////////////////////////////////////////////////////////

function out = BandNoise(len, bw, fs)
  dBw = 1;
  out = wgn(len, 1, dBw);
  
  % out = zeros(len, 1); 
  % out(1) = 1; 
  maxOut = max(abs(out));
  out = out/maxOut;

  passBw    = bw;
  stopBw    = bw + bw/20;
  % use kasier window design fir
  fcuts = [passBw, stopBw];
  % gain of pass band, stop band
  mags = [100 0];
  % ripple of pass band, stop band
  devs = [1E-3 2E-2];
  [n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fs);
  % hh is fir coefficents  
  hh = fir1(n,Wn,ftype,kaiser(n+1,beta));

  % freqz(hh);

  out = conv(out, hh);
  out = out(1:len).';
end
