Skip to content

Commit fe2a963

Browse files
committed
added microcontroller example
1 parent 3c1c997 commit fe2a963

4 files changed

Lines changed: 1044 additions & 66154 deletions

File tree

companders.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535

3636
DIO_s8 DIO_LinearToALaw(DIO_s16 sample)
3737
{
38-
const int cClip = 32635;
39-
const static char LogTable[128] =
38+
const DIO_s16 cClip = 32635;
39+
const static DIO_s8 LogTable[128] =
4040
{
4141
1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
4242
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
4343
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
4444
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
4545
};
4646

47-
int sign, exponent, mantissa;
48-
char compandedValue;
47+
DIO_s32 sign, exponent, mantissa;
48+
DIO_s8 compandedValue;
4949
sample = (sample ==-32768) ? -32767 : sample;
5050
sign = ((~sample) >> 8) & 0x80;
5151
if (!sign)
@@ -68,7 +68,7 @@ DIO_s8 DIO_LinearToALaw(DIO_s16 sample)
6868

6969
DIO_s16 DIO_ALawToLinear(DIO_s8 aLawByte)
7070
{
71-
const static short ALawDecompTable[256]={
71+
const static DIO_s16 ALawDecompTable[256]={
7272
5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
7373
7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
7474
2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
@@ -101,15 +101,15 @@ DIO_s16 DIO_ALawToLinear(DIO_s8 aLawByte)
101101
-1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
102102
-688, -656, -752, -720, -560, -528, -624, -592,
103103
-944, -912, -1008, -976, -816, -784, -880, -848};
104-
int addr = ((int)aLawByte)+128; // done for compilers with poor expr type enforcement
104+
DIO_s16 addr = ((DIO_s16)aLawByte)+128; // done for compilers with poor expr type enforcement
105105
return ALawDecompTable[addr];
106106
}
107107

108108
// see companders.h
109109
// fixed-radix IIR averager implementation supporting arbitrarily chosen windows
110110
DIO_s32 DIO_IIRavgFR (DIO_s32 prevAvg, DIO_u16 windowLen, DIO_s16 newSample, DIO_u8 radix)
111111
{
112-
int iirAvg=0;
112+
DIO_s32 iirAvg=0;
113113
iirAvg = ((prevAvg * (windowLen-1)) + (DIO_I2FR(newSample,radix)))/windowLen;
114114
return iirAvg;
115115
}
@@ -119,7 +119,7 @@ DIO_s32 DIO_IIRavgFR (DIO_s32 prevAvg, DIO_u16 windowLen, DIO_s16 newSample,
119119
// and only shift operations for cpu efficiency
120120
DIO_s32 DIO_IIRavgPower2FR (DIO_s32 prevAvg, DIO_u8 windowLenInBits, DIO_s16 newSample, DIO_u8 radix)
121121
{
122-
int iirAvg=0;
122+
DIO_s32 iirAvg=0;
123123
iirAvg = (((prevAvg<<windowLenInBits)-prevAvg) + (DIO_I2FR(newSample,radix)))>>windowLenInBits;
124124
return iirAvg;
125125
}

compandit.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ int main (int argc, char *argv[])
4646
printf("for more info see the accompanying compand.txt\n");
4747
printf("\n");
4848

49+
//uncomment to
50+
/*
4951
//show how linear-->alaw-->linear-->alaw progression / quantization error works
50-
5152
{
5253
char alaw=0,alaw2;
5354
short rev=0;
@@ -68,32 +69,58 @@ int main (int argc, char *argv[])
6869
//window length of 8, using fractional precision of 4 bits
6970
{
7071
int a3=0, a4=0;
71-
unsigned char rad=4;
72+
unsigned char rad=4; //4 bits fixed-radix fractional precision
7273
printf(" index wave IIRav(i) IIRav(f) IIRavP2(i) IIRavP2(f)\n");
7374
for (i=0; i < 300; i++)
7475
{
7576
j=(i&0x3f)-20; // triangle wave with range -20 to + 43
76-
a3= DIO_IIRavgFR(a3,8,j,rad); //4 bits fixed-radix fractional precision
77-
a4= DIO_IIRavgPower2FR(a4,3,j,rad); //4 bits fixed-radix fractional precision
77+
a3= DIO_IIRavgFR(a3,8,j,rad);
78+
a4= DIO_IIRavgPower2FR(a4,3,j,rad);
7879
printf("%6d %6d %6d %9.4f %6d %9.4f \n",i,j,
7980
DIO_FR2I(a3,rad),DIO_FR2D(a3,rad) ,DIO_FR2I(a4,rad),DIO_FR2D(a4,rad));
8081
}
8182
}
8283
//window length of 64
8384
{
8485
int a3=0, a4=0;
85-
unsigned char rad=6;
86+
unsigned char rad=6; //rad is the number of bits of precision
8687
printf(" index wave IIRav(i) IIRav(f) IIRavP2(i) IIRavP2(f)\n");
8788
for (i=0; i < 300; i++)
8889
{
8990
j=(i&0x3f)-20; // triangle wave with range -20 to + 43
90-
a3= DIO_IIRavgFR(a3,64,j,rad); //5 bits fixed-radix fractional precision
91-
a4= DIO_IIRavgPower2FR(a4,6,j,rad); //5 bits fixed-radix fractional precision
91+
a3= DIO_IIRavgFR(a3,64,j,rad);
92+
a4= DIO_IIRavgPower2FR(a4,6,j,rad);
9293
printf("%6d %6d %6d %9.4f %6d %9.4f \n",i,j,
9394
DIO_FR2I(a3,rad),DIO_FR2D(a3,rad) ,DIO_FR2I(a4,rad),DIO_FR2D(a4,rad));
9495
}
9596
}
96-
//done...
97+
*/
98+
//Typical microcontroller application. See readme-companders.txt
99+
// the input here simulates an A/D which has a range 0..3.3V mapped as 12 bits (0..4095)
100+
// with a DC bias of 1.55V ==> (1.55/3.3)*4095 counts = 1923 counts
101+
102+
//now window length of 256 is used for rejecting the resistor bias. at 8KHz this window
103+
// would be approx 8000/256 = 31.25 Hz (not quite but explaining Z xforms is beyond what
104+
// can be explained in this simple space.
105+
//we seed the DC average at 3.3/2 = 1.65V (we guess its in the middle) and let the long window
106+
//length hone in on the correct value. (1.65V/3.3V) *4095 = 2048 counts
107+
{
108+
int actualDCbias =1923;
109+
int calculatedDCbias =2048; //this is our estimate as outlined above
110+
unsigned char windowLenPow2InBits = 8; // 8 bit long window = 256 sample long window
111+
unsigned char rad=6; //rad is the number of bits of precision
112+
113+
calculatedDCbias = DIO_I2FR(calculatedDCbias,rad);
114+
printf(" index wave actDCbias calcDCbias calcDCbias(f) alaw\n");
115+
for (i=0; i < 1000; i++) // if 8000 hz sample rate this represents the number of samples captured
116+
{
117+
j=(((i&0x3f)<<1)-63)+1923; // triangle wave w range 0..127 with a bias set at actualDCbias
118+
calculatedDCbias = DIO_IIRavgPower2FR(calculatedDCbias,windowLenPow2InBits,j,rad);
119+
printf("%6d %6d %6d %6d %9.4f %3d\n",i,j,actualDCbias,
120+
DIO_FR2I(calculatedDCbias,rad),DIO_FR2D(calculatedDCbias,rad),
121+
(int)(DIO_LinearToALaw(j-DIO_FR2I(calculatedDCbias,rad)) ));
122+
}
123+
}
97124
printf("\n");
98125
return ret_val;
99126
}

0 commit comments

Comments
 (0)