2 // waveshaping compander, mostly for speech
4 This file is part of a program that implements a Software-Defined Radio.
6 Copyright (C) 2004-2005 by Frank Brickle, AB2KT and Bob McGwier, N4HY
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 The authors can be reached by email at
30 The DTTS Microwave Society
35 #include <wscompand.h>
38 WSCLookup(WSCompander wsc, REAL x) {
40 REAL d = x - (int) x, y, *tbl = wsc->tbl;
41 int i = (int) (x * wsc->npts), end = wsc->nend;
43 y = tbl[i] + d * (tbl[i + 1] - tbl[i]);
52 WSCompand(WSCompander wsc) {
53 int i, n = CXBsize(wsc->buff);
54 for (i = 0; i < n; i++) {
55 COMPLEX val = CXBdata(wsc->buff, i);
57 scl = WSCLookup(wsc, mag);
58 CXBdata(wsc->buff, i) = Cscl(val, scl);
63 WSCReset(WSCompander wsc, REAL fac) {
67 if (fac == 0.0) // just linear
68 for (i = 0; i < wsc->npts; i++)
69 tbl[i] = i / (REAL) wsc->npts;
72 REAL del = fac / wsc->nend,
74 for (i = 0; i < wsc->npts; i++)
75 tbl[i] = (1.0 - exp(i * del)) / scl;
81 // fac < 0: compression
85 newWSCompander(int npts,
90 wsc = (WSCompander) safealloc(1,
91 sizeof(WSCompanderInfo),
92 "WSCompander struct");
95 wsc->tbl = newvec_REAL(npts, "WSCompander table");
96 wsc->buff = newCXB(npts, CXBbase(buff), "WSCompander buff");
102 delWSCompander(WSCompander wsc) {
104 delvec_REAL(wsc->tbl);
106 safefree((char *) wsc);