Shop OBEX P1 Docs P2 Docs Learn Events
Help converting Psuedo to spin — Parallax Forums

Help converting Psuedo to spin

bambinobambino Posts: 789
edited 2010-12-15 15:53 in Propeller 1
Hi Guys, Been awile.
Below is some psuedo code i was lucky enough to get, as the equation used calculas notation(greek to me)
It appears to be easily converted to spin, but I thought I would post my first question now and just bump this thread latter when I get stumped again.

Right now the only thing that jumps out at me is the Power statement towards the bottom. Its not declared as a variable so I am assumeing this is some sort of operand. Can someone shed some light on that.

// GLOBAL VARIABLES
var
// Data Acquisition Information
SampleFrequency: integer; // Data acquisition rate, samples/second
nSamples : integer; // Number of acquired data samples
// Input Data
AccelData: array [0..nSamples] of real; // Array of acceleration data in g units
// Outputs
HICmax : real; // HIC score
HICinterval : real; // HIC interval
// HIC CALCULATION PROCEDURE
procedure HIC_Calculation;
// LOCAL VARIABLES
var
// Intermediate Results
integral : array [0..nSamples-1] of real; // HIC Integral Values
iHIC0,iHIC1 : integer; // HIC interval boundaries
HIC : real; // Intermediate HIC result
// Counters
i,j : integer;
begin
// Initialise results
iHIC0 := 0;
iHIC1 := 0;
HICmax:=-1.0;
// Calculate Integral
integral [0]:=0.0;
for i:=1 to nSamples do integral : =integral [i-1] +(AccelData +AccelData [i-1])/2;
// Scan all possible HIC intervals for maximum score
for i := 0 to nSamples-1 do
for j := i+1 to nSamples do
begin
HIC:=(integral [j]-integral )/(j-i);
if HIC>0.0
then HIC:=Power (HIC,2.5) 'This is the first statement I need help understanding
else HIC:=0.0;
HIC:=HIC*(j-i)/SampleFrequency;
if HIC>HICmax then
begin
HICmax:=HIC;
iHIC0:=i;
iHIC1:=j;
end;
end;
// Calculate the HIC interval
HICinterval := (IHIC1-IHIC0)/SampleFrequency;
end;
end.

