1 /*
2    t16_8_short.c
3    David Rowe
4    19 August 2014
5 
6    Unit test for 16 <-> 8 kHz sample rate conversion functions.  I
7    evaluated output by plotting using Octave and looking for jaggies:
8 
9      pl("../unittest/out16_short.raw",1,3000)
10      pl("../unittest/out8.raw",1,3000)
11 
12    Listening to it also shows up anything nasty:
13 
14      $ play -s -2 -r 16000 out16_short.raw
15      $ play -s -2 -r 8000 out8.raw
16 
17   */
18 
19 #include <assert.h>
20 #include <math.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "codec2_fdmdv.h"
24 
25 #define N8                        159 /* procssing buffer size at 8 kHz */
26 #define N16             (N8*FDMDV_OS)
27 #define FRAMES                     100
28 #define TWO_PI            6.283185307
29 #define FS                      16000
30 
31 #define SINE
32 
main()33 int main() {
34     short in8k_short[FDMDV_OS_TAPS_8K + N8];
35     short out16k_short[N16];
36     FILE *f16;
37 
38     short in16k_short[FDMDV_OS_TAPS_16K + N16];
39     short out8k_short[N16];
40     FILE *f8, *f8in;
41 
42     int i,f,t,t1;
43     float freq = 800.0;
44 
45     f16 = fopen("out16_short.raw", "wb");
46     assert(f16 != NULL);
47     f8 = fopen("out8_short.raw", "wb");
48     assert(f8 != NULL);
49     f8in = fopen("in8_short.raw", "wb");
50     assert(f8in != NULL);
51 
52     /* clear filter memories */
53     for(i=0; i<FDMDV_OS_TAPS_8K; i++)
54 	in8k_short[i] = 0;
55     for(i=0; i<FDMDV_OS_TAPS_16K; i++)
56 	in16k_short[i] = 0;
57 
58     t = t1 = 0;
59     for(f=0; f<FRAMES; f++) {
60 
61 #ifdef DC
62 	for(i=0; i<N8; i++)
63 	    in8k_short[FDMDV_OS_TAPS_8K+i] = 16000.0;
64 #endif
65 #ifdef SINE
66 	for(i=0; i<N8; i++,t++)
67 	    in8k_short[FDMDV_OS_TAPS_8K+i] = 8000.0*cos(TWO_PI*t*freq/FS);
68 #endif
69 	fwrite(in8k_short, sizeof(short), N8, f8in);
70 
71 	/* upsample  */
72 	fdmdv_8_to_16_short(out16k_short, &in8k_short[FDMDV_OS_TAPS_8K], N8);
73 
74 	fwrite(out16k_short, sizeof(short), N16, f16);
75 
76 	/* add a 6 kHz spurious signal for fun, we want down sampler to
77 	   knock this out */
78 	for(i=0; i<N16; i++,t1++)
79 	    in16k_short[i+FDMDV_OS_TAPS_16K] = out16k_short[i] + 8000.0*cos(TWO_PI*t1*6000.0/FS);
80 
81 	/* downsample */
82 	fdmdv_16_to_8_short(out8k_short, &in16k_short[FDMDV_OS_TAPS_16K], N8);
83 
84 	/* save 8k to disk for plotting and check out */
85 	fwrite(out8k_short, sizeof(short), N8, f8);
86 
87     }
88 
89     fclose(f16);
90     fclose(f8);
91     fclose(f8in);
92     return 0;
93 
94 }
95