⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smcbg.m

📁 FS1016源代码
💻 M
字号:
% MATLAB SIMULATION OF NSA FS-1016 CELP v3.2
% COPYRIGHT (C) 1995-99 ANDREAS SPANIAS AND TED PAINTER
%
% This Copyright applies only to this particular MATLAB implementation
% of the FS-1016 CELP coder.  The MATLAB software is intended only for educational
% purposes.  No other use is intended or authorized.  This is not a public
% domain program and distribution to individuals or networks is strictly
% prohibited.  Be aware that use of the standard in any form is goverened
% by rules of the US DoD.  Therefore patents and royalties may apply to
% authors, companies, or committees associated with this standard, FS-1016.  For
% questions regarding the MATLAB implementation please contact Andreas
% Spanias at (480) 965-1837.  For questions on rules,
% royalties, or patents associated with the standard, please contact the DoD.
%
% ALL DERIVATIVE WORKS MUST INCLUDE THIS COPYRIGHT NOTICE.
%
% ******************************************************************
% SMCBG
%
% PORTED TO MATLAB FROM CELP 3.2a C RELEASE
% 8-4-94
%
% ******************************************************************
%
% DESCRIPTION
%
% Smooth codebook gains when many errors are detected in the Hamming
% block.
%
% DESIGN NOTES
%
% Smooths stochastic codebook gains when errors are detected.
%
% If the variance of past cbgain values is within the range VARLIMIT,
% the validity of the current cbgain value is tested.  If the current
% value of cbgain is within the range CBGAINLIMIT, cbgain is passed.
% If CBGAIN is not within  the range CBGAINLIMIT it is reset to the
% average value of the surrounding cbgain values.
%
% The array OLDCBGAIN contains past values of cbgain.  The array
% GAINS contains current and future values of cbgain.  The array
% VECTOR is constructed from the arrays OLDCBGAIN and GAINS
% depending on the current subframe.  CBGAIN is smoothed based on
% the statistics of VECTOR, which contains the nearest four
% surrounding cbgain values, both past and future values, except
% where future values are not available (subframes 3 and 4).
%
% NOTE:  The smoothing parameters should be capable of adapting
% to various bit error rate estimates.  For example, different
% values of SYNDAVG should select different levels of CBGAINLIMIT,
% VARLIMIT, and SYNDAVG.
%
% VARIABLES
%
% INPUTS
%   cbgain       -     Unsmoothed stochastic codebook gain
%   twoerror     -     Error flag indicating occurrence of 2 errors
%   syndavg      -     Error rate estimation parameter
%   gains        -     Vector of gains used to calculate variance
%   subframe     -     Current subframe number
%
% OUTPUTS
%   cbgain       -     Smoothed stochastic codebook gain
%
% INTERNALS
%   abscbgain    -     Absolute value of input cbgain
%   vector       -     Vector of smoothing parameters (gains)
%   var          -     Variance of smoothing parameters
%   avg          -     Mean of smoothing parameters
%   cbgsign      -     Sign of current code book gain (+/-)
%
% GLOBALS
%   enableSMCBG  -     Smoothing enable/disable flag
%   oldcbgain    -     Vector of old stochastic codebook gains
%   FrameCnt     -     Current frame number
%
% CONSTANTS
%   CBGAINHISTORY-     History buffer size, stochastic codebook gain smoothing
%   CBGAINLIMIT  -     Threshold,             "             "              "
%   VARLIMIT     -     Variance limit,        "             "              "
%   SGAINLIMIT   -     Maximum gain,          "             "              "
%   SVARLIMIT    -     Maximum variance,      "             "              "
%   AVGLIMIT     -     Maximum average,       "             "              "
%   SYNDLIMIT    -     Error detection threshold,           "              "
%   TRUE         -     Logical 1
%   FALSE        -     Logical 0
%
% ******************************************************************

function cbgain = smcbg( cbgain, twoerror, syndavg, gains, subframe )

% DECLARE GLOBAL VARIABLES
global enableSMCBG oldcbgain FrameCnt

% DECLARE GLOBAL CONSTANTS
global CBGAINLIMIT VARLIMIT SGAINLIMIT SVARLIMIT AVGLIMIT SYNDLIMIT
global CBGAINHISTORY TRUE FALSE

% SAVE INITIAL GAIN VALUE
abscbgain = abs( cbgain );

% EXCLUDE SUBFRAME 4 FROM SMOOTHING
if subframe ~= 4
    enableSMCBG = TRUE;
end

% DO SMOOTHING IF TWOERROR FLAG OR SYNDLIMIT IS EXCEEDED
if ( ( twoerror == TRUE ) | ( syndavg > SYNDLIMIT ) ) & ...
   ( enableSMCBG == TRUE )
    if subframe == 1
        vector(1:2) = oldcbgain(1:2);
        vector(3:4) = abs(gains(2:3));
    elseif subframe == 2
        vector(1:2) = oldcbgain(1:2);
        vector(3:4) = abs(gains(3:4));
    elseif subframe == 3
        vector(1:3) = oldcbgain(1:3);
        vector(4)   = abs(gains(4));
    elseif subframe == 4
        vector = oldcbgain;
    else
        fprintf( 'smoothcbgain: Error in subframe number\n' );
    end

    % GENERATE STATISTICS OF CBGAIN HISTORY (MEAN AND VARIANCE)
    avg = mean(vector);
    var = std(vector) * std(vector);

    % DETERMINE SIGN OF CB GAIN
    cbgsign = sign(cbgain);

    % CHECK FOR SMOOTHING CONDITION 1
    if ( var < VARLIMIT ) & ( ( abscbgain > avg + CBGAINLIMIT ) | ...
                              ( abscbgain < avg - CBGAINLIMIT) )
        abscbgain = avg;
        fprintf( 'smcbg: cbgain reset (1) to avg cbgain @ frame %d\n', FrameCnt );
        cbgain = cbgsign * abscbgain;

        % DISABLE SMOOTHING FOR SUBFRAME 4
        if subframe == 3
            enableSMCBG = FALSE;
            fprintf( 'smcbg: smoothing disabled for subframe 4\n' );
        end
    end

    % CHECK FOR SMOOTHING CONDITION 2
    if ( var < SVARLIMIT ) & ( abscbgain > SGAINLIMIT ) & ...
       ( avg < AVGLIMIT )  & ( enableSMCBG == TRUE )
        abscbgain = avg;
        fprintf( 'smcbg: cbgain reset (2) to avg cbgain @ frame %d\n', FrameCnt );
        cbgain = cbgsign * abscbgain;

        % DISABLE SMOOTHING FOR SUBFRAME 4
        if subframe == 3
            enableSMCBG = FALSE;
            fprintf( 'smcbg: smoothing disabled for subframe 4\n' );
        end
    end
end

% UPDATE CBGAIN HISTORY
oldcbgain(2:CBGAINHISTORY) = oldcbgain(1:CBGAINHISTORY-1);
oldcbgain(1)               = abscbgain;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -