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 support@ccsinfo.com

18F2480 and interpolation ?

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



Joined: 22 Mar 2012
Posts: 70
Location: France (Paris)

View user's profile Send private message

18F2480 and interpolation ?
PostPosted: Tue Jun 05, 2012 9:09 am     Reply with quote

Hi

Looking to translate the code below for CCS compiler Ver 4.132.
Code:

int32 Regime_Sp[10]={500,1000,1500,1750,2000,2500,3000,3500,4000,4500};//Tr/mn
int32 Couple_Sp[10]={60,125,225,240,235,227,215,210,185,160};//N/m

 public static double interp1(double X, double[] Xref, double[] Table)
        {
            int longXref = Xref.Length;
            int j = 0; // Index de position en X dans Table
            while ((j < longXref - 1) & (X > Xref[j]))
            {
                j++;
            }
            double x1, x2, y1, y2, t, resultat;
            // Calcul de x1 et y1
            x1 = Xref[j];
            y1 = Table[j];
            // Calcul de y2
            if ((j < longXref - 1) & (j > 0))
            {
                x2 = Xref[j + 1];
                y2 = Table[j + 1];
            }
            else
            {
                y2 = y1;
                x2 = x1;
            }

            if (x2 == x1)
            {
                t = 0;
            }
            else
            {
                t = (X - x1) / (x2 - x1);
            }



            resultat = y1 + (y2 - y1) * t;
            return resultat;
        }

Thanks in advance for your help Very Happy
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jun 05, 2012 10:05 am     Reply with quote

If the code is intended for PCH compiler (PIC18), double (64 bit floating point) isn't supported. PIC24 PCD compiler however does.

Xref.Length seems to be a private non-standard C syntax extension. Referring to standard C, it would be necessary to provide the array size in a separate function parameter.

You should better show a complete example application including a function call. Which problems did you observe?
Cogitum



Joined: 22 Mar 2012
Posts: 70
Location: France (Paris)

View user's profile Send private message

18F2480 and interpolation next
PostPosted: Tue Jun 05, 2012 11:57 pm     Reply with quote

HI,

Thanks for your quick answer.
This code is made with C #. 64 Bits are not necessary for my application.
I'm just looking to translate this code for CCS compiler or looking for CCS example using interpolation calculation.

Regards
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:25 am     Reply with quote

OK then, go ahead and translate it. Its simple; just linear interpolation: the sort of thing I've done over and over again.

RF Developer
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 4:00 am     Reply with quote

Quote:
This code is made with C #. 64 Bits are not necessary for my application.
I'm just looking to translate this code for CCS compiler or looking for CCS example using interpolation calculation.


Ah, C#. I'm not familiar with this modern stuff. At first sight, you would write float for double, presumed you want to use floating point arithmetic. And supply length as an additional function parameter. Hopefully, that's it.
Cogitum



Joined: 22 Mar 2012
Posts: 70
Location: France (Paris)

View user's profile Send private message

18F2480 and interpolation
PostPosted: Thu Jun 07, 2012 10:41 am     Reply with quote

Hi,

This code RUN but the result is wrong !
Code:

int32 interpolation(int32 X,int32 Xref[10],int32 Table[10])
{
int32 x1, x2, y1, y2, t,t1,t2, resultat;
int longXref = 10;
int j = 0;
while ((j < longXref - 1) && (X > Xref[j]))
   {
   j++;
   }
  // Calcul de x1 et y1
  x1 = Xref[j];
  y1 = Table[j];
  // Calcul de y2
  if ((j < longXref - 1) && (j > 0))
            {
                x2 = Xref[j + 1];
                y2 = Table[j + 1];
            }
            else
            {
                y2 = y1;
                x2 = x1;
            }
            if (x2 == x1)
            {
                t = 0;
            }
            else
            {
             t = (X -x1)/(x2 - x1); // normal > -1,2                   
            }
            resultat = y1 + (y2 - y1) * t; // Normal 207
            return resultat;
        }

note: X= 1200, x1= 1500, X2= 1750, Y1= 225, Y2= 240

resultat = 10693 !! manual calculation is 207.

How to solve this problem with CCS ?

Thanks in advance
gaugeguy



Joined: 05 Apr 2011
Posts: 296

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 12:01 pm     Reply with quote

An unsigned int can only hold positive integers. No negatives and no fractions.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 2:34 pm     Reply with quote

I previously suggested to use float data type for the calculations. With integer, you have the problem, that t is truncated to an integer value, which can't give a correct result. Another problem, already mentioned by gaugeguy, in contrast to the C standard, CCS C is understanding int as unsigned int, you need to write signed int explicitely if you mean it.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 4:23 pm     Reply with quote

Three issues:-

(1) You're extrapolating not interpolating. You example X is outside the range of x1 to x2.
(2) As already noted CCS treats ints as unsigned. This is causing most of your error. X is outside the range x1 to x2 on the negative side. Therefore (X -x1) wraps round to a huge positive value instead of a small negative one.
(3) There's no need to move to floats. Simply change the order in which you perform the calculations. Again as already noted you are incurring a massive rounding off error with your calculation of t. Perform the divisions last. As it stands you're multiplying your rounding off error by (y2-y1).

Mike
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 10:51 pm     Reply with quote

Quote:
You're extrapolating not interpolating.

Yes. The algorithm should work for both. The problem is caused because the code selects an index j with x < x1 and x <x 2,
but it should select for x1 < x < x2.

Quote:
There's no need to move to floats.

If not float, fixed point calculation has to be used. Although the signed/unsigned problem causes
most of the error in your example, the code as shown is only meaningful for float.
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