Ivo Penzar ivo penzarfgmicrotec hr www fgmicrotec hr

  • Slides: 47
Download presentation
© Ivo Penzar ivo. penzar@fgmicrotec. hr www. fgmicrotec. hr Fixed point resampling of PCM

© Ivo Penzar ivo. penzar@fgmicrotec. hr www. fgmicrotec. hr Fixed point resampling of PCM audio data 1

© Ivo Penzar Fixed point audio PCM data resampling Thanks! 2

© Ivo Penzar Fixed point audio PCM data resampling Thanks! 2

© Ivo Penzar Fixed point audio PCM data resampling Agenda: • • • Sampling

© Ivo Penzar Fixed point audio PCM data resampling Agenda: • • • Sampling + quantization Nyquist-Shannon Theorem Examples PCM audio data Downsampling: 8 k. Hz 44. 1 k. Hz Upsampling: 44. 1 k. Hz 8 k. Hz Fixed point arithmetics Implementation & demonstration Analysis and conclusions Questions & answers 3

© Ivo Penzar Fixed point audio PCM data resampling 4

© Ivo Penzar Fixed point audio PCM data resampling 4

© Ivo Penzar Fixed point audio PCM data resampling Nyquist – Shannon Theorem: •

© Ivo Penzar Fixed point audio PCM data resampling Nyquist – Shannon Theorem: • Analog signal R(t), t > 0 with periodic component FR(w); periodic frequency w > 0. • Samples r(ti), i = 1, 2, 3, . . . ; sampling frequency f > 0. • Samples {r(ti)} preserve info about the harmonic FR(w) iff Nyquist condition: f > 2 w. Harry Nyquist, 1889 – 1976: Certain topics in Telegraph Transmition Theory [1928]; Claude E. Shannon, 1916 – 2001: Communication in the presence of noise [1949]. 5

© Ivo Penzar Fixed point audio PCM data resampling 6

© Ivo Penzar Fixed point audio PCM data resampling 6

© Ivo Penzar Fixed point audio PCM data resampling • Ferrari Spider, tyres 265/40/18,

© Ivo Penzar Fixed point audio PCM data resampling • Ferrari Spider, tyres 265/40/18, speeding v = 150 km/h = 41. 67 m/s. Tyres height 0. 4 * 265 mm = 10. 6 cm, R = (2. 54 * 18 + 2 * 10. 6) cm = 0. 7 m. • • Wheel: rotation frequency w = v / Rp = 18. 96 rps, Dtw = 1 / w = 53 ms. Movie camera: shooting frequency f = 24 fps, Dtf = 1 / f = 42 ms. • No Nyquist cond. : w < f < 2 w, Dtf < Dtw < 2 Dtf. • Every Dtf = 1 / 24 s, wheel turns w / f 2 / 3 of circle, looking like turning 1 / 3 of circle backwards. • Alias: vitrual rotating frequency w = f * (1 - f / w) = -6. 38 rps, would be corresponding to v’ = -50 km/h (slower driving backwards)! 7

© Ivo Penzar Fixed point audio PCM data resampling Hi-Fi audio CD: • Human

© Ivo Penzar Fixed point audio PCM data resampling Hi-Fi audio CD: • Human hearing: from 16 Hz to 20 k. Hz, by Nyquist, sampling frequency must be > 40 k. Hz (e. g. , 44. 1 k. Hz or 48 k. Hz). • Stereo, 16 bit quantization, sampling frequency j = 44. 1 k. Hz = 44100 Hz. • Two channels: audio signal R(t), PCM samples -32768 = -215 r(tj) 32767 = 215 - 1. • 60 minutes of recording: 2 * 3600 * 44100 * 2 B 605. 6 MB. • Standard CD capacity: 60 min audio or cca 600 MB data. 8

© Ivo Penzar Fixed point audio PCM data resampling Digital telecommunications (m–law): • Human

© Ivo Penzar Fixed point audio PCM data resampling Digital telecommunications (m–law): • Human speach: below 4 k. Hz, sampling frequency f = 8 k. Hz satisfies Nyquist cond. • Mono, 8 bit quantization, sampling frequency f = 8 k. Hz = 8000 Hz. • Single channel R(t): PCM samples -128 = -27 r(ti) 127 = 27 - 1. • Every second: 8000 * 8 b = 64 kb, throughput required: 64 kbps. • ISDN BRA: two digital channels, 2 x 64 kbps, each channel: voice call or full-duplex 64 kbps data (dial-up Internet). 9

