• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <math.h>
12 
13 #include "modules/audio_coding/codecs/isac/main/source/settings.h"
14 #include "modules/audio_coding/codecs/isac/main/source/codec.h"
15 #include "modules/audio_coding/codecs/isac/main/source/os_specific_inline.h"
16 #include "modules/third_party/fft/fft.h"
17 
WebRtcIsac_InitTransform(TransformTables * tables)18 void WebRtcIsac_InitTransform(TransformTables* tables) {
19   int k;
20   double fact, phase;
21 
22   fact = PI / (FRAMESAMPLES_HALF);
23   phase = 0.0;
24   for (k = 0; k < FRAMESAMPLES_HALF; k++) {
25     tables->costab1[k] = cos(phase);
26     tables->sintab1[k] = sin(phase);
27     phase += fact;
28   }
29 
30   fact = PI * ((double) (FRAMESAMPLES_HALF - 1)) / ((double) FRAMESAMPLES_HALF);
31   phase = 0.5 * fact;
32   for (k = 0; k < FRAMESAMPLES_QUARTER; k++) {
33     tables->costab2[k] = cos(phase);
34     tables->sintab2[k] = sin(phase);
35     phase += fact;
36   }
37 }
38 
WebRtcIsac_Time2Spec(const TransformTables * tables,double * inre1,double * inre2,int16_t * outreQ7,int16_t * outimQ7,FFTstr * fftstr_obj)39 void WebRtcIsac_Time2Spec(const TransformTables* tables,
40                           double* inre1,
41                           double* inre2,
42                           int16_t* outreQ7,
43                           int16_t* outimQ7,
44                           FFTstr* fftstr_obj) {
45   int k;
46   int dims[1];
47   double tmp1r, tmp1i, xr, xi, yr, yi, fact;
48   double tmpre[FRAMESAMPLES_HALF], tmpim[FRAMESAMPLES_HALF];
49 
50 
51   dims[0] = FRAMESAMPLES_HALF;
52 
53 
54   /* Multiply with complex exponentials and combine into one complex vector */
55   fact = 0.5 / sqrt(FRAMESAMPLES_HALF);
56   for (k = 0; k < FRAMESAMPLES_HALF; k++) {
57     tmp1r = tables->costab1[k];
58     tmp1i = tables->sintab1[k];
59     tmpre[k] = (inre1[k] * tmp1r + inre2[k] * tmp1i) * fact;
60     tmpim[k] = (inre2[k] * tmp1r - inre1[k] * tmp1i) * fact;
61   }
62 
63 
64   /* Get DFT */
65   WebRtcIsac_Fftns(1, dims, tmpre, tmpim, -1, 1.0, fftstr_obj);
66 
67   /* Use symmetry to separate into two complex vectors and center frames in time around zero */
68   for (k = 0; k < FRAMESAMPLES_QUARTER; k++) {
69     xr = tmpre[k] + tmpre[FRAMESAMPLES_HALF - 1 - k];
70     yi = -tmpre[k] + tmpre[FRAMESAMPLES_HALF - 1 - k];
71     xi = tmpim[k] - tmpim[FRAMESAMPLES_HALF - 1 - k];
72     yr = tmpim[k] + tmpim[FRAMESAMPLES_HALF - 1 - k];
73 
74     tmp1r = tables->costab2[k];
75     tmp1i = tables->sintab2[k];
76     outreQ7[k] = (int16_t)WebRtcIsac_lrint((xr * tmp1r - xi * tmp1i) * 128.0);
77     outimQ7[k] = (int16_t)WebRtcIsac_lrint((xr * tmp1i + xi * tmp1r) * 128.0);
78     outreQ7[FRAMESAMPLES_HALF - 1 - k] = (int16_t)WebRtcIsac_lrint((-yr * tmp1i - yi * tmp1r) * 128.0);
79     outimQ7[FRAMESAMPLES_HALF - 1 - k] = (int16_t)WebRtcIsac_lrint((-yr * tmp1r + yi * tmp1i) * 128.0);
80   }
81 }
82 
WebRtcIsac_Spec2time(const TransformTables * tables,double * inre,double * inim,double * outre1,double * outre2,FFTstr * fftstr_obj)83 void WebRtcIsac_Spec2time(const TransformTables* tables,
84                           double* inre,
85                           double* inim,
86                           double* outre1,
87                           double* outre2,
88                           FFTstr* fftstr_obj) {
89   int k;
90   double tmp1r, tmp1i, xr, xi, yr, yi, fact;
91 
92   int dims;
93 
94   dims = FRAMESAMPLES_HALF;
95 
96   for (k = 0; k < FRAMESAMPLES_QUARTER; k++) {
97     /* Move zero in time to beginning of frames */
98     tmp1r = tables->costab2[k];
99     tmp1i = tables->sintab2[k];
100     xr = inre[k] * tmp1r + inim[k] * tmp1i;
101     xi = inim[k] * tmp1r - inre[k] * tmp1i;
102     yr = -inim[FRAMESAMPLES_HALF - 1 - k] * tmp1r - inre[FRAMESAMPLES_HALF - 1 - k] * tmp1i;
103     yi = -inre[FRAMESAMPLES_HALF - 1 - k] * tmp1r + inim[FRAMESAMPLES_HALF - 1 - k] * tmp1i;
104 
105     /* Combine into one vector,  z = x + j * y */
106     outre1[k] = xr - yi;
107     outre1[FRAMESAMPLES_HALF - 1 - k] = xr + yi;
108     outre2[k] = xi + yr;
109     outre2[FRAMESAMPLES_HALF - 1 - k] = -xi + yr;
110   }
111 
112 
113   /* Get IDFT */
114   WebRtcIsac_Fftns(1, &dims, outre1, outre2, 1, FRAMESAMPLES_HALF, fftstr_obj);
115 
116 
117   /* Demodulate and separate */
118   fact = sqrt(FRAMESAMPLES_HALF);
119   for (k = 0; k < FRAMESAMPLES_HALF; k++) {
120     tmp1r = tables->costab1[k];
121     tmp1i = tables->sintab1[k];
122     xr = (outre1[k] * tmp1r - outre2[k] * tmp1i) * fact;
123     outre2[k] = (outre2[k] * tmp1r + outre1[k] * tmp1i) * fact;
124     outre1[k] = xr;
125   }
126 }
127