aboutsummaryrefslogtreecommitdiff
path: root/unittest/t16_8.c
blob: 1463b3030b3ca62d9a407a6f899f4b7ce22cdfb1 (plain)
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
/*
   t16_8.c
   David Rowe
   May 10 2012

   Unit test for 16 <-> 8 kHz sample rate conversion functions.  I

   Evaluated output by plotting using Octave and looking for jaggies:

     pl("../unittest/out16.raw",1,3000)
     pl("../unittest/out8.raw",1,3000)

   Listening to it also shows up anything nasty:

     $ play -s -2 -r 16000 out16.raw
     $ play -s -2 -r 8000 out8.raw

  */

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "codec2_fdmdv.h"

#define N8 159 /* processing buffer size at 8 kHz (odd number deliberate) */
#define N16 (N8 * FDMDV_OS)
#define FRAMES 50
#define TWO_PI 6.283185307
#define FS 16000

#define SINE

int main() {
  float in8k[FDMDV_OS_TAPS_8K + N8];
  short in8k_short[N8];
  float out16k[N16];
  short out16k_short[N16];
  FILE *f16;

  float in16k[FDMDV_OS_TAPS_16K + N16];
  float out8k[N16];
  short out8k_short[N8];
  FILE *f8, *f8in;

  int i, f, t, t1;
  float freq = 800.0;

  f16 = fopen("out16.raw", "wb");
  assert(f16 != NULL);
  f8 = fopen("out8.raw", "wb");
  assert(f8 != NULL);
  f8in = fopen("in8.raw", "wb");
  assert(f8in != NULL);

  /* clear filter memories */
  for (i = 0; i < FDMDV_OS_TAPS_8K; i++) in8k[i] = 0.0;
  for (i = 0; i < FDMDV_OS_TAPS_16K; i++) in16k[i] = 0.0;

  t = t1 = 0;
  for (f = 0; f < FRAMES; f++) {
#ifdef DC
    for (i = 0; i < N8; i++) in8k[FDMDV_OS_TAPS_8K + i] = 16000.0;
#endif
#ifdef SINE
    for (i = 0; i < N8; i++, t++)
      in8k[FDMDV_OS_TAPS_8K + i] =
          16000.0 * cos(TWO_PI * t * freq / (FS / FDMDV_OS));
#endif
    for (i = 0; i < N8; i++) in8k_short[i] = (short)in8k[i];
    fwrite(in8k_short, sizeof(short), N8, f8in);

    /* upsample  */
    fdmdv_8_to_16(out16k, &in8k[FDMDV_OS_TAPS_8K], N8);

    /* save 16k to disk for plotting and check out */
    for (i = 0; i < N16; i++) out16k_short[i] = (short)out16k[i];
    fwrite(out16k_short, sizeof(short), N16, f16);

    /* add a 6 kHz spurious signal, down sampler should
       knock this out */
    for (i = 0; i < N16; i++, t1++)
      in16k[i + FDMDV_OS_TAPS_16K] =
          out16k[i] + 16000.0 * cos(TWO_PI * t1 * 6000.0 / FS);

    /* downsample */
    fdmdv_16_to_8(out8k, &in16k[FDMDV_OS_TAPS_16K], N8);

    /* save 8k to disk for plotting and check out */
    for (i = 0; i < N8; i++) out8k_short[i] = (short)out8k[i];
    fwrite(out8k_short, sizeof(short), N8, f8);
  }

  fclose(f16);
  fclose(f8);
  fclose(f8in);
  return 0;
}