Navigation

Related Articles

Back to Latest Articles

How To Use Butterworth Highpass Filter – C# Guide


Andraz Krzisnik
How To Use Butterworth Highpass Filter...

Butterworth highpass filter is just one of the highpass filters. We use these type of filters for filtering data in the frequency domain.

I’ve talked about transforming the images into frequency domain more in detail on a post about Fourier transform. If we convert image data into frequency domain we get a map of complex numbers. And when we talk about filtering data in the frequecy domain, it simply means we multiply each complex number in the map with a filter formula.

If you take a look at ideal highpass filter, it basically cuts out a hole in the middle of a Fourier transform. Butterworth has a similar effect on the transform. It differs at the transition between frequencies we want to cut out and those we don’t.

Butterworth highpass filter

butterworth highpass filter
Butterworth highpass filter with cut off frequency 30 and order 2

What makes Butterworth highpass filter special is that smooth transition between high and low frequencies. It does require an additional parameter we need to set, which is the order of the filter.

Let’s take a look what the filter formula looks like and dissect it, so we’ll get a better understanding how it works.

Butterworth highpass filter formula
Butterworth highpass filter formula

Okay, so the formula above might look a little daunting, but don’t worry we’ll go through it piece by piece.

First of all, the u and v variables represent the coordinates on a frequency map that we get from Fourier transformation. Going forward, let’s take a look what’s going on below the fraction.

The n is the order we mentioned earlier. So the larger the order is, the more filter acts as an ideal highpass filter. It’s the controllable part that sets the width of the fade between the frequencies we want cut out and those we don’t.

D0 represents our cut off frequency and it stands for distance from center or in other words, the radius of the circle in the middle.

The D(u, v) is the distance from the center of each frequency on the map given the position of the coordinates. We can calculate it by the following formula.

The M and N expressions represent the width and height of the frequency map, which in our case are the same since our fast fourier transform algorithm only accepts square images.

C# for Butterworth highpass filter

The following function takes a 3 dimensional jagged array of complex numbers. Therefore we need to make 3 frequency maps, one for each color channel.

public static Complex[][][] Butterworth_Highpass_Filter(Complex[][][] frequencies, double distance, int order)
         {
             int channels = 3;
             Complex[][][] filtered = frequencies;
             for (int c = 0; c < channels; c++)
             {
                 for (int u = 0; u < Size; u++)
                 {
                     for (int v = 0; v < Size; v++)
                     {
                         double d = Math.Sqrt(Math.Pow(u - Size / 2, 2) + Math.Pow(v - Size / 2, 2));
                         filtered[c][u][v] *= (float)(1 / (1 + Math.Pow(distance / d, 2 * order)));
                     }
                 }
             }
         return filtered;
     }

If we compare it to the function for Butterworth lowpass filter, we can see it’s almost identical. The only difference is that the cut off frequency and calculated distance from center at any given position are swapped.

Conclusion

You can download the entire project and play with the values. The project also includes all code for fast Fourier transform, without which all this wouldn’t work anyway.

Related Articles

Image Noise

How To Make Gamma Noise On Images – C# Guide

Gamma noise is one of the noise models we use to simulate practical data corruption. This guide shows how to apply it on images.

Posted on by Andraz Krzisnik
Linked Lists

Everything About Circular Linked Lists In C# – Made Easy

Circular linked lists are an upgrade to generic double linked lists. They can loop around when navigating to adjacent elements.

Posted on by Andraz Krzisnik