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

📄 smpg.m

📁 实现fs1016w的CELP的低速率语音编解码功能的基于vc开发环境的原代码。
💻 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.
%
% ******************************************************************
% SMPG
%
% PORTED TO MATLAB FROM CELP 3.2a C RELEASE
% 8-7-94
%
% ******************************************************************
%
% DESCRIPTION
%
% Smooth pitch gain when many errors are detected in the
% Hamming block.
%
% DESIGN NOTES
%
% Smoothes pgain (alpha) when errors are detected:
%
% Due to the range of PGAIN, statistical variance is not appropriate.
% Pseudovariance is used and calculated as:
% sum of delta oldpgains/# of deltas
%
% If this variance of past pgain values is within the range VARLIMIT,
% the validity of the current pgain value is tested.  If the current
% value of pgain is within the range PGAINLIMIT, PGAIN is passed.
% If PGAIN is not within that range it is reset to the average
% value of surrounding pgain values.
%
% The array OLDPGAIN contains past values of pgain.  The array
% PGAINS contains current and future values of pgain.  The array
% VECTOR is constructed from the arrays OLDPGAIN and PGAINS
% depending on the current subframe.  PGAIN is smoothed based on
% the statistics of VECTOR, which contains the nearest four
% surrounding pgain values, both past and future values, except
% where future values are not available (subframes 3 and 4).
%
% Absolute values of pgain are used in averaging and reassigning
% pgain.  All reassigned pgains are limited to the range 0.0-1.0.
%
% 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 PGAINLIMIT,
% VARLIMIT, and SYNDLIMIT.
%
% VARIABLES
%
% INPUTS
%   pgain        -     Unsmoothed pitch gain value
%   twoerror     -     Error flag indicating occurrence of 2 errors
%   syndavg      -     Error rate estimation parameter
%   pgains       -     Vector of pgains to compute variance
%   subframe     -     Current subframe number
%
% OUTPUTS
%   pgain        -     Smoothed pitch gain value
%
% INTERNALS
%   vector       -     Vector of smoothing parameters (gains)
%   var          -     Variance of smoothing parameters
%   avg          -     Mean of smoothing parameters
%   abspgain     -     Pgain magnitude
%
% GLOBALS
%   enableSMPG   -     Smoothing enable/disable flag
%   oldpgain     -     Vector of old pitch gains
%   FrameCnt     -     Current frame number
%
% CONSTANTS
%   PGAINHISTORY -     History buffer size, pitch gain smoothing
%   PGAINLIMIT   -     Pitch gain excursion limit, "         "
%   TRUE         -     Logical 1
%   FALSE        -     Logical 0
%   SYNDLIMIT    -     Error detection smoothing threshold
%   VARLIMIT     -     Variance limit for pitch gain history
%
% ******************************************************************

function pgain = smpg( pgain, twoerror, syndavg, pgains, subframe )

% DECLARE GLOBAL VARIABLES
global enableSMPG oldpgain FrameCnt

% DECLARE GLOBAL CONSTANTS
global PGAINHISTORY PGAINLIMIT TRUE FALSE

% DEFINE LOCAL CONSTANTS
SYNDLIMIT = 0.04;
VARLIMIT = 0.2;

% STORE PGAIN MAGNITUDE
abspgain = abs(pgain);

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

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

    % GENERATE STATISTICS OF PITCH GAIN HISTORY (MEAN AND PSUEDO-VARIANCE)
    avg = mean(vector);
    var = sum( abs(vector(1:PGAINHISTORY-1)-vector(2:PGAINHISTORY)) ) / ...
          (PGAINHISTORY-1);

    % CHECK FOR SMOOTHING CONDITION
    if ( var < VARLIMIT ) & ( enableSMPG == TRUE ) & ...
       ( ( abspgain > avg + PGAINLIMIT ) | ...
         ( abspgain < avg - PGAINLIMIT ) )

        % SET TO AVERAGE AND CLAMP TO [-1,+1]
        pgain = avg;
        if pgain > 1.0
            pgain = 1.0;
        end
        if pgain < -1.0
            pgain = -1.0
        end
        fprintf( 'smpg: pgain reset to avg @ frame %d\n', FrameCnt );

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

% UPDATE PGAIN HISTORY
oldpgain(2:PGAINHISTORY) = oldpgain(1:PGAINHISTORY-1);
oldpgain(1)              = abspgain;

⌨️ 快捷键说明

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