Digital Filter, Math in Java,

java, math, signal-processing

Solution

Whatever language you use, the direct form II transposed structure is quite simple.

For example, in C, it could be something like:

float myFilter( float u)
{
  static float[nb] x = {0,0,0,...,0);  // initialize x
  static float[na] y = {0,0,0,...,0);  // initialize y
  static float b1 = ....;  // put b(1) here
  static float[nb] b = {...,...,...,...,...}; // put b(2) to b(nb+1) here
  static float[na] a = {...,...,...,...,...}; // put a(2) to a(na+1) values here

  // initialization
  float sum = 0;
  int i=0;

  // compute the value
  for(i=0;i<nb;i++)
    sum += b[i]*x[i];
  for(i=0;i<na;i++)
    sum -= a[i]*y[i];
  sum += b1*u;

  // prepare the values for the next time
  for(i=1;i<nb;i++)
    x[i] = x[i-1];
  x[0] = u;
  for(i=1;i<na;i++)
    y[i] = y[i-1];
  y[0] = sum;

  // return the value
  return sum;
}

I did not test the code, but it is something like that.

The Direct Form II transposed is the simplest form to implement a FIR filter (numerically, and specially in fixed-point, it is not the best, but it is the form that requires the less operations).

Of course, it is possible to have a better implementation (with cycling array, for example). If needed, I can provide it, too.

EDIT: I answered too quickly. The algorithm you provide

y(n) = b(1)x(n) + b(2)x(n-1) + ... + b(nb+1)x(n-nb) - a(2)y(n-1) - ... - a(na+1)*y(n-na)

is not the Direct Form II, but the direct form I. It requires to store na+nb values (n is the order of your filter), whereas the Direct Form II requires only max(na,nb). The algorithm used for the Direct Form II is

e(n) = u(n) - a(1)*e(n-1) - a(2)*e(n-2) - ... - a(na)*e(n-na)
y(n) = b(1)*e(n-1) + b(2)*e(n-2) + ... + b(nb)*e(n-nb)

Tell me if you need this form or not.

Problem

I need your help, and thank you for reading my question! I am currently writing a java Programm that will use an Direket Form 2 Transposed Filter. I know that the function filter in Matlab will do that just fine, but i have to use Java. So does anyone know you to implement this Direkt Form 2 Transposed , this Math Function: y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na) in any Programmm Language? All it takes is hopefully a point to the wrigth direction so i can figure it out! Maybe there is an C Lib that implements some of the matlab functions, just anything. So thank you for your time yours Elektro Follow up: I tried for a couple of days to understand your function but i couldn't. This is the function from Matlab: filter http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/filter.html&http://www.google.de/search?hl=de&q=filter+matlab&btnG=Google-Suche&meta=&aq=f&oq= All i know is that i use in matlab the function like this: newArray = filter(1,LPC_Faktor,OldArray) All I have to do is to implement the filter function. So could you help again? Thanks Elektro

Original source