© Ivo Penzar Fixed point audio PCM data resampling 10

© Ivo Penzar Fixed point audio PCM data resampling 10

© Ivo Penzar Fixed point audio PCM data resampling • Hi-Fi {r(tj )}: j

© Ivo Penzar Fixed point audio PCM data resampling • Hi-Fi {r(tj )}: j = 44. 1 k. Hz, Dt = Dtj = 1 / j = 10 / 441 ms, tj = j Dt (j 1). • m–law {r(ti)}: f = 8 k. Hz, Dt = Dti = 1 / f = 10 / 80 ms, ti = i Dt (i 1). • Downsampling: {r(ti)} {r(tj )} ? • Upsampling: {r(tj )} {r(ti)} ? • Simplifications: single channel {r(tj )} (averaging stereo channels); 16 -bit quantization also for {r(ti)} (scaling by 28, bitwise rotations in C/C++). 11

© Ivo Penzar Fixed point audio PCM data resampling • If 48 k. Hz

© Ivo Penzar Fixed point audio PCM data resampling • If 48 k. Hz (another standard Hi-Fi frequency) instead of 44. 1 k. Hz: 48000 / 8000 = 6, six r(tj ) samples would correspond to the single r(ti) sample. • Downsampling by simple averaging: r(ti) [r(t 6 i) + r(t 6 i+1) +. . . r(t 6 i+5)] / 6. • Averaging: low-pass antialiasing filter! Decimation (taking each sixth) requires extra low-pass antialiasing pre-filtration. • Upsampling (inverse) by e. g. linear interpolation. 12

© Ivo Penzar Fixed point audio PCM data resampling • However, 44. 1 k.

© Ivo Penzar Fixed point audio PCM data resampling • However, 44. 1 k. Hz not a multiple of 8 k. Hz: c : = Dt / Dt = 441 / 80, Dt = (5 + 41 / 80) Dt = (5 + 1 / 2 + 1 / 80) Dt. • Base interval [0, 10 ms] : t 80 = t 441 = 10 ms, t 40 = (t 220 + t 221) / 2 = 5 ms. • Periodicity: t 80 n+i = ti + n *10 ms, • Btw, voice codecs (lossy compressions) GSM/EFR or AMR (UMTS Vo. IP) also operate on 5, 10 or 20 ms frames, and similarly audio codecs MP 3 (MPEG 1 -2, Layer 3) or AAC (MPEG 2). t 441 n+j = tj + n *10 ms. 13

© Ivo Penzar Fixed point audio PCM data resampling 14

© Ivo Penzar Fixed point audio PCM data resampling 14

© Ivo Penzar Fixed point audio PCM data resampling Resampling 44. 1 k. Hz

© Ivo Penzar Fixed point audio PCM data resampling Resampling 44. 1 k. Hz vs. 8 k. Hz: • t 1 = t 5 + 41 / 80 Dt, t 2 = t 11 + 2 / 80 Dt, t 0 = t 0 + 0 / 80 Dt : = 0, t 40 = t 220 + 40 / 80 Dt, • ti = tj(i) + qi Dt, i 0. • qi : = (441 i mod 80) / 80, qi + q 80 -i = 1, • ji = j(i); j 0 = 0, j 2 i : = 11 i, j 2 i+1 : = 11 i + 5, 0 i 79; t 3 = t 16 + 43 / 80 Dt. . . t 80 = t 441 = t 440 + 80 / 80 Dt; q 80 : = 1; 0 i 80. j 1 = 5, j 2 = 11, j 3 = 16 … 0 2 i 80; 0 < 2 i + 1 < 40. j 40+2 i+1 : = 11 i + 6, 15

© Ivo Penzar Fixed point audio PCM data resampling 16

© Ivo Penzar Fixed point audio PCM data resampling 16

© Ivo Penzar Fixed point audio PCM data resampling Upsampling by linear interpolation: •

