Hello everyone: [Thank you in advance]
I am contributing a loop with an OSAKA 64 bit sort for the Goertzel algorithm. The algorithm is a frequency detection routine that Chicago based trading advisor Dennis Meyers PhD (Math) asserts compares very favorably, in many cases, to Maximum Entropy method. You can find out more about this method on his site Meyers Analytics. He does, or at least used to have, free papers which compared the two on his site.
I am trying to get away from the Osaka 64 bit sort, and need help. I am not sure how to convert this code to matrix functions and variables. I use this Osaka routine for a long list of custom coded indicators.
First, my question.( I AM NOT A PROGRAMMER...., AND STRUGGLED THROUGH PUTTING THIS CODE TOGETHER, SO IT MAY NOT BE THE MOST EFFICIENT, BUT IT WORKS).
Then, a brief note about the code.
Then, the code.
 Can someone help me with some basic code for converting this loop, sort routine, and extraction of output to the new matrix functions?
I am donating the algorithm to the community. It is mathematically sound. Tomasz, if you believe this is worthwhile, you are free to add this, in whatever form with your programming expertise to Amibroker. It is a pretty sound frequency detection method.

Brief bit about the code. For EACH price bar, it sorts through a range of values from 6 to 200 to detect the frequency with the highest amplitude (strongest frequency). The Osaka table is used to SORT AND EXTRACT the top 5 frequencies and associated amplitudes which can be put together to create an amplitude weighted indicator. (e.g., weighted avg, etc) Please note: if you change this range, the number in the "flattened price" section must be changed. It must always be double or more according to Meyers of the highest number in the range , so for 6 to 200, it must be 400 or more as shown