Comments

  • bambinobambino Posts: 789
    edited 2010-12-13 10:41
    I lost the formatting, if someone will tell me how the code function works again, I'll repost.
  • rod1963rod1963 Posts: 752
    edited 2010-12-13 10:47
    It reads like Pascal and is very straightforward. Power looks to be a undefined function.
  • HShankoHShanko Posts: 402
    edited 2010-12-13 10:49
    {code]
    your source lines here
    [/code}

    works for me. (NOTE: use square brackets instead of the '{ or }' ones.) Is there a simpler form?
  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2010-12-13 11:00
    Power(HIC,2.5) means take HIC to the power of 2.5. Or HIC2.5.

    If you use the Float32 object, it has a Pow() function that will do this.
  • bambinobambino Posts: 789
    edited 2010-12-13 11:12
    Many thanks Roy, I assumed as much, but it's good to have others insight.
    Yes, the code looks pretty straight forward. It will not get a lot of attention from me and thus this thread will get buried. But when I get into the guts of it and have aproblem I'll bump it up again.

    Pascal.....The article actually said it was from c++, but the := notation and the "do" statements didn't seem to fit. But I have limited languages to draw experience from.
  • rod1963rod1963 Posts: 752
    edited 2010-12-13 11:48
    A lot of Pseudo code is written like Pascal because of it's readability. Heck the example you have is good enough to do a real easy port to Turbo Pascal or even Oberon(Pascal's successor) without much effort.
  • ericballericball Posts: 774
    edited 2010-12-13 12:31
    I can't promise this is 100% correct, but Spin Tool compiles it.
    OBJ
      f : "Float32"
    CON
      nSamples = 16                 ' Number of acquired data samples
    VAR
    ' Data Acquisition Information
      LONG  SampleFrequency         ' Data acquisition rate, samples/second
    ' Input Data
      LONG  AccelData[nSamples+1]   ' Array of acceleration data in g units
    ' Outputs
      LONG  HICmax                  ' HIC score
      LONG  HICinterval             ' HIC interval
    ' Intermediate Results
      LONG  integral[nSamples]      ' HIC Integral Values
    PUB HIC_Calculation | iHIC0, iHIC1, HIC, i, j ' HIC interval boundaries, Intermediate HIC result, Counters
      f.start
    ' Initialise results
      iHIC0 := 0
      iHIC1 := 0
      HICmax := -1.0
    ' Calculate Integral
      integral[0] := 0.0
      repeat i from 1 to nSamples
        integral[i] := f.FAdd( integral[i-1], f.FDiv( f.FAdd(AccelData[i],AccelData[i-1]), 2.0 ) )
    ' Scan all possible HIC intervals for maximum score
      repeat i from 0 to nSamples-1
        repeat j from i+1 to nSamples
          HIC := f.FDiv( f.FSub( integral[j], integral[i] ), f.FFloat( j-i ) )
          if f.FCMP( HIC, 0.0 ) > 0
            HIC := f.Pow( HIC, 2.5 )
          else
            HIC := 0.0
          HIC := f.FDiv( f.fMul( HIC, f.FFloat( j-i ) ), f.FFloat( SampleFrequency ) )
          if f.FCMP( HIC, HICmax ) > 0
            HICmax := HIC
            iHIC0 := i
            iHIC1 := j
    ' Calculate the HIC interval
      HICinterval := f.FDiv ( f.FFloat( IHIC1-IHIC0), f.FFloat( SampleFrequency ) )
    
  • bambinobambino Posts: 789
    edited 2010-12-13 18:02
    Two hours soldering, eat dinner , feed the dog and you guys have it converted for me!!!!

    Thanks for all the input. I don't even have the hardware done that this little guy will play in. I need to be able to understand what's going on here as well. At the peak of my mathematical prime we are Talking Algebra 2. Integral equations in calculus, like this, need to seep into my memory very slowly!

    Thank you very much!
  • Heater.Heater. Posts: 21,230
    edited 2010-12-13 21:43
    Out of curiosity can you show us the original equation?
  • bambinobambino Posts: 789
    edited 2010-12-14 06:55
    d04f6bc9fad591b72b98336e73d59dc1.png
    Untitled.png




    506 x 392 - 6K
  • Heater.Heater. Posts: 21,230
    edited 2010-12-14 11:14
    Ah, so you are calculating the famous Head Injury Criteria (HIC). As per this document: http://www.health.uottawa.ca/biomech/courses/apa6903/CrashTests%20and%20the%20HIC.pdf
    Now, if the maths is all Greek to you how are you going to tell if your final code is bug free and producing the correct results?
  • User NameUser Name Posts: 1,451
    edited 2010-12-14 11:42
    Heater. wrote: »
    Now, if the maths is all Greek to you how are you going to tell if your final code is bug free and producing the correct results?

    Volunteer numskulls and a large hammer?
  • bambinobambino Posts: 789
    edited 2010-12-14 18:36
    @ Heater,
    I will have a commercial unit available with which to compare samples.
    The link to your PDF would not open for me , but I have found that most documents on the web refer to automobile testing. The ASTM. I quoted from is actually for the sports industry. Without soundly Like the HIC test are BS, let me state that this big equation boils down to a boolean answer. Pass or Fail. With the threshold being a bloated number that suggests someone survived or Everyone died!
    With so many numbers to consider, I presume verifying the accuracy will pan out easily against the Control unit. The results will either jive with the control, or not.
    The real scare here: Is figuring out where the math went wrong went it doesn't sync With the control.
    Now that Will be a good time to have a hammer available!!!
  • Heater.Heater. Posts: 21,230
    edited 2010-12-14 23:11
    bambino,

    OK I fixed the link above to the HIC PDF.
    ...let me state that this big equation boils down to a boolean answer. Pass or Fail. With the threshold being a bloated number that suggests someone survived or Everyone died!

    This worries me, be aware that the HIC is not and probably never was intended to be used in such a way and is mostly a very vague hand waving guestimate. From that PDF:

    "Just as with students' examination marks, the HIC does not provide an interval scale that enables comparisons. The HIC cannot make any statement about kind and severity of eventual injuries but gives an initial orientation for an estimation of the general injury risk."

    "The complicate formula, the huge computer used for the calculation and the exact numerical result feign a meaningfulness that that crash-modelling using the HIC does not have."

    "Reality was simplified very strongly."

    "Probably the formula defining the HIC was created by accident ....got more or less satisfactory results , and this formulation became the norm."

    "...such thing "smell" like a rule of thumb"


    Given all that it probably does not matter much about how accurately you calculate it:)
  • bambinobambino Posts: 789
    edited 2010-12-15 07:58
    Given all that it probably does not matter much about how accurately you calculate it

    From where I'm sitting no! But it will be expected to come within a certain percent of what the control or Triaxe says it is. Most folks won't to know what the G max is and are happy with that. But some of our customers are wanting the HIC as well!
    The HIC in no accident though. It was derived from the severity Index which has a similar equation. They are both covered in the testing standard and the limitations of the HIC Are pointed out! So don't be too scared! Anytime I've seen it used was to weed out poor designs! Which is to say "If a play surface won't pass the HIC, Then don't bother to pay for more extensive testing. "
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-12-15 08:30
    In your equation there are only two bits that might be a bit complicated, the integral and the power of 2.5. The integral just means the area under the graph of 'a' between times t1 and t2. There are simple ways to calculate an integral if you know 'a' for lots of times between t1 and t2. The more times you have the more accurate, a simple method is to consider the area as a lot of trapeziums or even rectangles, take a look at this: http://demonstrations.wolfram.com/ComparingBasicNumericalIntegrationMethods/

    The other thing is the power of 2.5, well 2.5 is the same as 5/2 (five halves) and if you raise something to 5/2 you actually raise it to the power of 5 and then square root it:

    http://images.planetmath.org:8080/cache/objects/7340/js/img1.png

    I hope that helps a little.

    Graham
  • bambinobambino Posts: 789
    edited 2010-12-15 15:53
    Graham, that is wonderful! Definitely let some light in on what's happening. Maybe even showed me how to speed up the calculations inside the prop!
Sign In or Register to comment.