Question

I use the following function to filter a signal. The code works perfectly for order 4, but when I use a higher order, like 5, I get NaN values in the output. Your help is highlly appreciated

void filt(int ord, double a[], double b[], int np,  double x[], ArrayList<Double> array)
    {
        int i,j;
       // y[0]=b[0]*x[0];
        array.add(0, b[0]*x[0]);
        for (i=1;i<ord+1;i++)
        {

            array.add(i, 0.0);
            for (j=0;j<i+1;j++)
               array.add(i, array.get(i)+b[j]*x[i-j]);
            for (j=0;j<i;j++)
                array.add(i, array.get(i)-a[j+1]*array.get(i-j-1));
        }
        for (i=ord+1;i<np;i++)
        {
           array.add(i, 0.0);
                for (j=0;j<ord+1;j++)
                array.add(i, array.get(i)+b[j]*x[i-j]); 
                for (j=0;j<ord;j++)
                array.add(i, array.get(i)-a[j+1]*array.get(i-j-1));
        }

    }
Was it helpful?

Solution

I tried the same functionality of Matlab, and got NaN values. I dig in Matlab documentation and found the following which explains the NaN values:

"For higher order filters (possibly starting as low as order 8), numerical problems due to roundoff errors may occur when forming the transfer function using the [b,a] syntax." Therefor the butterworth filter is unstable for higher orders.

To get over this problem in Matlab, try the following:

n = 6; Wn = [2.5e6 29e6]/500e6;
ftype = 'bandpass';

% Transfer Function design
[b,a] = butter(n,Wn,ftype);
h1=dfilt.df2(b,a);      % This is an unstable filter.

% Zero-Pole-Gain design
[z, p, k] = butter(n,Wn,ftype);
[sos,g]=zp2sos(z,p,k);
h2=dfilt.df2sos(sos,g);

% Plot and compare the results
hfvt=fvtool(h1,h2,'FrequencyScale','log');
legend(hfvt,'TF Design','ZPK Design')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top