% Function used for commeq2/* --> commeq3/* replacement
function ReplaceEqualizer(block,h,replaceBlockReasonStr)

%   Copyright 2018-2020 The MathWorks, Inc.

myBlock = cleanBlockName(h,block);
refBlock = cleanBlockName(h,get_param(block,'ReferenceBlock'));

if contains(refBlock, 'Decision')
  replacementBlock = 'commeq3/Decision Feedback Equalizer';
else
  replacementBlock = 'commeq3/Linear Equalizer';
end
libName = 'commeq3';

if ~contains(myBlock, {'Sign', 'Normalized', 'Variable Step'})
  if askToReplace(h, myBlock)
    olpParams = get_param(myBlock, 'MaskNames');
    
    count = 1;
    if contains(myBlock, 'RLS')
      paramNamesValues{1}= 'Algorithm';
      paramNamesValues{2}= 'RLS';
    elseif contains(myBlock, 'CMA')
      paramNamesValues{1}= 'Algorithm';
      paramNamesValues{2}= 'CMA';
    elseif contains(myBlock, 'LMS')
      paramNamesValues{1}= 'Algorithm';
      paramNamesValues{2}= 'LMS';
    end
    
    count = count + 2;
    for p=1:length(olpParams)
      switch olpParams{p}
        case 'delay'
          paramNamesValues{count}= 'ReferenceTap';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'initWeights'
          paramNamesValues{count}= 'InitialWeights';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'leakage'
          % Not supported
          count = count - 2;
        case 'mode'
          paramNamesValues{count}= 'TrainingFlagInputPort';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'nSamp'
          paramNamesValues{count}= 'InputSamplesPerSymbol';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'nTaps'
          paramNamesValues{count}= 'NumTaps';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'nFwdTaps'
          paramNamesValues{count}= 'NumForwardTaps';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'nFdbkTaps'
          paramNamesValues{count}= 'NumFeedbackTaps';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'outErr'
          paramNamesValues{count}= 'ErrorOutputPort';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'outWeights'
          paramNamesValues{count}= 'TapWeightsOutputPort';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'sigConst'
          paramNamesValues{count}= 'Constellation';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'stepSize'
          paramNamesValues{count}= 'StepSize';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'lambda'
          paramNamesValues{count}= 'ForgettingFactor';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        case 'invCorr'
          paramNamesValues{count}= 'InitialInverseCorrelationMatrix';
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
        otherwise
          paramNamesValues{count}= olpParams{p};
          paramNamesValues{count+1}= get_param(myBlock,olpParams{p});
      end
      count = count + 2;
    end
    load_system(libName);

    if doUpdate(h)
      insertDelayBlock(block)
    end
    
    funcSetReplace = uReplaceBlock(h, myBlock, replacementBlock, ...
      paramNamesValues{:});
    appendTransaction(h, myBlock, h.ReplaceBlockReasonStr, ...
      {funcSetReplace});
    
  end
else
  % do not replace, just inform user about obsolete block
  funcSet = 0;
  if contains(myBlock, {'Linear'})
    replaceBlockReasonStr = sprintf(['consider replacing with Linear Equalizer from commeq3 library. ', ...
      'Sign LMS, Normalized LMS, and Variable Step LMS algorithms are not supported by the new equalizer block.']);
  else
  end
  
  appendTransaction(h, myBlock, replaceBlockReasonStr, {funcSet});
end
end
%--------------------------------------------------------------------------
function insertDelayBlock(block)

blkLHandles = get_param(block, 'LineHandles');
for count =1: length(blkLHandles.Outport)
  blkLHOutport = get(blkLHandles.Outport(count));
  InsertExtraBlock(block, blkLHOutport, 'Post', 80, 0,...
    'dspsigops','dspsigops/Delay',...
    {'DelayLength'},{'1'});
end
end
