CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Weird problem with values

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

Weird problem with values
PostPosted: Wed May 02, 2007 9:39 am     Reply with quote

I've recently changed PICs from an F88 to an F886 as I needed hardware I2C and now code I had running fine on the 88 doesn't want to work on the 886.

Basically, I have a function which decodes compressed audio (4 bit ADPCM to 16 bit PCM), but when I encode the audio using a function written in MATLAB, it decodes fine and the PIC produces a nice range of values from -32000 to + 32000, when I use my encoder it produces values that just hover around the same low value the whole time. Now I know this would point to my encoder being the problem, but the two encoders produce incredibly similar values and, when using the F88, it decoded the same files produced by my encoder flawlessly. Are there any changes in the 886's architecture compared to the 88 which would result in something like this happening?

This is the decoding routine:

Code:
signed long ADPCMDecoder(char code) // ADPCM decoding routine
{
   // Set previous values of predicted sample and step size index point
   predsample = prevsample;
    index = previndex;

   step = StepSizeTable[index]; // Find quantiser step size

   // Inverse quantise ADPCM code
   diffq = step >> 3;
   if(code & 4) diffq += step;
   if(code & 2) diffq += step>>1;
   if(code & 1) diffq += step>>2;

   // Add the difference to the predicted sample
   if( code & 8 ) predsample -= diffq;
   else predsample += diffq;


   // Check for and stop overflow of the new predicted sample
   if(predsample > 30000) predsample = 30000;
   else if(predsample < -30000) predsample = -30000;

   index += IndexTable[ code];   // Find new quantiser step size

   // Check for and stop overflow of the new quantizer step size index
   if( index < 0 ) index = 0;
   if( index > 88 ) index = 88;
   

   // Save predicted sample and quantiser step size index point
   prevsample = predsample;
    previndex = index;

   return((long)predsample);   // Return decoded sample
}


I've tried pasting in the values produced by decoding each encoder's audio, but the formatting is terrible unfortunately.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 02, 2007 10:57 am     Reply with quote

Post the declarations of the global variables that are used in your routine.
Also post your compiler version. (NB: We don't remember your compiler
version from one post to the next. Always include it in your posts).
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

PostPosted: Wed May 02, 2007 1:11 pm     Reply with quote

Apologies, compiler version is 4.013 (I think, that's what PCM, PCB and PCH are listed as, IDE is 4.010). MPLAB version is 7.52 if that's relevant, thinking about it I did have to upgrade it from 7.50 so I could program this new PIC.

Global variable declarations are:

Code:
// Index changes
const int IndexTable[16] =
{
   0xff, 0xff, 0xff, 0xff, 2, 4, 6, 8,
   0xff, 0xff, 0xff, 0xff, 2, 4, 6, 8
};


// Quantiser step sizes
const long StepSizeTable[89] =
{
    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};

// ADPCM decoder variables
long step;                      // Quantiser step size
signed int32 predsample;        // Predicted ADPCM sample (decoder output)
signed int32 prevsample;         // Previous sample
signed long diffq;              // Dequantised predicted difference
signed int index;               // Step size table index point
signed int previndex;         // Previous step size table index point


Many thanks,

Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 02, 2007 1:22 pm     Reply with quote

It's probably going to be a problem in 4.013.

If you can combine the code that you posted into a complete test
program that can be dropped into MPLAB and compiled, and run
in the MPLAB simulator (with printf output going to 'UART1' in the
output window), then I can test it. I don't have PCM 4.013.
The closest I have is 4.014. But I can test it with that, and also
with 4.033 and tell you the results. Post a test program that
prints out some easily verifiable results, so that you can tell at
a glance whether the program is running properly or not.
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

PostPosted: Wed May 02, 2007 5:18 pm     Reply with quote

Thanks for your help Smile

I've modified my code so it should hopefully compile first time for you (it does for me) but was unsure as to how to get the printf statements to print to UART1, so haven't changed those... I've put the first 200 "code" values my encoder produces in an array just to test, as they're normally read from an EEPROM. The program should read in a code byte from the array, print its value, decode the upper nibble and print that value, then decode the lower nibble and print that value.



Thanks again for your help,

Andy

Code:
#include <16f886.h>
#include <stdio.h>
#include <stdlib.h>


#fuses INTRC_IO, NOWDT, NOLVP // PIC setup


#use delay(clock = 8000000)                                    // Delay setup
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7, stream = PC)       // RS232 setup
#use I2C(Master, sda = PIN_C4, scl = PIN_C3, FAST = 400000, FORCE_HW)   // I2C setup


// Define registers & bits
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte CCP2CON = 0x1D
#byte CCPR2L = 0x1B
#byte OSCCON = 0x8F
#byte ANSEL = 0x188
#byte T1CON = 0x10
#byte T2CON = 0x12
#byte PR2 = 0x92

