xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/ilbc/simple_lpc_analysis.c (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /******************************************************************
12 
13  iLBC Speech Coder ANSI-C Source Code
14 
15  WebRtcIlbcfix_SimpleLpcAnalysis.c
16 
17 ******************************************************************/
18 
19 #include "modules/audio_coding/codecs/ilbc/simple_lpc_analysis.h"
20 
21 #include "modules/audio_coding/codecs/ilbc/bw_expand.h"
22 #include "modules/audio_coding/codecs/ilbc/constants.h"
23 #include "modules/audio_coding/codecs/ilbc/defines.h"
24 #include "modules/audio_coding/codecs/ilbc/poly_to_lsf.h"
25 #include "modules/audio_coding/codecs/ilbc/window32_w32.h"
26 
27 /*----------------------------------------------------------------*
28  *  lpc analysis (subrutine to LPCencode)
29  *---------------------------------------------------------------*/
30 
WebRtcIlbcfix_SimpleLpcAnalysis(int16_t * lsf,int16_t * data,IlbcEncoder * iLBCenc_inst)31 void WebRtcIlbcfix_SimpleLpcAnalysis(
32     int16_t *lsf,   /* (o) lsf coefficients */
33     int16_t *data,   /* (i) new block of speech */
34     IlbcEncoder *iLBCenc_inst
35     /* (i/o) the encoder state structure */
36                                      ) {
37   int k;
38   int scale;
39   size_t is;
40   int16_t stability;
41   /* Stack based */
42   int16_t A[LPC_FILTERORDER + 1];
43   int32_t R[LPC_FILTERORDER + 1];
44   int16_t windowedData[BLOCKL_MAX];
45   int16_t rc[LPC_FILTERORDER];
46 
47   is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
48   WEBRTC_SPL_MEMCPY_W16(iLBCenc_inst->lpc_buffer+is,data,iLBCenc_inst->blockl);
49 
50   /* No lookahead, last window is asymmetric */
51 
52   for (k = 0; k < iLBCenc_inst->lpc_n; k++) {
53 
54     is = LPC_LOOKBACK;
55 
56     if (k < (iLBCenc_inst->lpc_n - 1)) {
57 
58       /* Hanning table WebRtcIlbcfix_kLpcWin[] is in Q15-domain so the output is right-shifted 15 */
59       WebRtcSpl_ElementwiseVectorMult(windowedData, iLBCenc_inst->lpc_buffer, WebRtcIlbcfix_kLpcWin, BLOCKL_MAX, 15);
60     } else {
61 
62       /* Hanning table WebRtcIlbcfix_kLpcAsymWin[] is in Q15-domain so the output is right-shifted 15 */
63       WebRtcSpl_ElementwiseVectorMult(windowedData, iLBCenc_inst->lpc_buffer+is, WebRtcIlbcfix_kLpcAsymWin, BLOCKL_MAX, 15);
64     }
65 
66     /* Compute autocorrelation */
67     WebRtcSpl_AutoCorrelation(windowedData, BLOCKL_MAX, LPC_FILTERORDER, R, &scale);
68 
69     /* Window autocorrelation vector */
70     WebRtcIlbcfix_Window32W32(R, R, WebRtcIlbcfix_kLpcLagWin, LPC_FILTERORDER + 1 );
71 
72     /* Calculate the A coefficients from the Autocorrelation using Levinson Durbin algorithm */
73     stability=WebRtcSpl_LevinsonDurbin(R, A, rc, LPC_FILTERORDER);
74 
75     /*
76        Set the filter to {1.0, 0.0, 0.0,...} if filter from Levinson Durbin algorithm is unstable
77        This should basically never happen...
78     */
79     if (stability!=1) {
80       A[0]=4096;
81       WebRtcSpl_MemSetW16(&A[1], 0, LPC_FILTERORDER);
82     }
83 
84     /* Bandwidth expand the filter coefficients */
85     WebRtcIlbcfix_BwExpand(A, A, (int16_t*)WebRtcIlbcfix_kLpcChirpSyntDenum, LPC_FILTERORDER+1);
86 
87     /* Convert from A to LSF representation */
88     WebRtcIlbcfix_Poly2Lsf(lsf + k*LPC_FILTERORDER, A);
89   }
90 
91   is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
92   WEBRTC_SPL_MEMCPY_W16(iLBCenc_inst->lpc_buffer,
93                         iLBCenc_inst->lpc_buffer+LPC_LOOKBACK+BLOCKL_MAX-is, is);
94 
95   return;
96 }
97