function commAWGNBlockCompileCheck(block, muObj)
% commAWGNBlockCompileCheck Compile check registration
% function for pre-R2023a AWGN block. The function determines the sample
% rate of the block and sets the Samples per symbol parameter accordingly.
% Also, enables the new block mask by setting the LegacyMode to 'off'.
%
%  Input arguments:
%    block - handle to the block being updated muObj - model updater object

% Copyright 2022-2023 The MathWorks, Inc.

 %#ok<*AGROW>
 
appendCompileCheck(muObj, block, @CollectData, ...
  @switchSymbolPeriodWithSPS);
end

%--------------------------------------------------------------------------
% Collect block information under compile stage
function blkInfo = CollectData(block, ~)

% Get dimensions, and sample time status
blkInfo.SampleTime = get_param(block, 'CompiledSampleTime');
blkInfo.Dimensions = get_param(block, 'CompiledPortDimensions');

end
%--------------------------------------------------------------------------
function switchSymbolPeriodWithSPS(block, muObj, blkInfo)
% Post compile action:
if askToReplace(muObj, block)
  funcSet = {'insertFrameConversionBlock',block};
  reasonStr = message('comm:system:AWGNChannel:reason').getString;
  if doUpdate(muObj)
    TsymStr = get_param(block,'SymbolPeriod');
    TsymStr = strtok(TsymStr,'%');

    noiseMode = get_param(block,'noiseMode');
    switch noiseMode
      case 'Variance from port'
        set_param(block,'NoiseMethod','Variance');
        set_param(block,'VarianceSource','Input port');
      case 'Variance from mask'
        set_param(block,'NoiseMethod','Variance');
        set_param(block,'VarianceSource','Parameter');
      otherwise
        set_param(block,'NoiseMethod',noiseMode)
    end
    set_param(block,'LegacyMode', 'off')
    oldSeed = evalin('base',get_param(block,'Seed'));
    if numel(oldSeed) > 1
        set_param(block,'Seed',sprintf("%d %%%s",oldSeed(1),mat2str(oldSeed)))
    end
    set_param(block,'RandomStream','mt19937ar with seed')

    sps = evalin('base',TsymStr) / (blkInfo.SampleTime(1) / blkInfo.Dimensions.Outport(2));
    if (sps - floor(sps)) > sqrt(eps)
      % Samples per symbol is non-integer. Check if Eb/No or Es/No is used.
      noiseMethod = get_param(block,'NoiseMethod');
      if contains(noiseMethod,'Eb/No')
        ebno = get_param(block,'EbNo');
        bps = get_param(block,'BitsPerSymbol');
        spsStr = sprintf('(%s / %e)', TsymStr, ...
          blkInfo.SampleTime(1) / blkInfo.Dimensions.Outport(2));
        snr = sprintf("%s + 10*log10((%s)/(%s))",ebno,bps,spsStr);
      elseif contains(noiseMethod,'Es/No')
        esno = get_param(block,'EsNo');
        spsStr = sprintf('(%s / %e)', TsymStr, ...
          blkInfo.SampleTime(1) / blkInfo.Dimensions.Outport(2));
        snr = sprintf("%s - 10*log10(%s)",esno,spsStr);
      end
      set_param(block,'NoiseMethod','Signal to noise ratio  (SNR)')
      set_param(block,'SNR',snr)
    end

    % Make sure that SPS is integer
    set_param(block,'SamplesPerSymbol', ...
      sprintf('round(%s / %e)', TsymStr, ...
      blkInfo.SampleTime(1) / blkInfo.Dimensions.Outport(2)))
  end
  appendTransaction(muObj, block, reasonStr, {funcSet});
end
end