// ADPCM code bytes
const char codeTable[200] =
{
   167, 119, 255, 195, 53, 10, 202, 131, 53, 10, 202, 131,
   53, 137, 217, 130, 51, 11, 233, 130, 66, 11, 189, 1, 81,
   10, 187, 2, 112, 26, 186, 131, 67, 11, 233, 130, 66, 11,
   189, 1, 81, 10, 187, 2, 112, 26, 186, 131, 67, 138, 248,
   146, 50, 11, 203, 2, 112, 26, 186, 131, 67, 11, 233, 130,
   66, 11, 189, 1, 81, 10, 187, 2, 112, 26, 186, 131, 67, 11,
   233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112, 26, 186, 131,
   67, 11, 233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112, 26, 186,
   131, 67, 11, 233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112, 26,
   186, 131, 67, 11, 233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112,
   26, 186, 131, 67, 138, 248, 146, 50, 11, 203, 2, 112, 26, 186, 131,
   67, 11, 233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112, 26, 186, 131,
   67, 11, 233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112, 26, 186, 131,
   67, 11, 233, 130, 66, 11, 189, 1, 81, 10, 187, 2, 112, 26, 186, 131
};

// Index changes
const int IndexTable[16] =
{
   0xff, 0xff, 0xff, 0xff, 2, 4, 6, 8,
   0xff, 0xff, 0xff, 0xff, 2, 4, 6, 8
};


// Quantiser step sizes
const long StepSizeTable[89] =
{
    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};

// ADPCM decoder variables
long step;                      // Quantiser step size
signed int32 predsample;        // Predicted ADPCM sample (decoder output)
signed int32 prevsample;         // Previous sample
signed long diffq;              // Dequantised predicted difference
signed int index;               // Step size table index point
signed int previndex;         // Previous step size table index point


signed long ADPCMDecoder(char code) // ADPCM decoding routine
{
   // Set previous values of predicted sample and step size index point
   predsample = prevsample;
    index = previndex;

   step = StepSizeTable[index]; // Find quantiser step size

   // Inverse quantise ADPCM code
   diffq = step >> 3;

   if(code & 4) diffq += step;
   if(code & 2) diffq += (step / 2);
   if(code & 1) diffq += (step / 4);

   // Add the difference to the predicted sample
   if( code & 8 ) predsample -= diffq;
   else predsample += diffq;

   // Check for and stop overflow of the new predicted sample
   if(predsample > 32767) predsample = 32767;
   else if(predsample < -32768) predsample = -32768;

   index += IndexTable[ code];   // Find new quantiser step size

   // Check for and stop overflow of the new quantizer step size index
   if( index < 0 ) index = 0;
   if( index > 88 ) index = 88;

   // Save predicted sample and quantiser step size index point
   prevsample = predsample;
    previndex = index;

   return((long)predsample);   // Return decoded sample
}



