Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Really tricky algorithm question

Posted on 2004-04-05
18
Medium Priority
?
393 Views
Last Modified: 2008-02-20
My job unfortunatly just got rid of some really talented programmers, and i am left to do this. We are working with a new system we are developing which takes an input (stock quotes) from a database (could even be excel file) and returns a specific answers based on the following function:
The function to be evaluated is this:  P.S. the 'E' means Summation (Sigma).

         N-1                    L-1         M-1
y(n) = E a(k) x(n-k) +     E     +     E  b(l,m) x(n-1)  x(n-m)
        K=0                     I=0        m=0


Now, i have no idea why the algorithm has to be based on the above function, (remember, i am taking over a half completed job and im not even a real programmer) but this is what was in the old programmers notes. I need some sort of java or c++ code to evaluate the above function. I know that we are going to be working with 200 stock quotes at a time, so let x(n) be 200 values, choosing some variable (a and b) to be real constant coefficiants.
0
Comment
Question by:enray
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 500 total points
ID: 10762434
Assuming there are values in arrays

public float y(n)
{
t1=0;
for(int k=0;k<=N-1;k++)
{
   t1=t1+a[k]*x[n-k];
}

for(int l=0;l<=L-1;l++)
for(int m=0;l<=M-1;m++)
{
   t1=t1+ b[l][m]*x[n-1]*x[n-m];
}
}

System.out.println("y100="+y(100));

0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10762440
I assumed there are already values loaded in arrays a,x,b
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10763297
What's the name of the formula?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:enray
ID: 10767450
Im not sure what the name is, remember i am reading a programmers notes, but i looked online and it seems like part of an Finitie Impulse Response Filter.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10767938
In that case, you would want to have a look at the following file:
http://www.dspguru.com/sw/lib/fir_algs_1-0.c

It is an implementation in C and the fir_basic() function is pretty straight forward to re-implement in Java, like so:

public class FIR {

/**   These functions use most or all of the following input parameters:
*
*       input    - the input sample data
*       ntaps    - the number of taps in the filter  
*       h[]      - the FIR coefficient array
*       z[]      - the FIR delay line array
*/

/****************************************************************************
* fir_basic: Does the basic FIR algorithm: store input sample, calculate      
* output sample, move delay line                                          
*****************************************************************************/
public static double firBasic(double input, int ntaps, doubleh[], double z[])      
{
    int ii;
    double accum;
   
    /* store input at the beginning of the delay line */
    z[0] = input;

    /* calc FIR */
    accum = 0;
    for (ii = 0; ii < ntaps; ii++) {
        accum += h[ii] * z[ii];
    }

    /* shift delay line */
    for (ii = ntaps - 2; ii >= 0; ii--) {
        z[ii + 1] = z[ii];
    }

    return accum;
}
}

You then call the method by: FIR.firBasic(startValue, numTaps, doubleArray1, doubleArray2);

Replace "startValue", "numTaps", "doubleArray1", "doubleArray2" with the appropriate variables.
0
 

Expert Comment

by:deuel47
ID: 10767973
Well here's an idea, treat each function as a function, in other words convert each function in a java function.

Random example not based on anything:
a(k)
 public float a(k)
{
   \\does some work
    returns n;
}

do that for each function, then make a function to handle the entire summation.

You probably want to research the function to understand what it does in order to code it
 
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10768430
Deuel47, please keep in mind that enray is a self-proclaimed non-programmer.
0
 

Expert Comment

by:deuel47
ID: 10769581
yeah your right orangehead....sorry about that
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10769676
no worries! :-)
0
 

Author Comment

by:enray
ID: 10788901
Thanks guys... let me try out these code examples, then i will reward points. Thank you all so much!
0
 

Author Comment

by:enray
ID: 10789337
yenkateshwarr, what does t1 refer to? Also, which is the array which will hold the initial values loaded from the external file??
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10789642
I doubt that yenkateshwarr's example implements the actual algorithm. The source file link that I provided contains implementations of the algorithm with a bunch of variations.
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10793312
t1 is just a temporary double/float value...
this value is returned after the final calcuation is done
0
 

Author Comment

by:enray
ID: 10834300
Orangeheaed911, what are the nTaps???  
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10834698
It's the number of applications of the filter. I.e. the size of the arrays.
0
 

Author Comment

by:enray
ID: 10853858
Allright guys, this is what i have: I think it will be good for testing purposes of a specific FIR, but i am now getting indexoutofbounds errors.  Please advise:

class fir2 {
static public float y(int n)
{
int a[] = new int[200];
int x[] = new int[3];
int b[][]=  new int[4][3];

for(int i=0; i<199; i++) { // load array
      a[i] = i+1;
}

for(int i = 0;i<2;i++){ // load array
      x[i]=i+1;
}

for (int i = 0; i < 4; i++){ // load 2d array
       for (int j = 0; j < 3; j++){
              b[i][j] = i*j;
    }
}      

int t1=0;

for(int k=0;k<=n-1;k++)

{
   t1=t1+a[k]*x[n-k];
}

for(int l=0; l <= l-1; l++)
for(int m=0;l<=m-1;m++)
{
   t1=t1+ b[l][m]*x[n-1]*x[n-m];
}
return n;
}
public static void main(String[] args) {
      y(2);
      System.out.println("y100="+y(100));
}
0
 

Author Comment

by:enray
ID: 10966090
Thank you all... points to venkateshwarr
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10966235
enray, there is such a thing as a point split.

Btw, the reason why you're getting an ArrayIndexOutOfBoundsException is the fact that the array's are defined with a fixed size whereas the input value of n is not bound. This makes the line;
   t1=t1+a[k]*x[n-k];
break when n is larger than the size of a. Also, x is only 3 in size and the index in the earlier referenced line will go from n to n-k, which in your code is from 100 to 0, which is way outside the bounds of that array.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question