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.

Show Comments (0)

Comments

Related Articles

Grayscale Morphology

How To Make Morphological Smoothing Work With C#

Morphological smoothing is an image processing technique, which includes grayscale erosion and dilation, and grayscale opening and closing

Posted on by Andraz Krzisnik
Adaptive Filters

How To Make Adaptive Median Filter For Images With C#

Adaptive median filter is much more effective at removing impulse noise, also known as salt and pepper noise, than traditional median filter.

Posted on by Andraz Krzisnik