© Ivo Penzar Fixed point audio PCM data resampling Upsampling by linear interpolation: • inputr(t 0), output {r(t 1), r(t 2), . . . r(t 80)}; {r(t 1), r(t 2), . . . r(t 441)}. • wi : = c qi, w 0 = 0, wi + w 80 -i = c, w 1 = 1 / 441, wm = m w 1 , wn = w 40 + n w 1, • 0 i 80; w 40 = c / 2 = 40 / 441, w 40+m = w 40 + m w 1, w 40+n = n w 1, Dr 1 : = r(t 1) - r(t 0), r(t 1) r(t 0) + (c – w 0) Dr 1, r(tj) r(tj-1) + c Dr 1, j = 2, 3, 4, 5; Dr 2 : = r(t 2) - r(t 1), r(t 6) r(t 1) + (c – w 1) Dr 2, r(tj) r(tj-1) + c Dr 2, j = 7, 8, 9, 10, 11. 17 c : = f / j = Dt / Dt; w 80 = c = 80 / 441; 0 m = 2 i 40; 0 < n = 2 i + 1 < 40;

© Ivo Penzar Fixed point audio PCM data resampling Upsampling by linear interpolation, cont.

© Ivo Penzar Fixed point audio PCM data resampling Upsampling by linear interpolation, cont. : • upsample (input r(t 0), r(t 1). . . r(t 80); output r(t 1). . . r(t 441)): Dri : = r(ti) - r(ti-1), r(tj(i-1)+1) r(ti-1) + w 81 -i Dri, r(tj) r(tj-1) + c Dri, • j = j(i-1) + 2, . . . j(i). r(t 0), upsample (r(t 0), r(t 1). . . r(t 80); r(t 1). . . r(t 441)), upsample (r(t 80), r(t 81). . . r(t 160); r(t 442). . . r(t 882)), … We can omit: r(t 0), we can assume: r(t 0) : = r(t 1) or r(t 0) : = 0 (specially if silence preceeds to recording). 18

