Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of FIR filter in C#

at the moment im trying to implement a FIR lowpass filter on a wave file. The FIR coefficients where obtained using MATLAB using a 40 order. Now i need to implement the FIR algorithm in C# and im finding it difficult to implement it.

Any help?

Thanks

like image 708
Tristan Demanuele Avatar asked Mar 18 '10 17:03

Tristan Demanuele


People also ask

How do you implement a FIR filter?

Structurally, FIR filters consist of just two things: a sample delay line and a set of coefficients. To implement the filter: Put the input sample into the delay line. Multiply each sample in the delay line by the corresponding coefficient and accumulate the result.

What are the applications of FIR filter?

Finite impulse response (FIR) filters are widely used in communication [1], consumer electronics, audio [2], and other signal processing applications [3]. One of the important applications of FIR filters is as a Hilbert transformer.

How does FIR filter work?

Introduction to Digital Signal Processing The finite impulse response (FIR) filter is a nonrecursive filter in that the output from the filter is computed by using the current and previous inputs. It does not use previous values of the output, so there is no feedback in the filter structure.


2 Answers

How about this:

private static double[] FIR(double[] b, double[] x)
{
    int M = b.Length;
    int n = x.Length;
    //y[n]=b0x[n]+b1x[n-1]+....bmx[n-M]
    var y = new double[n];
    for (int yi = 0; yi < n; yi++)
    {
        double t = 0.0;
        for (int bi = M-1; bi >=0; bi--)
        {
            if (yi - bi < 0) continue;

            t += b[bi] * x[yi - bi];
        }
        y[yi] = t;    
    }
    return y;
}
like image 135
Nestor Avatar answered Sep 29 '22 06:09

Nestor


Try this. Does it help?

static void Main()
{
    var bb = new List<double> { 1, 2, 3, 4 };
    var xx = new List<double> { 3, 3, 4, 5 };

    var yy = func_FIR(bb, xx);

    for (int i = 0; i < yy.Count; i++)
    {
        Console.WriteLine("y[{0}] = {1}",i,yy[i]);
    }

}

public static List<double> func_FIR(List<double> b, List<double> x)
{
    //y[n]=b0x[n]+b1x[n-1]+....bmx[n-M]

    var y = new List<double>();

    int M = b.Count;
    int n = x.Count;

    double t = 0.0;

    for (int j = 0; j < n; j++)
    {
        for (int i = 0; i < M; i++)
        {
            t += b[i] * x[n - i-1];
        }
        y.Add(t);    
    }

    return y;
}
like image 23
Pratik Deoghare Avatar answered Sep 29 '22 06:09

Pratik Deoghare