File: subband.c

package info (click to toggle)
toolame 02i-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 604 kB
  • ctags: 558
  • sloc: ansic: 12,338; sh: 111; makefile: 96
file content (202 lines) | stat: -rw-r--r-- 5,279 bytes parent folder | download
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
#include "common.h"
#include "encoder.h"
#include "enwindow.h"

void create_dct_matrix (double filter[16][32]);

/************************************************************************
*
* window_subband()
*
* PURPOSE:  Overlapping window on PCM samples
*
* SEMANTICS:
* 32 16-bit pcm samples are scaled to fractional 2's complement and
* concatenated to the end of the window buffer #x#. The updated window
* buffer #x# is then windowed by the analysis window #c# to produce the
* windowed sample #z#
*
************************************************************************/

void
window_subband (short **buffer, double z[64], int k)
{
  typedef double XX[2][HAN_SIZE];
  static XX *x;
  double *xk;
  int i;
  static int off[2] = { 0, 0 };
  static char init = 0;
  double t;
  double *ep0, *ep1, *ep2, *ep3, *ep4, *ep5, *ep6, *ep7;
  if (!init)
    {
      x = (XX *) mem_alloc (sizeof (XX), "x");
      memset (x, 0, 2 * HAN_SIZE * sizeof (double));
      init = 1;
    }
  xk = (*x)[k];

  /* replace 32 oldest samples with 32 new samples */
  for (i = 0; i < 32; i++)
    xk[31 - i + off[k]] = (double) *(*buffer)++ / SCALE;

  ep0 = &enwindow[0];
  ep1 = &enwindow[64];
  ep2 = &enwindow[128];
  ep3 = &enwindow[192];
  ep4 = &enwindow[256];
  ep5 = &enwindow[320];
  ep6 = &enwindow[384];
  ep7 = &enwindow[448];

  /* shift samples into proper window positions */
  for (i = 0; i < 64; i++)
    {
      t = xk[(i + off[k]) & (512 - 1)] * *ep0++;
      t += xk[(i + 64 + off[k]) & (512 - 1)] * *ep1++;
      t += xk[(i + 128 + off[k]) & (512 - 1)] * *ep2++;
      t += xk[(i + 192 + off[k]) & (512 - 1)] * *ep3++;
      t += xk[(i + 256 + off[k]) & (512 - 1)] * *ep4++;
      t += xk[(i + 320 + off[k]) & (512 - 1)] * *ep5++;
      t += xk[(i + 384 + off[k]) & (512 - 1)] * *ep6++;
      t += xk[(i + 448 + off[k]) & (512 - 1)] * *ep7++;
      z[i] = t;
    }

  off[k] += 480;		/*offset is modulo (HAN_SIZE-1) */
  off[k] &= HAN_SIZE - 1;

}


/************************************************************************
*
* filter_subband()
*
* PURPOSE:  Calculates the analysis filter bank coefficients
*
* SEMANTICS:
*      The windowed samples #z# is filtered by the digital filter matrix #m#
* to produce the subband samples #s#. This done by first selectively
* picking out values from the windowed samples, and then multiplying
* them by the filter matrix, producing 32 subband samples.
*
************************************************************************/
void
create_dct_matrix (double filter[16][32])
{
  register int i, k;

  for (i = 0; i < 16; i++)
    for (k = 0; k < 32; k++)
      {
	if ((filter[i][k] =
	     1e9 * cos ((double) ((2 * i + 1) * k * PI64))) >= 0)
	  modf (filter[i][k] + 0.5, &filter[i][k]);
	else
	  modf (filter[i][k] - 0.5, &filter[i][k]);
	filter[i][k] *= 1e-9;
      }
}

void
filter_subband (z, s)
     double FAR z[HAN_SIZE], s[SBLIMIT];
{
  double yprime[32];
  register int i, j;

  static double m[16][32];
  static int init = 0;

  if (init == 0)
    {
      init++;
      create_dct_matrix (m);
    }

  yprime[0] = z[16];
  for (i = 1; i <= 16; i++)
    yprime[i] = z[i + 16] + z[16 - i];
  for (i = 17; i <= 31; i++)
    yprime[i] = z[i + 16] - z[80 - i];

  for (i = 15; i >= 0; i--)
    {
      register double s0 = 0.0, s1 = 0.0;
      register double *mp = m[i];
      register double *xinp = yprime;
      for (j = 0; j < 8; j++)
	{
	  s0 += *mp++ * *xinp++;
	  s1 += *mp++ * *xinp++;
	  s0 += *mp++ * *xinp++;
	  s1 += *mp++ * *xinp++;
	}
      s[i] = s0 + s1;
      s[31 - i] = s0 - s1;
    }
}


#ifdef NEWWS
/***********************************************************************
 An implementation of a modified window subband as seen in Kumar & Zubair's
"A high performance software implentation of mpeg audio encoder"
I think from IEEE ASCAP 1996 proceedings
 
input: shift in 32*12 (384) new samples into a 864 point buffer.
ch - which channel we're looking at.
 
This routine basically does 12 calls to window subband all in one go.
Not yet called in code. here for testing only.
************************************************************************/
void
window_subband12 (short **buffer, int ch)
{
  static double x[2][864];	/* 2 channels, 864 buffer for each */
  double *xk;
  double t[12];			/* a temp buffer for summing values */
  double y[12][64];		/* 12 output arrays of 64 values */
  int i, j, k, m;
  static double c[512];		/* enwindow array */
  static int init = 0;
  double c0;

  xk = x[ch];			/* an easier way of referencing the array */

  /* shift 384 new samples into the buffer */
  for (i = 863; i >= 384; i--)
    xk[i] = xk[i - 384];
  for (i = 383; i >= 0; i--)
    xk[i] = (double) *(*buffer)++ / SCALE;

  for (j = 0; j < 64; j++)
    {
      for (k = 0; k < 12; k++)
	t[k] = 0;
      for (i = 0; i < 8; i++)
	{
	  m = i * 64 + j;
	  c0 = c[m];
	  t[0] += c0 * xk[m + 352];
	  t[1] += c0 * xk[m + 320];
	  t[2] += c0 * xk[m + 288];
	  t[3] += c0 * xk[m + 256];
	  t[4] += c0 * xk[m + 224];
	  t[5] += c0 * xk[m + 192];
	  t[6] += c0 * xk[m + 160];
	  t[7] += c0 * xk[m + 128];
	  t[8] += c0 * xk[m + 96];
	  t[9] += c0 * xk[m + 64];
	  t[10] += c0 * xk[m + 32];
	  t[11] += c0 * xk[m];
	}
      for (i = 0; i < 12; i++)
	{
	  y[i][j] = t[i];
	}
    }
}
#endif /* NEWWS */