How To Use Bandpass Filters – C# Guide


Andraz Krzisnik
How To Use Bandpass Filters – C# Guide

Bandpass filters are the counterpart of bandreject filters. Therefore, they attenuate every frequency outside the ring.

In case you’re just tuning in, let me clarify what I meant by all above.

We use bandpass filters to process data in the frequency domain and we will focus on processing image data in this guide. Therefore we need to transform image into frequency domain before we do anything with filters. For that we need to use Fourier transform.

To filter data we have to multiply each frequency with filter formula. Each filter has its own but there are similarities between bandpass and bandreject filters. Much like how highpass and lowpass filters differ one another.

Applying bandpass filters

As we can use ideal, Butterworth and Gaussian bandreject filters, we can also use their bandpass counterparts. In other words, they attenuate frequencies bandreject filters pass and pass frequencies that bandreject attenuates.

Ideal bandpass filter

This filter attenuates frequencies instantly at the edge of the ring.

ideal bandpass filter on Fourier transform
Ideal bandpass filter on Fourier transform visualized

Function that applies this filter is very similar to the bandreject one, since all we needed to change are conditions which frequencies to attenuate.

public static Complex[][][] IdealBandpassFilter(Complex[][][] frequencies, double d0, double W)
         {
             Complex[][][] filtered = frequencies;
             for (int i = 0; i < 3; i++)
             {
                 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)));
                         if (d <= (d0 - W/2) || d >= (d0 + W/2))
                         {
                             filtered[i][u][v] *= 0f;
                         }
                     }
                 }
             }
         return filtered;
     }

Butterworth bandpass filters

As with all other Butterworth filters, this one has an additional parameter called order. When we set the order of the filter, we basically control how smooth the transition between attenuated and passed frequencies is.

Butterworth bandpass filters on Fourier transform
Butterworth bandpass filter on Fourier transform

We need the formula for Butterworth bandreject filter and subtract it from 1. Here’s the code that applies this.

public static Complex[][][] ButterworthBandpassFilter(Complex[][][] frequencies, double d0, int n, double W)
         {
             Complex[][][] filtered = frequencies;
             for (int i = 0; i < 3; i++)
             {
                 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[i][u][v] *= 1f - (float)(1f / (1 + Math.Pow(d * W / (dd - d0d0), 2 * n)));
                     }
                 }
             }
         return filtered;
     }

Gaussian bandpass filter

Gaussian bandpass filter makes the smoothest transition between attenuated and passed frequencies of the three. The same principle as with other bandpass filters applies here as well. In other words, we need the formula from Gaussian bandreject filter.

public static Complex[][][] GaussianBandpassFilter(Complex[][][] frequencies, double d0, double W)
         {
             Complex[][][] filtered = frequencies;
             for (int i = 0; i < 3; i++)
             {
                 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[i][u][v] = (float)(Math.Pow(Math.E, (-Math.Pow((dd - d0*d0) / (d * W), 2))));
                     }
                 }
             }
         return filtered;
     }

Conclusion

I recommend checking out the entire project. I implemented fast Fourier transform so the computation time is much shorter than DFT. However it does have some constraints, like the size and shape of the images it takes. Hopefully this guide was helpful and instructive.

Related Articles

Simple Lists

How To Make Array Lists In C# – Made Easy

Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.

Posted on by Andraz Krzisnik
Image Noise

How To Make Uniform Noise On Images – C# Guide

Uniform noise is one of the noise models we can use to simulate real life data corruption. This guide shows how to make in on images.

Posted on by Andraz Krzisnik