%heInitializeModel Model configuration
%   heInitializeModel(MULTIRATEBLOCK,CFG,ISTRANSMITTER) initializes the
%   multirate parameters given the configuration selected in MULTIRATEBLOCK
%   and configures the Spectrum Analyzer given the parameters in CFG. The
%   Spectrum Analyzer block provides Spectral Mask measurements if the
%   parameter ISTRANSMITTER is equal to 1, and Occupied Bandwidth
%   measurements if the parameter ISTRANSMITTER is equal to 0.

%   Copyright 2020-2021 The MathWorks, Inc.

function heInitializeModel(MultirateBlock,HEInfo,istransmitter)
               
    % Get parameters from the Multirate Parameters block
    multirateBlockVar = get_param(MultirateBlock, 'MaskWSVariables');
    TWInterpolationIdx = strcmp({multirateBlockVar.Name}, 'TWInterpolation');
    TWInterpolation = multirateBlockVar(TWInterpolationIdx).Value; % Normalized transition
                                                                   % width of FIR Interpolation block
    TWDecimationIdx = strcmp({multirateBlockVar.Name}, 'TWDecimation');
    TWDecimation = multirateBlockVar(TWDecimationIdx).Value; % Normalized transition
                                                             % width of FIR Decimation block
    AstopInterpolationIdx = strcmp({multirateBlockVar.Name}, 'AstopInterpolation');
    AstopInterpolation = multirateBlockVar(AstopInterpolationIdx).Value; % Stopband attenuation
                                                                         % of FIR Interpolation block  
    AstopDecimationIdx = strcmp({multirateBlockVar.Name}, 'AstopDecimation');
    AstopDecimation = multirateBlockVar(AstopDecimationIdx).Value; % Stopband attenuation
                                                                   % of FIR Decimation block
    OSRIdx = strcmp({multirateBlockVar.Name}, 'OSR'); 
    OSR = multirateBlockVar(OSRIdx).Value; % Oversampling factor

    % Comment FIR blocks if OSR is equal to 1 (no oversampling is applied)
    if OSR == 1
        set_param([gcs '/FIR Interpolation'],'Commented','through');
        set_param([gcs '/FIR Decimation'],'Commented','through');
    else
        set_param([gcs '/FIR Interpolation'],'Commented','off');
        set_param([gcs '/FIR Decimation'],'Commented','off');
    end
    
    % Set spectrum analyzer configuration 
    spectrumAnalyzer = find_system(gcs,'BlockType','SpectrumAnalyzer');
    if ~isempty(spectrumAnalyzer) 
        
        % Get HE channel bandwidth
        cbwMHz = str2double(erase(HEInfo.WaveformConfig.ChannelBandwidth,'CBW')); % Channel bandwidth in MHz  
        
        % From IEEE P802.11ax/D7.0 Section 27.3.19.1
        rbw = 100e3;  % resolution bandwidth
        vbw = 7.5e3;  % video bandwidth

        % Configure Spectrum Analyzer block
        for index = 1:length(spectrumAnalyzer)
            scopeConfig = get_param(spectrumAnalyzer{index},'ScopeConfiguration');
            scopeConfig.RBWSource = 'Property';
            scopeConfig.RBW = num2str(rbw);
            scopeConfig.AveragingMethod = 'Exponential';
            scopeConfig.ForgettingFactor = num2str(helperVBWtoFF(vbw,HEInfo.Fs*OSR));
    
            if istransmitter
                % From IEEE P802.11ax/D7.0 Section 27.3.19.1
                dBrLimits = [-40 -40 -28 -20 0 0 -20 -28 -40 -40]; % Power limits in dBr
                if cbwMHz == 20
                    fLimits = [-Inf -1.5*cbwMHz -cbwMHz -(cbwMHz/2+0.5) -(cbwMHz/2-0.25) ...
                        (cbwMHz/2-0.25) (cbwMHz/2+0.5) cbwMHz 1.5*cbwMHz Inf];
                else
                    fLimits = [-Inf -1.5*cbwMHz -cbwMHz -(cbwMHz/2+0.5) -(cbwMHz/2-0.5) ...
                        (cbwMHz/2-0.5) (cbwMHz/2+0.5) cbwMHz 1.5*cbwMHz Inf];
                end
                scopeConfig.SpectralMask.EnabledMasks = 'Upper';
                scopeConfig.SpectralMask.ReferenceLevel = 'Spectrum peak';
                scopeConfig.SpectralMask.UpperMask = [fLimits*1e6; dBrLimits].'; % fLimits in Hz.
            else
                scopeConfig.ChannelMeasurements.Algorithm = 'Occupied BW';
                scopeConfig.ChannelMeasurements.Span = cbwMHz*1e6;
            end
        end
            
    end

    % Load parameters to workspace
    assignin('base','OSR',OSR);
    assignin('base','TWInterpolation',TWInterpolation);
    assignin('base','TWDecimation',TWDecimation);
    assignin('base','AstopInterpolation',AstopInterpolation);
    assignin('base','AstopDecimation',AstopDecimation);
    
end

