2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "g72x_priv.h"
29 #define M_PI 3.14159265358979323846264338
32 #define BUFFER_SIZE (1<<14) /* Should be (1<<14) */
33 #define SAMPLE_RATE 11025
36 static void g721_test (void) ;
37 static void g723_test (double margin
) ;
39 static void gen_signal_double (double *data
, double scale
, int datalen
) ;
40 static int error_function (double data
, double orig
, double margin
) ;
42 static int oct_save_short (short *a
, short *b
, int len
) ;
45 main (int argc
, char *argv
[])
50 { printf ("Usage : %s <test>\n", argv
[0]) ;
51 printf (" Where <test> is one of the following:\n") ;
52 printf (" g721 - test G721 encoder and decoder\n") ;
53 printf (" g723 - test G721 encoder and decoder\n") ;
54 printf (" all - perform all tests\n") ;
58 bDoAll
=!strcmp (argv
[1], "all");
60 if (bDoAll
|| ! strcmp (argv
[1], "g721"))
65 if (bDoAll
|| ! strcmp (argv
[1], "g723"))
71 { printf ("Mono : ************************************\n") ;
72 printf ("Mono : * No '%s' test defined.\n", argv
[1]) ;
73 printf ("Mono : ************************************\n") ;
87 g723_test (double margin
)
88 { static double orig_buffer
[BUFFER_SIZE
] ;
89 static short orig
[BUFFER_SIZE
] ;
90 static short data
[BUFFER_SIZE
] ;
92 G72x_STATE encoder_state
, decoder_state
;
95 int code
, position
, max_err
;
97 private_init_state (&encoder_state
) ;
98 encoder_state
.encoder
= g723_24_encoder
;
99 encoder_state
.codec_bits
= 3 ;
101 private_init_state (&decoder_state
) ;
102 decoder_state
.decoder
= g723_24_decoder
;
103 decoder_state
.codec_bits
= 3 ;
105 memset (data
, 0, BUFFER_SIZE
* sizeof (short)) ;
106 memset (orig
, 0, BUFFER_SIZE
* sizeof (short)) ;
108 printf (" g723_test : ") ;
111 gen_signal_double (orig_buffer
, 32000.0, BUFFER_SIZE
) ;
112 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
113 orig
[k
] = (short) orig_buffer
[k
] ;
115 /* Write and read data here. */
118 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
119 { code
= encoder_state
.encoder (orig
[k
], &encoder_state
) ;
120 data
[k
] = decoder_state
.decoder (code
, &decoder_state
) ;
121 if (abs (orig
[k
] - data
[k
]) > max_err
)
123 max_err
= abs (orig
[k
] - data
[k
]) ;
127 printf ("\n\nMax error of %d at postion %d.\n", max_err
, position
) ;
129 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
130 { if (error_function (data
[k
], orig
[k
], margin
))
131 { printf ("Line %d: Incorrect sample A (#%ld : %d should be %d).\n", __LINE__
, k
, data
[k
], orig
[k
]) ;
132 oct_save_short (orig
, data
, BUFFER_SIZE
) ;
144 #define SIGNAL_MAXVAL 30000.0
145 #define DECAY_COUNT 1000
148 gen_signal_double (double *gendata
, double scale
, int gendatalen
)
152 ramplen
= DECAY_COUNT
;
154 for (k
= 0 ; k
< gendatalen
; k
++)
156 amp
= scale
* k
/ ((double) ramplen
) ;
157 else if (k
> gendatalen
- ramplen
)
158 amp
= scale
* (gendatalen
- k
) / ((double) ramplen
) ;
160 gendata
[k
] = amp
* (0.4 * sin (33.3 * 2.0 * M_PI
* ((double) (k
+1)) / ((double) SAMPLE_RATE
))
161 + 0.3 * cos (201.1 * 2.0 * M_PI
* ((double) (k
+1)) / ((double) SAMPLE_RATE
))) ;
165 } /* gen_signal_double */
168 error_function (double data
, double orig
, double margin
)
171 if (fabs (orig
) <= 500.0)
172 error
= fabs (fabs (data
) - fabs(orig
)) / 2000.0 ;
173 else if (fabs (orig
) <= 1000.0)
174 error
= fabs (data
- orig
) / 3000.0 ;
176 error
= fabs (data
- orig
) / fabs (orig
) ;
179 { printf ("\n\n*******************\nError : %f\n", error
) ;
183 } /* error_function */
186 oct_save_short (short *a
, short *b
, int len
)
190 if (! (file
= fopen ("error.dat", "w")))
193 fprintf (file
, "# Not created by Octave\n") ;
195 fprintf (file
, "# name: a\n") ;
196 fprintf (file
, "# type: matrix\n") ;
197 fprintf (file
, "# rows: %d\n", len
) ;
198 fprintf (file
, "# columns: 1\n") ;
200 for (k
= 0 ; k
< len
; k
++)
201 fprintf (file
, "% d\n", a
[k
]) ;
203 fprintf (file
, "# name: b\n") ;
204 fprintf (file
, "# type: matrix\n") ;
205 fprintf (file
, "# rows: %d\n", len
) ;
206 fprintf (file
, "# columns: 1\n") ;
208 for (k
= 0 ; k
< len
; k
++)
209 fprintf (file
, "% d\n", b
[k
]) ;
213 } /* oct_save_short */