working on 8bit turbo

This commit is contained in:
cnlohr 2019-04-27 03:23:07 -04:00
parent 0a056db03d
commit 21398bf6c6
2 changed files with 116 additions and 40 deletions

@ -1 +1 @@
Subproject commit a08b47184b3fcf04172ecc0b6a1aee9c90e5d92d Subproject commit 113e0d1a182cd138510f748abf2854c0e84cfa23

View file

@ -8,6 +8,7 @@
#define MAX_FREQS (24) #define MAX_FREQS (24)
#define OCTAVES (5) #define OCTAVES (5)
#define TARGFREQ 8000.0
/* /*
* The first thought was using an integration map and only operating when we need to, to pull the data out. * The first thought was using an integration map and only operating when we need to, to pull the data out.
@ -29,21 +30,24 @@ NOTE:
Only use 16 bins, lets action table be 16-bits wide. Only use 16 bins, lets action table be 16-bits wide.
*/ */
//These live in RAM.
int16_t running_integral; int16_t running_integral;
int16_t cossindata[MAX_FREQS*OCTAVES*2]; //Contains COS and SIN data. int16_t integral_at[MAX_FREQS*OCTAVES*2];
int32_t cossindata[MAX_FREQS*OCTAVES*2]; //Contains COS and SIN data. (32-bit for now, will be 16-bit)
uint8_t which_octave_for_op[MAX_FREQS]; //counts up, tells you which ocative you are operating on. PUT IN RAM.
#define NR_OF_OPS (4<<OCTAVES)
//Format is:
// 255 = DO NOT OPERATE
// bits 0..3 unfolded octave, i.e. sin/cos are offset by one.
// bit 4 = add or subtract.
uint8_t optable[NR_OF_OPS]; //PUT IN FLASH
#define ACTIONTABLESIZE 256
uint8_t which_octave_for_op[MAX_FREQS]; //counts up, tells you which ocative you are operating on. uint32_t actiontable[ACTIONTABLESIZE]; //PUT IN FLASH
uint8_t highbit_table[2<<OCTAVES]; //PUT IN FLASH uint8_t actiontableplace;
//Format is
#define ACTIONTABLESIZE 512
uint16_t * placeintable;
//Put this in flash.
uint32_t actiontable[ACTIONTABLESIZE];
static int Setup( float * frequencies, int bins ) static int Setup( float * frequencies, int bins )
{ {
@ -52,17 +56,16 @@ static int Setup( float * frequencies, int bins )
for( i = bins-MAX_FREQS; i < bins; i++ ) for( i = bins-MAX_FREQS; i < bins; i++ )
{ {
int topbin = i - (bins-MAX_FREQS); int topbin = i - (bins-MAX_FREQS);
float f = frequencies[i]/2.0; //2x the hits (sin/cos) float f = frequencies[i]/4.0; //4x the hits (sin/cos and we need to do it once for each edge)
float hits_per_table = (float)ACTIONTABLESIZE/f; float hits_per_table = (float)ACTIONTABLESIZE/f;
int dhrpertable = (int)(hits_per_table+.5);//TRICKY: You might think you need to have even number of hits (sin/cos), but you don't! It can flip sin/cos each time through the table! int dhrpertable = (int)(hits_per_table+.5);//TRICKY: You might think you need to have even number of hits (sin/cos), but you don't! It can flip sin/cos each time through the table!
float err = (8000./((float)ACTIONTABLESIZE/dhrpertable) - 8000./f)/(8000./f); float err = (TARGFREQ/((float)ACTIONTABLESIZE/dhrpertable) - (float)TARGFREQ/f)/((float)TARGFREQ/f);
//Perform an op every X samples. How well does this map into units of 1024? //Perform an op every X samples. How well does this map into units of 1024?
printf( "%d %f -> hits per 1024: %f %d (%f error)\n", topbin, f, (float)ACTIONTABLESIZE/f, dhrpertable, err * 100.0 ); printf( "%d %f -> hits per %d: %f %d (%.2f%% error)\n", topbin, f, ACTIONTABLESIZE, (float)ACTIONTABLESIZE/f, dhrpertable, err * 100.0 );
float advance_per_step = dhrpertable/(float)ACTIONTABLESIZE; float advance_per_step = dhrpertable/(float)ACTIONTABLESIZE;
float fvadv = 0.0; float fvadv = 0.0;
int j; int j;
int actions = 0;
int countset = 0; int countset = 0;
//XXX TODO Tricky: We need to start fadv off at such a place that there won't be a hicchup when going back around to 0. //XXX TODO Tricky: We need to start fadv off at such a place that there won't be a hicchup when going back around to 0.
@ -80,38 +83,110 @@ static int Setup( float * frequencies, int bins )
printf( " countset: %d\n", countset ); printf( " countset: %d\n", countset );
} }
for( i = 0; i < (1<<OCTAVES); i++ ) int phaseinop[OCTAVES] = { 0 };
for( i = 0; i < NR_OF_OPS; i++ )
{ {
int longestzeroes = 0; int longestzeroes = 0;
for( longestzeroes = 0; longestzeroes < 255 && ( ((i >> longestzeroes) & 1) == 0 ); longestzeroes++ ); int val = i & ((1<<OCTAVES)-1);
for( longestzeroes = 0; longestzeroes < 255 && ( ((val >> longestzeroes) & 1) == 0 ); longestzeroes++ );
//longestzeroes goes: 255, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, ... //longestzeroes goes: 255, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, ...
//This isn't great, because we need to also know whether we are attacking the SIN side or the COS side. //This isn't great, because we need to also know whether we are attacking the SIN side or the COS side, and if it's + or -.
highbit_table[i] = longestzeroes; //We can actually decide that out.
if( longestzeroes == 255 )
{
//This is a nop. Emit a nop.
optable[i] = longestzeroes;
}
else
{
int iop = phaseinop[longestzeroes]++;
optable[i] = (longestzeroes<<1) | (iop & 1);
if( iop & 2 ) optable[i] |= 1<<4;
//printf( " %d %d\n", iop, val );
}
//printf( "HBT: %d = %d\n", i, optable[i] );
} }
//Repeat the highbit table in the second half.
//XXX PICK UP HERE
//Encode into highbit_table which cell is being operated on
//Also, do the * MAX_FREQS here. That will
return 0;
placeintable = actiontable;
// for( i = 0; i < ACTIONTABLESIZE; i++ ) printf( "%08x\n", actiontable[i] );
} }
#if 0
int16_t running_integral; int16_t running_integral;
int16_t cossindata[MAX_FREQS*OCTAVES*2]; int16_t integral_at[MAX_FREQS*OCTAVES];
uint8_t which_octave_for_op[MAX_FREQS]; //counts up, tells you which ocative you are operating on. int16_t cossindata[MAX_FREQS*OCTAVES*2]; //Contains COS and SIN data.
uint16_t * placeintable; uint8_t which_octave_for_op[MAX_FREQS]; //counts up, tells you which ocative you are operating on. PUT IN RAM.
//Put this in flash. #define NR_OF_OPS (4<<OCTAVES)
uint32_t actiontable[ACTIONTABLESIZE]; //Format is:
// 255 = DO NOT OPERATE
// bits 0..3 unfolded octave, i.e. sin/cos are offset by one.
// bit 4 = add or subtract.
uint8_t optable[NR_OF_OPS]; //PUT IN FLASH
#define ACTIONTABLESIZE 256
uint32_t actiontable[ACTIONTABLESIZE]; //PUT IN FLASH
//Format is
#endif
void Turbo8BitRun( int8_t adcval ) void Turbo8BitRun( int8_t adcval )
{ {
running_integral += adcval;
#define dprintf( ... )
uint32_t action = actiontable[actiontableplace++];
int n;
dprintf( "%4d ", actiontableplace );
for( n = 0; n < MAX_FREQS; n++ )
{
if( action & (1<<n) )
{
int ao = which_octave_for_op[n];
int op = optable[ao];
ao++;
if( ao >= NR_OF_OPS ) ao = 0;
which_octave_for_op[n] = ao;
if( op == 255 )
{
dprintf( "*" ); //NOP
}
else
{
int octaveplace = op & 0xf;
int idx = (octaveplace>>1) * MAX_FREQS * 2 + n * (octaveplace&1)*2;
int16_t diff;
if( op & 0x10 ) //ADD
{
diff = integral_at[idx>>1] - running_integral;
dprintf( "%c", 'a' + octaveplace );
}
else //SUBTRACT
{
diff = running_integral - integral_at[idx>>1];
dprintf( "%c", 'A' + octaveplace );
}
integral_at[idx>>1] = running_integral;
printf( "%d\n", diff );
//dprintf( "%d\n", idx );
cossindata[idx] += diff;
cossindata[idx] -= cossindata[idx] >> 8;
}
}
else
{
dprintf( " " );
}
}
dprintf( "\n" );
#if 0
uint32_t actions = *(placeintable++); uint32_t actions = *(placeintable++);
if( placeintable == &actiontable[ACTIONTABLESIZE] ) placeintable = actiontable; if( placeintable == &actiontable[ACTIONTABLESIZE] ) placeintable = actiontable;
int b; int b;
@ -131,6 +206,7 @@ void Turbo8BitRun( int8_t adcval )
//if( b == 0 ) printf( "%d\n", whichoctave ); //if( b == 0 ) printf( "%d\n", whichoctave );
//XXX TODO Optimization: Use a table, since octavebit can only be 0...31. //XXX TODO Optimization: Use a table, since octavebit can only be 0...31.
} }
#endif
} }
@ -148,20 +224,20 @@ void DoDFT8BitTurbo( float * outbins, float * frequencies, int bins, const float
} }
last_place = place_in_data_buffer; last_place = place_in_data_buffer;
#if 0 #if 1
for( i = 0; i < bins; i++ ) for( i = 0; i < bins; i++ )
{ {
outbins[i] = 0; outbins[i] = 0;
} }
for( i = 0; i < MAX_FREQS; i++ ) for( i = 0; i < MAX_FREQS; i++ )
{ {
int iss = nd[i].sinm>>8; int iss = 0;//cossindata[i*2+0]>>8;
int isc = nd[i].cosm>>8; int isc = 0;//cossindata[i*2+1]>>8;
int mux = iss * iss + isc * isc; int mux = iss * iss + isc * isc;
if( mux == 0 ) mux = 1; if( mux == 0 ) mux = 1;
if( i == 0 ) if( i == 0 )
printf( "MUX: %d %d\n", isc, iss ); //printf( "MUX: %d %d = %d\n", isc, iss, mux );
outbins[i+MAX_FREQS] = sqrt(mux)/200.0; outbins[i+MAX_FREQS] = sqrt(mux);///200.0;
} }
#endif #endif
} }