📄 celpanal.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.
%
% ******************************************************************
% CELPANAL
%
% NSA CELP 3.2a ANALYSIS DRIVER
%
% PORTED TO MATLAB FROM CELP 3.2a C RELEASE
% 8-2-94
%
% ******************************************************************
%
% DESCRIPTION
%
% CELP 3.2a analysis driver
%
% DESIGN NOTES
%
% This driver has been derived from the main loop of the NSA CELP 3.2a
% simulation. CELP analysis accepts a PCM binary speech file as input, then
% produces a hex bitstream (144 bits/frame) and hex file as simulation
% output. Bitstream output is decoded and used for synthesis by CELPSYN.M.
%
% VARIABLES
%
% INTERNALS
%
% NOTE: Many of these variables are initialized in init.m
%
% iarf - Unprocessed input speech data
% snew - Input speech buffer (1 frame ahead)
% sold - Input speech buffer (1 frame behind)
% ssub - Subframe analysis buffer (ll samples = 1/2 old, 1/2 new)
% scale - Input speech scale factor
% minv - Minimum allowed input speech sample value
% maxv - Maximum allowed input speech sample value
% ahpf - H(Z) denominator, input speech highpass filter
% bhpf - H(Z) numerator, input speech highpass filter
% dhpf1 - Filter memory, input highpass filter
% hamw - Hamming window, one full frame in length
% ll - Frame length, in samples
% l - Subframe length, in samples
% no - LPC filter order
% fcn - LPC predictor coefficients {1/A(Z) coefficients}, synthesis filter
% rcn - Synthesis filter reflection coefficients
% lspflag - LSP error flag
% newfreq - LSP frequencies derived from LPC direct form coefficients (quantized)
% findex - LSP quantization table indicies
% sbits - Bit allocation, LSP quantization
% pbits - Bit allocation, pitch lag quantization
% unqfreq - Unquantized version of LSP frequencies
% dm2 - Spectral distortion measures, current frame
% sumdm2 - Average spectral distortion measures, summation
% framedm2 - Average spectral distortion measures, framecount
% stream - CELP analysis output bitstream
% pointer - CELP analysis output bitstream current pointer
% lsp - Linearly interpolated lsps for each subframe of current frame
% nn - Number of subframes
% subframe - Current subframe under analysis
% fci - Direct form LPC coefficients, obtained from interpolated, quantized LSPs
% fc - Copy of fci
% nseg - Cumulative subframe counter
% sfl, sfh - Subframe boundaries within current frame buffer
% v - Optimum synthesis filter excitation derived from codebook searches
% lp - Pitch analysis subframe length, in samples
% tauptr - Pitch delay pointer
% minptr - Minimum delay pointer
% pdencode - Pitch delay (tau) encoding table
% pindex - Pitch gain index into pitch gain table
% cbindex - Stochastic codebook index
% cbbits - Bit allocation, stochastic codebook index
% gindex - Stochastic codebook gain index (into gain table)
% cbgbits - Bit allocation, stochastic codebook gain
% protect - Hamming bit protection enable/disable flag
% bitprotect - Bitstream indicies of bits protected by the hamming code
% codeword - Hamming (15,11) codeword, formed from protected bits
% paritybit - Extraneous paritybit used for codeword parity overall
% hmatrix - Hamming matrix, used during hamming codeword generation
% sync - Sync bit, CELP analysis output bitstream
% savestream - Copy of CELP analysis output bitstream
% bitperm - Bit stream permutation table
% pstream - Permuted version of CELP analysis output bitstream
% line - One frame of hex bitstream CELP analysis output,
% used to generate hex file 'ofile.cha'
%
% GLOBALS
% FrameCnt - Current frame number
% SubFrameCnt - Current subframe number
% guiFDM2 - Spectral distortion averaging frame count
% guisdm2 - Spectral distortion summation over FDM2 frames
% guidm2 - Spectral distortion measures, current frame
% guiState - Graphical output or status output mode
% 0 = status mode (non-interactive), 1 = graphics mode
%
% CONSTANTS
% OMEGA - Expansion coefficient for LPC filter pole bandwidth
% CODELENGTH1 - Hamming codeword overall length (in bits)
% CODELENGTH2 - Hamming codeword data length (in bits)
%
% ******************************************************************
% DECLARE GLOBAL VARIABLES
global FrameCnt guiState SubFrameCnt guiFDM2 guisdm2 guidm2
% SCALE AND CLAMP INPUT DATA TO 16-BIT INTEGER RANGE
snew = min( [ (iarf .* scale)'; maxv ] )';
snew = max( [ snew'; minv ] )';
% RUN HIGHPASS FILTER TO ELIMINATE HUM AND LF NOISE
[ snew, dhpf1 ] = filter( bhpf, ahpf, snew, dhpf1 );
% MAINTAIN SSUB SUBFRAME ANALYSIS BUFFER. IT IS
% 1/2 FRAME BEHIND SNEW AND 1/2 FRAME AHEAD OF SOLD.
ssub( 1:ll/2 ) = sold( (ll/2)+1:ll );
ssub( (ll/2)+1:ll ) = snew( 1:ll/2 );
% UPDATE SOLD WITH CONTENTS OF SNEW
sold = snew;
% DO LPC SPECTRAL ANALYSIS (OPEN LOOP)
[ fcn, rcn ] = autohf( snew, hamw, ll, no, OMEGA );
% GENERATE LSP FREQUENCIES FROM PREDICTOR COEFFICIENTS
[ newfreq, lspflag ] = pctolsp2( fcn, no );
% ALERT USER TO LSP ERROR CONDITIONS
if lspflag == TRUE
fprintf( 'celp: Bad "new" lsp at frame: %d\n', FrameCnt );
end
% SAVE UNQUANTIZED LSP
unqfreq = newfreq;
% QUANTIZE LSPs
[ newfreq, findex ] = lsp34( newfreq, no, sbits );
% MEASURE SPECTRAL DISTORTION DUE TO LSP QUANTIZATION
[ dm2, sumdm2, framedm2 ] = specdist( unqfreq, newfreq, no, sumdm2, framedm2 );
guidm2 = dm2; guisdm2 = sumdm2; guiFDM2 = framedm2;
% PACK LSP QUANTIZATION VALUES INTO BIT STREAM
[ stream, pointer ] = cpack( findex, sbits, pointer, stream );
% LINEARLY INTERPOLATE FRAME LSPs FOR EACH SUBFRAME
lsp = intanaly( newfreq, nn, no );
% SEARCH ADAPTIVE AND STOCHASTIC CODEBOOKS FOR EACH SUBFRAME
for subframe = 1:nn
% OBTAIN SHORT TERM PREDICTOR COEFFICIENTS FROM QUANTIZED LSPs
fci = lsptopc( lsp(subframe,:)', no );
fc = fci;
nseg = nseg + 1;
% SEARCH ADAPTIVE AND STOCHASTIC CODEBOOKS
sfl = ((subframe-1)*l)+1; sfh = sfl+l-1;
v(sfl:sfh) = csub( ssub(sfl:sfh), l, lp );
% PACK PARAMETER INDICIES INTO BITSTREAM ARRAY
% PITCH DELAY INDEX
if rem( subframe, 2 ) ~= 0
[ stream, pointer ] = ...
packtau( tauptr-minptr, pbits(1), pdencode, stream, pointer );
else
[ stream, pointer ] = ...
cpack( tauptr-minptr, pbits(2), pointer, stream );
end
% PITCH GAIN INDEX
[ stream, pointer ] = cpack( pindex, pbits(3), pointer, stream );
% STOCHASTIC CODEWORD INDEX
[ stream, pointer ] = cpack( cbindex-1, cbbits, pointer, stream );
% STOCHASTIC GAIN INDEX
[ stream, pointer ] = cpack( gindex, cbgbits, pointer, stream );
end
% ADD BIT ERROR PROTECTION TO THE ANALYSIS BIT STREAM
if protect == TRUE
% FIRST, EXTRACT BITS TO PROTECT
codeword( 1:CODELENGTH2 ) = stream( bitprotect( 1:CODELENGTH2 ) );
% HAMMING ENCODE THE CODEWORD
[ paritybit, codeword ] = encodham( CODELENGTH1, CODELENGTH2, hmatrix, codeword );
% PACK FUTURE BIT
[ stream, pointer ] = cpack( 0, 1, pointer, stream );
% PACK PARITY BITS
[ stream, pointer ] = cpack( codeword( CODELENGTH2+1:CODELENGTH1 ), ...
[1,1,1,1], pointer, stream );
% TOGGLE AND PACK THE SYNC BIT
sync = ~sync;
[ stream, pointer ] = cpack( sync, 1, pointer, stream );
end
% COPY BIT STREAM
savestream = stream;
% REARRANGE ORIGINAL BIT STREAM FOR TRANSMISSION
pstream = stream( bitperm );
% GENERATE HEX FILE, REPRESENTING THE CELP ANALYSIS BIT STREAM
line = puthex( STREAMBITS, pstream );
fprintf( 'ofile.cha', '%s\n', line );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -