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:
- Calculate the poles of the digital transfer function H(s)
- Use bilinear transformation to get the poles of the analog transfer function H(z)
- 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.
Can anyone help me please?