© Ivo Penzar Fixed point audio PCM data resampling enum {n 8 k. Hz_5

© Ivo Penzar Fixed point audio PCM data resampling enum {n 8 k. Hz_5 ms = 40, n 8 k. Hz_10 ms = 2 * n 8 k. Hz_5 ms, n 44_1 k. Hz_5 ms = 220, n 44_1 k. Hz_10 ms = 2 * n 44_1 k. Hz_5 ms + 1}; const double w[n 8 k. Hz_10 ms] = {. 092971, . 111111, . 129252, . 147392, . 165533, . 002268, . 020408, . 038549, . 056689, . 074830, . 004535, . 022676, . 040816, . 058957, . 077098, . 095238, . 113379, . 131519, . 149660, . 167800, . 097506, . 115646, . 133787, . 151927, . 170068, . 006803, . 024943, . 043084, . 061224, . 079365, . 009070, . 027211, . 045351, . 063492, . 081633, . 099773, . 117914, . 136054, . 154195, . 172336, . 102041, . 120181, . 138322, . 156463, . 174603, . 011338, . 029478, . 047619, . 065760, . 083900, . 013605, . 031746, . 049887, . 068027, . 086168, . 104308, . 122449, . 140590, . 158730, . 176871, inline short round(const double &a) {return (short) (a < 0 ? a -. 5 : a +. 5); }; 19 . 106576, . 124717, . 142857, . 160998, . 179138, . 015873, . 034014, . 052154, . 070295, . 088435, . 018141, . 036281, . 054422, . 072562, . 090703, . 108844, . 126984, . 145125, . 163265, . 181406};

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, floating point implementation: void

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, floating point implementation: void up. Sample 1(short in 0, const short in[n 8 k. Hz_10 ms], short out[n 44_1 k. Hz_10 ms]) {out[n 44_1 k. Hz_10 ms - 1] = in[n 8 k. Hz_10 ms - 1]; bool b. Extra = false; double a = in 0 , wi = w[n 8 k. Hz_10 ms - 1]; for (unsigned i = 0, j = 0, n = 0; n < n 8 k. Hz_10 ms / n 8 k. Hz_5 ms; n++, b. Extra = !b. Extra) for (unsigned k = 0; k < n 8 k. Hz_5 ms; k++, b. Extra = !b. Extra) {const double a 1 = in[i++], b = a 1 - a, v = w [n 8 k. Hz_10 ms - 1] * b; double u = a; out[j++] = round(u += wi * b); for (unsigned m = b. Extra ? 5 : 4; m--; ) out[j++] = round(u += v); a = a 1; wi = w[n 8 k. Hz_10 ms – i - 1]; }} 20

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, floating point implementation, revised

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, floating point implementation, revised (about 7% faster): void up. Sample 2(short in 0, const short in[n 8 k. Hz_10 ms], short out[n 44_1 k. Hz_10 ms]) {out[n 44_1 k. Hz_10 ms - 1] = in[n 8 k. Hz_10 ms - 1]; const double c = w[n 8 k. Hz_10 ms - 1], *ptr. W = w; double a = in 0 , wi = c; for (unsigned n = n 8 k. Hz_10 ms / n 8 k. Hz_5 ms; n--; ) for (unsigned k = n 8 k. Hz_5 ms; k--; ) {const double a 1 = *in++, b = a 1 – a, v = c * b; double u = a; *out++ = round(u += wi * b); if (n + k & 1) *out++ = round(u += v); *out++ = round(u += v); a = a 1; wi = c - *ptr. W++; }} 21

© Ivo Penzar Fixed point audio PCM data resampling Fixed point arithmetics: • r

© Ivo Penzar Fixed point audio PCM data resampling Fixed point arithmetics: • r > 0: r k=0 M-1 N+k 2 k + k=0 N-1 k 2 k-N, representing as a binary array with virtual but fixed place binary point: • sign: 1 bit integral part: M bits binary fraction: N bits N+M-1. . . N+1 N N-1. . . 1 0 Usually: N + M + 1 = 16, 32 or 64, we leave it to the computer to perform integer arithmetics like if it was: 2 N r + 1 / 2 = k=0 M+N-1 k 2 k. 22

© Ivo Penzar Fixed point audio PCM data resampling Fixed point arithmetics, cont. :

© Ivo Penzar Fixed point audio PCM data resampling Fixed point arithmetics, cont. : • In the code, programmer must care about: - representing values by fixed point, e. g. , writing in integral values - properly subscribing fixed point operands for +, -, *, / - properly adjusting virtual binary point before/upon *, / - reading results out of fixed point repr. , e. g. , their integral part values. • In C/C++ notation: using bitwise rotation operators: << and >> corresponding to the multiplication and division by powers of 2. 23

© Ivo Penzar Fixed point audio PCM data resampling Fixed point arithmetics, cont. :

© Ivo Penzar Fixed point audio PCM data resampling Fixed point arithmetics, cont. : • Fixed point arithmetics can be extended to negative values based on the known implementation of negative integers. • For the processor and C/C++ compiler (platform) we assume: - 16 or 32 bit words (integers); - short int represented by 16 bits, long int by 32 bits; - two´s complement implementation for negative integers. • E. g. : Intel x 86, Pentiums and clones (PCs); Motorola 68 xxx (Apple); RISC (UNIX); ARM (PDAs and smartphones); DSPs (embedded platforms). 24

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point representation: •

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point representation: • Input/output PCM samples short int: 16 bits, signed, Most Significant Bit sign. • For the fixed point representation we will use long int (32 bits): - Most Significant Bit sign, - M = 16: 15 bits + 1 additional bit (overflow protection) for the integral part, - N = 32 – 17: 15 bits for the binary fractional part. • Fixed point precision up to 2 -15 / 2 = 2 -16, numerical errors for the upsampling multiplications c*Dri and w 81 -i*Dri : absolute error up to (215 + 215 ) * 2 -16 = 1, relative error up to 1 / 215 = 2 -15. • More detailed numerical analysis for fixed point downsampling and upsampling later! 25

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point implementation: typedef

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point implementation: typedef long fix; // fixed point implementation const int fix. Prec = 15; // fixed point precision const fix. Half = 1 << (fix. Prec - 1); // one over two inline fix to. Fix (const long &a) {return a << fix. Prec; }; inline short to. Short(const fix &a) {return (short) ((a + fix. Half) >> fix. Prec); }; fix w[n 8 k. Hz_10 ms]; for (unsigned j = 0; j < n 8 k. Hz_10 ms; j++) {unsigned long n = (j * n 44_1 k. Hz_10 ms) % n 8 k. Hz_10 ms; if (j == n 8 k. Hz_10 ms - 1) n = n 8 k. Hz_10 ms; w[j] = (1 + n 44_1 k. Hz_5 ms + to. Fix(n)) / n 44_1 k. Hz_10 ms; } 26

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point implementation, cont.

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point implementation, cont. : // const unsigned fix w[n 8 k. Hz_10 ms] = const unsigned short w[n 8 k. Hz_10 ms] = {0 x 0 be 6, 0 x 0 e 39, 0 x 108 b, 0 x 12 de, 0 x 1530, 0 x 004 a, 0 x 029 d, 0 x 04 ef, 0 x 0742, 0 x 0994, 0 x 0095, 0 x 02 e 7, 0 x 0539, 0 x 078 c, 0 x 09 de, 0 x 0 c 31, 0 x 0 e 83, 0 x 10 d 6, 0 x 1328, 0 x 157 a, 0 x 0 c 7 b, 0 x 0 ecd, 0 x 1120, 0 x 1372, 0 x 15 c 5, 0 x 00 df, 0 x 0331, 0 x 0584, 0 x 07 d 6, 0 x 0 a 29, 0 x 0129, 0 x 037 c, 0 x 05 ce, 0 x 0821, 0 x 0 a 73, 0 x 0 cc 5, 0 x 0 f 18, 0 x 116 a, 0 x 13 bd, 0 x 160 f, 0 x 0 d 10, 0 x 0 f 62, 0 x 11 b 5, 0 x 1407, 0 x 1659, 0 x 0174, 0 x 03 c 6, 0 x 0618, 0 x 086 b, 0 x 0 abd, 0 x 01 be, 0 x 0410, 0 x 0663, 0 x 08 b 5, 0 x 0 b 08, 0 x 0 d 5 a, 0 x 0 fac, 0 x 11 ff, 0 x 1451, 0 x 16 a 4, 0 x 0 da 4, 0 x 0 ff 7, 0 x 1249, 0 x 149 c, 0 x 16 ee, 0 x 0208, 0 x 045 b, 0 x 06 ad, 0 x 08 ff, 0 x 0 b 52, 0 x 0252, 0 x 04 a 5, 0 x 06 f 7, 0 x 094 a, 0 x 0 b 9 c, 0 x 0 def, 0 x 1041, 0 x 1293, 0 x 14 e 6, 0 x 1738}; Due to the values as calculated: const unsigned short w[] instead of: const unsigned fix w[] needing only: 80 x 2 = 160 B. 27

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point implementation (almost

© Ivo Penzar Fixed point audio PCM data resampling Upsampling, fixed point implementation (almost 22 x faster than floating point): void up. Sample 3(short in 0, const short in[n 8 k. Hz_10 ms], short out[n 44_1 k. Hz_10 ms]) {out[n 44_1 k. Hz_10 ms - 1] = in[n 8 k. Hz_10 ms - 1]; const unsigned short *ptr. W = w; const fix c = w[n 8 k. Hz_10 ms - 1], c 1 = c + 1; fix wi = c; long a = in 0; for (unsigned n = n 8 k. Hz_10 ms / n 8 k. Hz_5 ms; n--; ) for (unsigned k = n 8 k. Hz_5 ms; k--; ) {const long a 1 = *in++, b = a 1 – a; register fix u = to. Fix(a); register const fix v = c * b; *out++ = to. Short(u += wi * b); if (n + k & 1) *out++ = to. Short(u += v); a = a 1; wi = c 1 - *ptr. W++; }} 28

© Ivo Penzar Fixed point audio PCM data resampling 29

© Ivo Penzar Fixed point audio PCM data resampling 29

© Ivo Penzar Fixed point audio PCM data resampling Downsampling by averaging: {r(t 1),

© Ivo Penzar Fixed point audio PCM data resampling Downsampling by averaging: {r(t 1), r(t 2), . . . r(t 441)}; {r(t 1), r(t 2), . . . r(t 80)}. • input output • By Mean value Thm (and/or by A/D conversion): Dt r(ti) Dt r(tj) • t(i) t(i-1) t(j-1) R(s) ds, i 1, j 1. Evaluating these integrals over the [ti-1, ti] intervals: r(t 1) c [j=1 5 r(tj) + q 1 r(t 6)], r(t 2) c [(1 - q 1) r(t 6) + j=7 11 r(tj) + q 2 r(t 12)] … 30

© Ivo Penzar Fixed point audio PCM data resampling Downsampling by averaging, cont. :

© Ivo Penzar Fixed point audio PCM data resampling Downsampling by averaging, cont. : • downsample (input r(t 1). . . r(t 441); output r(t 1). . . r(t 80)): r(ti) w 81 -i r(tj(i-1)+1) + j = j(i-1)+2 j(i) c r(tj) + wi r(tj(i)+1)), • downsample (input r(t 1). . . r(t 441); output r(t 1). . . r(t 80)), downsample (input r(t 442). . . r(t 882); output r(t 81). . . r(t 160)), … • Averaging assures conservation of (im)puls: i=1 80 r(ti) 0 10 ms R(s) ds j=1 441 r(tj). 31 i 1.

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, floating point implementation: void

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, floating point implementation: void down. Sample 1(const short in[n 44_1 k. Hz_10 ms], short out[n 8 k. Hz_10 ms]) {bool b. Extra = false; double u = w[n 8 k. Hz_10 ms - 1] * in[0]; for (unsigned i = 0, j = 1, n = 0; n < n 8 k. Hz_10 ms / n 8 k. Hz_5 ms; n++, b. Extra = !b. Extra) for (unsigned k = 0; k < n 8 k. Hz_5 ms; k++, b. Extra = !b. Extra) {double b = in[j++]; for (unsigned m = b. Extra ? 3 : 2; m--; ) b += in[j++]; const double a = in[j++]; *out++ = round(u + w[n 8 k. Hz_10 ms - 1] * (a + b) + w[i] * in[j++]); u = w[n 8 k. Hz_10 ms - i - 1] * a; }} 32

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, floating point implementation, revised

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, floating point implementation, revised (about 14% faster): void down. Sample 2(const short in[n 44_1 k. Hz_10 ms], short out[n 8 k. Hz_10 ms]) {const double &c = w[n 8 k. Hz_10 ms - 1], *ptr. W = w; double u = c * *in++; for (unsigned n = n 8 k. Hz_10 ms / n 8 k. Hz_5 ms; n--; ) for (unsigned k = n 8 k. Hz_5 ms; k--; ) {double b = *in++; b += *in++; if (n + k & 1) b += *in++; const double a = *in++, &wi = *ptr. W++; *out++ = round(u + c * (a + b) + wi * *in++); u = (c - wi) * a; }} 33

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point representation: •

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point representation: • Let c‘, wi’, r(ti)‘, etc. denote fixed point represented/computed values: |wi‘ - wi| < 2 -16. • Maximal numerical error for maximal input samples |r(tj)| r = 215: |r(ti)‘ - r(ti)| 0. 5 + r * maxi [|w 81 -i‘ – w 81 -i| + |wi‘ - wi| + (ji – ji-1 - 1) * |c‘ - c|]. • Calculations for |c‘ - c| < 0. 4 * 2 -16 and maxi […] < 4 * 2 -16 reveal exact bounds: |r(ti)‘ - r(ti)| 2, 0 i 80. • Absolute error up to 2; but only 2 / 215 = 2 -14 relative to the corresponding input samples range r = 215. • Similarly for upsampling, max. error for r(ti-1) = -215, r(ti) = 215 – 1 (or vice versa): |r(tj)‘ - r(tj)| 4, 0 j 441. 34

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point implementation (8

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point implementation (8 x faster than floating point): void down. Sample 3(const short in[n 44_1 k. Hz_10 ms], short out[n 8 k. Hz_10 ms]) {const unsigned short *ptr. W = w; const fix c = w[n 8 k. Hz_10 ms - 1], c 1 = c + 1; register fix u = c 1 * *in++; for (unsigned n = n 8 k. Hz_10 ms / n 8 k. Hz_5 ms; n--; ) for (unsigned k = n 8 k. Hz_5 ms; k--; ) {register long b = *in++; b += *in++; if (n + k & 1) b += *in++; const long a = *in++; const fix wi = *ptr. W++; *out++ = to. Short(u + c * (a + b) + wi * *in++); u = (c 1 - wi) * a; }} 35

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point implementation, cont.

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point implementation, cont. (additionally 30% faster): void down. Sample 4(const short in[n 44_1 k. Hz_10 ms], short out[n 8 k. Hz_10 ms]) {const unsigned short *ptr. W = w; const fix c = w[n 8 k. Hz_10 ms - 1], c 1 = c + 1; register fix u = c 1 * *in++, wi; register long a, b; unsigned k; for (k = n 8 k. Hz_5 ms / 2; k--; ) {b = (in[0] + (long) in[1]) + in[2]; a = in[3]; wi = *ptr. W++; *out++ = to. Short(u + c * (a + b) + wi * in[4]); u = (c 1 - wi) * a; in += 5; b = (in[0] + (long) in[1]) + (in[2] + (long) in[3]); a = in[4]; wi = *ptr. W++; *out++ = to. Short(u + c * (a + b) + wi * in[5]); u = (c 1 - wi) * a; in += 6; } for (k = n 8 k. Hz_5 ms / 2; k--; ) {b = (in[0] + (long) in[1]) + (in[2] + (long) in[3]); a = in[4]; wi = *ptr. W++; *out++ = to. Short(u + c * (a + b) + wi * in[5]); u = (c 1 - wi) * a; in += 6; b = (in[0] + (long) in[1]) + in[2]; a = in[3]; wi = *ptr. W++; *out++ = to. Short(u + c * (a + b) + wi * in[4]); u = (c 1 - wi) * a; in += 5; }} 36

© Ivo Penzar Fixed point audio PCM data resampling 37

© Ivo Penzar Fixed point audio PCM data resampling 37

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point vs. floating

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point vs. floating point: • Total execution time (time-sharing issues), not exclusively processor time: long k, N = 1000000 L; short in[n 44_1 k. Hz_10 ms], out[n 8 k. Hz_10 ms]; for (k = 0; k < n 44_1 k. Hz_10 ms; k++) in[k] = rand(); time_t t. Start = clock(); for (k = 0; k < N; k++) down. Sample 1(in, out); time_t t 1 = clock() - t. Start; t. Start = clock(); for (k = 0; k < N; k++) down. Sample 4(in, out); time_t t 3 = clock() - t. Start; (Profiling can give realistic processor time statistics. ) 38

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point vs. floating

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point vs. floating point, cont. : • Pentium IV 1. 6 GHz, Windows 2 k, MSVC 6. 0: down. Sample 2() about 14% faster than down. Sample 1(), down. Sample 3() 8 x faster than down. Sample 1() (although with powerful floating point co-processor built in), down. Sample 4() about 30% faster than down. Sample 3(). • N = 106 corresponds to 441 mil. input samples (2 h 46 min recording time) takes 0. 75 second for fixed point downsampling! Due to the cache coherence (no memory or file I/O, input constant and output not used or saved). In driver or real-time applications data would be similarly available over the fast DMA channel. 39

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point vs. floating

© Ivo Penzar Fixed point audio PCM data resampling Downsampling, fixed point vs. floating point, cont. : • Theoretical/numerical and statistical analysis: - analysis of calculated output for synthetic input types: constant, linear, periodic or pseudo-random signals; - differences between fixed and floating point implementation output (absolute, relative, normalized and average errors); - subjective or psychological testing (hearing tests) for ´real´ audio data (more important for audio applications than first two!). • Short graphical overview on the following slides: 40

© Ivo Penzar Fixed point audio PCM data resampling 41

© Ivo Penzar Fixed point audio PCM data resampling 41

© Ivo Penzar Fixed point audio PCM data resampling 42

© Ivo Penzar Fixed point audio PCM data resampling 42

© Ivo Penzar Fixed point audio PCM data resampling 43

© Ivo Penzar Fixed point audio PCM data resampling 43

© Ivo Penzar Fixed point audio PCM data resampling Conclusions: • 80/20 (sometimes 90/10)

© Ivo Penzar Fixed point audio PCM data resampling Conclusions: • 80/20 (sometimes 90/10) rule: Only 10 or 20 % of code consumpts 80 or 90 % of run-time. Investigate these hot-spots, optimize their algorithms and/or implementation! Not worth to loose time on detailed optimization for the rest of the code. Leave it generic, better readable and managable (code-reuse purposes, etc. ). • Digital Signal Processing on Embedded platforms: Algorithms must be carefully optimized: - Less powerful processors and less memory (size, weight and price factors), less or no cache (price and energy), energy saving (due to the battery capacity). - Specially important for real-time applications! - Absence of FPU requires Fixed Point arithmetics algorithms and implementation. 44

© Ivo Penzar Fixed point audio PCM data resampling • Word document, Power Point

© Ivo Penzar Fixed point audio PCM data resampling • Word document, Power Point presentation and Visual C project with audio data, available by FTP: URL: ftp. fgmicrotec. hr/Download/Ivo/re. Sampling. zip User: anonymous Password: <e-mail_address> ZIP password: down. Sample 45

© Ivo Penzar Fixed point audio PCM data resampling Thank you for your attention!

© Ivo Penzar Fixed point audio PCM data resampling Thank you for your attention! 46

© Ivo Penzar Fixed point audio PCM data resampling Q&A 47

© Ivo Penzar Fixed point audio PCM data resampling Q&A 47