forked from nostar/imbe_vocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsp_sub.cc
278 lines (244 loc) · 6.97 KB
/
dsp_sub.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Project 25 IMBE Encoder/Decoder Fixed-Point implementation
* Developed by Pavel Yazev E-mail: [email protected]
* Version 1.0 (c) Copyright 2009
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#include "typedef.h"
#include "basic_op.h"
#include "imbe.h"
#include "tbls.h"
#include "dsp_sub.h"
#include "math_sub.h"
#include "encode.h"
#include "imbe_vocoder_impl.h"
//-----------------------------------------------------------------------------
// PURPOSE:
// Perform inverse DCT
//
//
// INPUT:
// in - pointer to input data
// m_lim - input data's size
// i_lim - result's size
// out - pointer to save result
//
// OUTPUT:
// None
//
// RETURN:
// Saved in out result of conversion
//
//-----------------------------------------------------------------------------
void imbe_vocoder_impl::idct(Word16 *in, Word16 m_lim, Word16 i_lim, Word16 *out)
{
UWord16 angl_step, angl_intl, angl_intl_2;
UWord16 angl_acc;
Word32 sum;
Word16 i, m;
if(m_lim == 1)
{
angl_intl = CNST_0_5_Q1_15;
angl_intl_2 = CNST_1_0_Q1_15;
}
else
{
angl_intl = div_s ((Word16) CNST_0_5_Q5_11, m_lim << 11); // calculate 0.5/m_lim
angl_intl_2 = shl(angl_intl, 1);
}
angl_step = angl_intl;
for(i = 0; i < i_lim; i++)
{
sum = 0;
angl_acc = angl_step;
for(m = 1; m < m_lim; m++)
{
sum = L_add(sum, L_shr( L_mult(in[m], cos_fxp(angl_acc)), 7));
angl_acc += angl_step;
}
sum = L_add(sum, L_shr( L_deposit_h(in[0]), 8));
out[i] = extract_l(L_shr_r (sum, 8));
angl_step += angl_intl_2;
}
}
//-----------------------------------------------------------------------------
// PURPOSE:
// Perform DCT
//
//
// INPUT:
// in - pointer to input data
// m_lim - input data's size
// i_lim - result's size
// out - pointer to save result
//
// OUTPUT:
// None
//
// RETURN:
// Saved in out result of conversion
//
//-----------------------------------------------------------------------------
void imbe_vocoder_impl::dct(Word16 *in, Word16 m_lim, Word16 i_lim, Word16 *out)
{
UWord16 angl_step, angl_intl, angl_intl_2, angl_begin;
UWord16 angl_acc;
Word32 sum;
Word16 i, m;
if(m_lim == 1)
{
angl_intl = CNST_0_5_Q1_15;
angl_intl_2 = CNST_1_0_Q1_15;
}
else
{
angl_intl = div_s ((Word16) CNST_0_5_Q5_11, m_lim << 11); // calculate 0.5/m_lim
angl_intl_2 = shl(angl_intl, 1);
}
// Calculate first coefficient
sum = 0;
for(m = 0; m < m_lim; m++)
sum = L_add(sum, L_deposit_l(in[m]));
out[0] = extract_l(L_mpy_ls(sum, angl_intl_2));
// Calculate the others coefficients
angl_begin = angl_intl;
angl_step = angl_intl_2;
for(i = 1; i < i_lim; i++)
{
sum = 0;
angl_acc = angl_begin;
for(m = 0; m < m_lim; m++)
{
sum = L_add(sum, L_deposit_l(mult(in[m], cos_fxp(angl_acc))));
angl_acc += angl_step;
}
out[i] = extract_l(L_mpy_ls(sum, angl_intl_2));
angl_step += angl_intl_2;
angl_begin += angl_intl;
}
}
void imbe_vocoder_impl::fft_init(void)
{
Word16 i, fft_len2, shift, step, theta;
fft_len2 = shr(FFTLENGTH, 1);
shift = norm_s(fft_len2);
step = shl(2, shift);
theta = 0;
for(i = 0; i <= fft_len2; i++)
{
wr_array[i] = cos_fxp(theta);
wi_array[i] = sin_fxp(theta);
if(i >= (fft_len2 - 1))
theta = ONE_Q15;
else
theta = add(theta, step);
}
}
// Subroutine FFT: Fast Fourier Transform
// ***************************************************************
// * Replaces data by its DFT, if isign is 1, or replaces data *
// * by inverse DFT times nn if isign is -1. data is a complex *
// * array of length nn, input as a real array of length 2*nn. *
// * nn MUST be an integer power of two. This is not checked *
// * The real part of the number should be in the zeroeth *
// * of data , and the imaginary part should be in the next *
// * element. Hence all the real parts should have even indeces *
// * and the imaginary parts, odd indeces. *
// * *
// * Data is passed in an array starting in position 0, but the *
// * code is copied from Fortran so uses an internal pointer *
// * which accesses position 0 as position 1, etc. *
// * *
// * This code uses e+jwt sign convention, so isign should be *
// * reversed for e-jwt. *
// ***************************************************************
//
// Q values:
// datam1 - Q14
// isign - Q15
#define SWAP(a,b) temp1 = (a);(a) = (b); (b) = temp1
void imbe_vocoder_impl::fft(Word16 *datam1, Word16 nn, Word16 isign)
{
Word16 n, mmax, m, j, istep, i;
Word16 wr, wi, temp1;
Word32 L_tempr, L_tempi;
Word16 *data;
Word32 L_temp1, L_temp2;
Word16 index, index_step;
// Use pointer indexed from 1 instead of 0
data = &datam1[-1];
n = shl(nn,1);
j = 1;
for( i = 1; i < n; i+=2 )
{
if ( j > i)
{
SWAP(data[j],data[i]);
SWAP(data[j+1],data[i+1]);
}
m = nn;
while ( m >= 2 && j > m )
{
j = sub(j,m);
m = shr(m,1);
}
j = add(j,m);
}
mmax = 2;
// initialize index step
index_step = nn;
while ( n > mmax)
{
istep = shl(mmax,1); // istep = 2 * mmax
index = 0;
index_step = shr(index_step,1);
wr = ONE_Q15;
wi = 0;
for ( m = 1; m < mmax; m+=2)
{
for ( i = m; i <= n; i += istep)
{
j = i + mmax;
// tempr = wr * data[j] - wi * data[j+1]
L_temp1 = L_shr(L_mult(wr,data[j]),1);
L_temp2 = L_shr(L_mult(wi,data[j+1]),1);
L_tempr = L_sub(L_temp1,L_temp2);
// tempi = wr * data[j+1] + wi * data[j]
L_temp1 = L_shr(L_mult(wr,data[j+1]),1);
L_temp2 = L_shr(L_mult(wi,data[j]),1);
L_tempi = L_add(L_temp1,L_temp2);
// data[j] = data[i] - tempr
L_temp1 = L_shr(L_deposit_h(data[i]),1);
data[j] = round(L_sub(L_temp1,L_tempr));
// data[i] += tempr
data[i] = round(L_add(L_temp1,L_tempr));
// data[j+1] = data[i+1] - tempi
L_temp1 = L_shr(L_deposit_h(data[i+1]),1);
data[j+1] = round(L_sub(L_temp1,L_tempi));
// data[i+1] += tempi
data[i+1] = round(L_add(L_temp1,L_tempi));
}
index = add(index,index_step);
wr = wr_array[index];
if (isign < 0)
wi = negate(wi_array[index]);
else
wi = wi_array[index];
}
mmax = istep;
}
}