# Implement a Butterworth Filter in C#

I'm very new to signal processing, but was asked to implement a filter function for Butterworth low pass filter for given order, cutoff frequency and sample rate.

After reading some articles I think i figured out what I have to do is:

1. Calculate the poles of the digital transfer function H(s)
2. Use bilinear transformation to get the poles of the analog transfer function H(z)
3. Write difference equation with y(k) = ...

For now I implemented the calculation of the poles, which seems to work:

     public class ButterworthBase
{
private const double Gain = 1; // Gain for Butterworth

public ButterworthBase(int order, double cutoffFrequency, double sampleRate)
{
// calc digital cutoff frequency
double wc = 2 * Math.PI * cutoffFrequency;

var poles = new ImaginaryNumber[order];

// calc poles TODO: calc only (order) n / 2 poles => mirror real axis to get the other n/2 poles
for (int k = 1; k <= order; k++)
{
// poles[k] = wc * Math.Exp( i * exponent);

double exponent = (Math.PI / (2.0 * (double)order)) * (2.0 * k + (double)order - 1.0);
double real = Math.Cos(exponent);
double imaginary = Math.Sin(exponent);
poles[k - 1] = new ImaginaryNumber(wc * real, wc * imaginary);
}

// bilinear transformation of poles:
var transformedPoles = new ImaginaryNumber[order];

for (int k = 1; k <= order; k++)
{
var transformedPole = new ImaginaryNumber(0, 0);

transformedPoles[k - 1] = transformedPole;
}
}
}

public class ImaginaryNumber
{
public double Real { get; }
public double Imaginary { get; }

public ImaginaryNumber(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}
}


I'm struggling at the transformation, I know the transfer function is s -> 2/T (1-z^-1)/(1+z^-1) where T = sampling period.