1 /*
2  * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3  * Universitaet Berlin.  See the accompanying file "COPYRIGHT" for
4  * details.  THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5  */
6 
7 /* $Header: /cvsroot/sox/sox/libgsm/preprocess.c,v 1.1 2007/09/06 16:50:55 cbagwell Exp $ */
8 
9 #include	<stdio.h>
10 #include	<assert.h>
11 
12 #include "private.h"
13 
14 #include	"gsm.h"
15 
16 /*	4.2.0 .. 4.2.3	PREPROCESSING SECTION
17  *
18  *  	After A-law to linear conversion (or directly from the
19  *   	Ato D converter) the following scaling is assumed for
20  * 	input to the RPE-LTP algorithm:
21  *
22  *      in:  0.1.....................12
23  *	     S.v.v.v.v.v.v.v.v.v.v.v.v.*.*.*
24  *
25  *	Where S is the sign bit, v a valid bit, and * a "don't care" bit.
26  * 	The original signal is called sop[..]
27  *
28  *      out:   0.1................... 12
29  *	     S.S.v.v.v.v.v.v.v.v.v.v.v.v.0.0
30  */
31 
32 
Gsm_Preprocess(struct gsm_state * S,word * s,word * so)33 void Gsm_Preprocess (
34 	struct gsm_state * S,
35 	word		 * s,
36 	word 		 * so )		/* [0..159] 	IN/OUT	*/
37 {
38 
39 	word       z1 = S->z1;
40 	longword L_z2 = S->L_z2;
41 	word 	   mp = S->mp;
42 
43 	word 	   	s1;
44 	longword      L_s2;
45 
46 	longword      L_temp;
47 
48 	word		msp, lsp;
49 	word		SO;
50 
51 	longword	ltmp;		/* for   ADD */
52 	ulongword	utmp;		/* for L_ADD */
53 
54 	register int		k = 160;
55 
56 	while (k--) {
57 
58 	/*  4.2.1   Downscaling of the input signal
59 	 */
60 		SO = SASR( *s, 3 ) << 2;
61 		s++;
62 
63 		assert (SO >= -0x4000);	/* downscaled by     */
64 		assert (SO <=  0x3FFC);	/* previous routine. */
65 
66 
67 	/*  4.2.2   Offset compensation
68 	 *
69 	 *  This part implements a high-pass filter and requires extended
70 	 *  arithmetic precision for the recursive part of this filter.
71 	 *  The input of this procedure is the array so[0...159] and the
72 	 *  output the array sof[ 0...159 ].
73 	 */
74 		/*   Compute the non-recursive part
75 		 */
76 
77 		s1 = SO - z1;			/* s1 = gsm_sub( *so, z1 ); */
78 		z1 = SO;
79 
80 		assert(s1 != MIN_WORD);
81 
82 		/*   Compute the recursive part
83 		 */
84 		L_s2 = s1;
85 		L_s2 <<= 15;
86 
87 		/*   Execution of a 31 bv 16 bits multiplication
88 		 */
89 
90 		msp = SASR( L_z2, 15 );
91 		lsp = L_z2-((longword)msp<<15); /* gsm_L_sub(L_z2,(msp<<15)); */
92 
93 		L_s2  += GSM_MULT_R( lsp, 32735 );
94 		L_temp = (longword)msp * 32735; /* GSM_L_MULT(msp,32735) >> 1;*/
95 		L_z2   = GSM_L_ADD( L_temp, L_s2 );
96 
97 		/*    Compute sof[k] with rounding
98 		 */
99 		L_temp = GSM_L_ADD( L_z2, 16384 );
100 
101 	/*   4.2.3  Preemphasis
102 	 */
103 
104 		msp   = GSM_MULT_R( mp, -28180 );
105 		mp    = SASR( L_temp, 15 );
106 		*so++ = GSM_ADD( mp, msp );
107 	}
108 
109 	S->z1   = z1;
110 	S->L_z2 = L_z2;
111 	S->mp   = mp;
112 }
113