Code snippet.
//This code checked as error free in AFL. It does not produce a final indicator. At the very end, you could //use these frequencies and their weightings to construct your own weighted average, or any other //weighted indicator
SetBarsRequired(sbrAll,0);
SetChartOptions(0,chartShowArrowschartShowDates);
_N(Title = StrFormat("{{NAME}}  {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
//Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle  ParamStyle("Style")  GetPriceStyle() );
PI = 3.1415926;
Med = (H+L)/2;
k=0;
a = 0;
b = 0;
y = 0;
GA = 0;
AmpL = 0;
//earliest bar you want to take. the more bars, the longer it takes to process them
EarliestBar = 1;
//osaka plugin initialize
osInitialize();
//osaka table create
tableID = osTabCreate();
//create osaka's table's columns
osTabAddColumn("amp", 1, tableID);
osTabAddColumn("GA", 1, tableID);
osTabAddColumn("bar", 1, tableID);
//gets "number_of_order"'s amplitude, use 1 for highest value, 2 for 2nd highest, etc.
//Use after sorting.
//pass MaxNMinN+1 as freqCount
function GetAmplitude(bar_number, number_of_order, freqCount)
{
return (osTabGet(freqCount*(bar_number  EarliestBar)+(number_of_order1), 0, tableID));
}
//gets "number_of_order"'s frequency, use 1 for highest value, 2 for 2nd highest, etc.
//Use after sorting.
//pass MaxNMinN+1 as freqCount
function GetFrequency(bar_number, number_of_order, freqCount)
{
return (osTabGet(freqCount*(bar_number  EarliestBar)+(number_of_order1), 1, tableID));
}
function flattenedprice(input)
{
for(i = EarliestBar; i < BarCount; i++ )
{
if (i < 400 ) //In Meyers MESA vs. Goertzel paper, he indicates we should use at least twice the number of bars as MaxN (400 = 2*200)
{
a = Med[i]; //need specified number of data points to begin calculation
b = Null;
y = Null;
}
else
{
g = 400;
a = Med[i  g];
b = (Med  a)/(g1);
y = Med  (a[i] + b[i]*(i1));
}
}
return y;
}
flt = flattenedprice(Med);
//Plot(flt,"MaxAmpL", colorBlue); // plots end flattened prices
//  I declared these because we will need them more than once
MaxN = 200;
MinN = 6;
// 
rown = 0; //row increment variable
for (n = MinN; n <= MaxN; n++) //used minN and maxN instead of hardcoded values.
{
for(i = EarliestBar; i <= BarCount  1; i++ )
{
switch(i)
{
case 0 :
{
GA[0] = 0;
break;
}
case 1 :
{
GA[1] = 2*cos(2*PI/n)*0  0 + flt[i];
break;
}
default:
{
GA[i] = 2*cos(2*PI/n)*GA[i1]  GA[i2] + flt[i];
// Amibroker uses radians. Goertzel formula uses 2 pi in this formulation
//which equals 6.28318 radians.
break;
}
}
if(i == BarCount  1)
// According to Meyers article "MESA vs. Goertzel", Goertzel is
// calculated after final item in flattened input (i.e., BarCount 1)
{
AmpL = (GA^2 + GA[i1]^2  2*cos(2*PI/n)*GA* GA[i1])/100;
//we have to go through whole AmpL array and add values to Osaka table
for (iteratorSet = EarliestBar; iteratorSet < BarCount; iteratorSet++)
{
//set values into table, 0 column  amplitude, 1 column  frequency, 2 column  barnumber
osTabSetNumber(AmpL[iteratorSet], rown, 0, tableID);
osTabSetNumber(n, rown, 1, tableID);
osTabSetNumber(iteratorSet, rown, 2, tableID);
rown++; //increment table row
}
}
else
{
AmpL = Null;
}
}
//Plot(AmpL ,"AmpL", colorBlue);
//if place Plot function here, I get sinusoids for ALL "n" values
}
//Plot(AmpL ,"AmpL", colorBlue);
// if place Plot function here, I get only final sinusoid for last "n"
//*** few tests and examples below ***//
//sorts table by barnumber (column 2) in ASCENDING ORDER (True)
// AND amplitude (column 0) in DESCENDING ORDER (False).
//see osaka's readme for details.
osTabSort(tableID, 2, True, 0, False);
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentFrequency[i] = GetFrequency(i, 1, MaxNMinN+1); //1st strongest
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentFrequencyA[i] = GetFrequency(i, 2, MaxNMinN+1); //2nd strongest
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentFrequencyB[i] = GetFrequency(i, 3, MaxNMinN+1); //3rd strongest
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentFrequencyC[i] = GetFrequency(i, 4, MaxNMinN+1); //4th strongest, and so on ......
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentFrequencyD[i] = GetFrequency(i, 5, MaxNMinN+1);
}
DomCycle = Median(CurrentFrequency,3);
DomCycleA = Median(CurrentFrequencyA,3);
DomCycleB = Median(CurrentFrequencyB,3);
DomCycleC = Median(CurrentFrequencyC,3);
DomCycleD = Median(CurrentFrequencyD,3);
//************** These loops "extract" amplitudes for "weighting" should user desire to weight items by amplitude *****//
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentAmplitude[i] = GetAmplitude(i, 1, MaxNMinN+1);
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentAmplitudeA[i] = GetAmplitude(i, 2, MaxNMinN+1);
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentAmplitudeB[i] = GetAmplitude(i, 3, MaxNMinN+1);
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentAmplitudeC[i] = GetAmplitude(i, 4, MaxNMinN+1);
}
for(i = EarliestBar; i <= BarCount  1; i++ )
{
CurrentAmplitudeD[i] = GetAmplitude(i, 5, MaxNMinN+1);
}
DomAmplitude = CurrentAmplitude;
DomAmplitudeA = CurrentAmplitudeA;
DomAmplitudeB = CurrentAmplitudeB;
DomAmplitudeC = CurrentAmplitudeC;
DomAmplitudeD = CurrentAmplitudeD;
SumAmplitude = DomAmplitude + DomAmplitudeA + DomAmplitudeB + DomAmplitudeC + DomAmplitudeD;
// use each respective amplitude divided by SumAmplitude times indicator output computed from respective associated cycle value should an amplitude weighted indicator be desired.*/
//always perform osTabDelete after you've done operations with table (usually at the very end of the AFL code)
osTabDelete(tableID);