void main(void)
{
   int16 i;                      // Counter variable
   char code;                          // ADPCM byte from EEPROM
   signed long sample;                   // Decoded sample


   OSCCON = 0x71;                  // Use internal 8 MHz oscillator
   set_tris_c(0xFC);               // Setup PORTC
   ANSEL = 0x00;                  // Turn off analogue inputs
   ANSEL = 0x00;                    // Turn off analogue inputs    
   T1CON = 0x0;                        // Disable TIMER1
   T2CON = 0x4;                        // Turn on TIMER2 and set pre- and postscalers to 1   
   PR2 = 0xF9;                       // Set PR2 value to 249 to give PWM frequency of 8 kHz     
   CCP2CON = 0xC;                     // Setup CCP2 as PWM
   setup_uart(9600, PC);            // Setup hardware serial port

   
   prevsample = 0;   // Clear ADPCM previous sample
   previndex = 0;   // Clear ADPCM previous index
   index = 0;   
   diffq = 0;
   step = StepSizeTable[index];   

   for(i=0;i<200;i++)
   {
   code = codeTable[i];      
   
   fprintf(PC, "%d\t", code);
   
   sample = ADPCMDecoder((code>>4) & 0x0f);   // Decode upper nibble

   fprintf(PC, "%ld\t", sample);
   
   sample = ADPCMDecoder(code & 0x0f);         // Decode lower nibble

   fprintf(PC, "%ld\n", sample);
   
   }   

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 02, 2007 5:47 pm     Reply with quote

Here's the first part of output when compiled with vs. 4.033.
Does it look correct ? What do you get ?

I changed your printf statements slightly by inserting a field
width of six ("%6ld"), so it would be nicely formatted below.
Code:

   -89       -3        8
   119       38      101
    -1      -35     -328
   -61     -707     -350
    53      -27      436
    10      497      217
   -54     -242     -550
  -125     -606     -249
    53       74      537
    10      598      318
   -54     -141     -449
  -125     -505     -148
    53      175      638
  -119      577      409
   -39     -152     -375
  -126     -443     -135
    51      257      614
    11      660      366
   -23     -132     -336
  -126     -397     -117
    66      342      650
    11      706      349
   -67       26     -437
     1     -376     -208
    81      353      576
    10      644      336
   -69      -56     -413
     2     -367     -157
   112      417      499
    26      722      382
   -70      -49     -329
  -125     -380      -57
    67      322      679
    11      725      431
   -23      -67     -271
  -126     -332      -52
    66      407      715
    11      771      414
   -67       91     -372
     1     -311     -143
    81      418      641
    10      709      401
   -69        9     -348
     2     -302      -92
   112      482      564
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

PostPosted: Thu May 03, 2007 5:38 am     Reply with quote

Thanks for taking the time to do that! Unfortunately it would seem it's not down to the compiler as those are exactly the same values that I get. Could you possibly try it again with these values (produced by the other encoder) and if they give a nice range of values, can you think of anything which could be causing the difference?

Many thanks,

Andy

Code:
// ADPCM code bytes
const char codeTable[200] =
{
   7, 119, 223, 255, 87, 116, 252, 128, 67, 9, 188, 145, 67, 25, 204,
   145, 67, 25, 218, 145, 53, 25, 203, 129, 52, 25, 203, 145, 68, 25,
   203, 145, 82, 25, 189, 128, 66, 25, 188, 145, 67, 25, 204, 145, 67,
   25, 218, 145, 53, 25, 203, 129, 52, 25, 203, 145, 68, 25, 203, 145,
   82, 25, 189, 128, 66, 25, 188, 145, 67, 25, 204, 145, 67, 25, 218,
   145, 53, 25, 203, 129, 52, 25, 203, 145, 68, 25, 203, 145, 82, 25,
   189, 128, 66, 25, 188, 145, 67, 25, 204, 145, 67, 25, 218, 145, 53,
   25, 203, 129, 52, 25, 203, 145, 68, 25, 203, 145, 82, 25, 189, 128,
   66, 25, 188, 145, 67, 25, 204, 145, 67, 25, 218, 145, 53, 25, 203,
   129, 52, 25, 203, 145, 68, 25, 203, 145, 82, 25, 189, 128, 66, 25,
   188, 145, 67, 25, 204, 145, 67, 25, 218, 145, 53, 25, 203, 129, 52,
   25, 203, 145, 68, 25, 203, 145, 82, 25, 189, 128, 66, 25, 188, 145,
   67, 25, 204, 145, 67, 25, 218, 145, 53, 25, 203, 129, 52, 25, 203,
   145, 68, 25, 203, 145
};
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 03, 2007 11:19 am     Reply with quote

Here's results for vs. 4.033 with your latest table:
Code:
  7        0       11
   119       41      104
   -33        4     -195
    -1     -625    -1550
    87      -93     2817
   116     9053    17076
    -4      896   -19916
  -128   -22714   -20171
    67      641    20227
     9    22770    15833
   -68     1118   -16082
  -111   -23019   -16713
    67      487    16674
    25    22980    17247
   -52     1611   -17309
  -111   -24939   -18002
    67      918    18723
    25    25660    19354
   -38    -1668   -15658
  -111   -23288   -16351

If you think there's a bug, then upgrade the compiler. 4.013 is such
an early version, that there could easily be some bug that affects
your code.
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

PostPosted: Thu May 03, 2007 5:37 pm     Reply with quote

Unfortunately I don't think it is a compiler issue as you get the same values I do when it's compiled with 4.033, I just find it really strange that it all went wrong when I changed PICs. I guess if you can't think of anything else then I'll have to change my encoder so it produces exactly the same values as the MATLAB one, though I'm not sure exactly how I'll do that as it seems to be following exactly the same steps...!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 03, 2007 5:41 pm     Reply with quote

I don't know what values you're expecting to get.

What do you consider to be a "good" output ?
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

PostPosted: Fri May 04, 2007 2:59 am     Reply with quote

The ones I got from the second set - a nice range of values in the +/- tens of thousands, rather than hovering around a couple of hundred like the first ones do Smile
andyd



Joined: 08 Mar 2007
Posts: 30

View user's profile Send private message

PostPosted: Fri May 04, 2007 8:45 am     Reply with quote

Also, please could you show me how you modified the printf statement to print into MPLAB so that I don't have to stick the code onto a chip everytime I change something?

Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 04, 2007 9:21 am     Reply with quote

You don't have to change anything in your program to use 'UART1'.
See the instructions in my first post in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&highlight=uart1
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group