### 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...

Filter by Category

- C# Tutorial(85)
- C# Image Processing(69)
- Morphological Processes(20)
- Image Processing(16)
- Image Restoration and Reconstruction(16)
- Image Segmentation(13)
- C# Data Structures And Algorithms(11)
- Frequency Domain Filtering(8)
- Color Image Processing(8)
- Image Noise(6)
- Grayscale Morphology(5)
- Thresholding(4)
- Sorting Algorithms(4)
- Mean Filters(4)
- Order-Statistic Filters(4)
- Morphological Reconstruction(3)
- Edge Detection(3)
- Simple Lists(2)
- Linked Lists(2)
- Adaptive Filters(2)
- RGB to HSI Color Model(2)
- Tone and Color Corrections(2)
- Landing Pages(1)
- Point Detection(1)
- Line Detection(1)
- Social Games(1)
- Region Growing Segmentation(1)
- Region Splitting And Merging(1)
- Region Segmentation With K Means Clustering(1)
- Region Segmentation Using Superpixels(1)
- C# Arrays(1)
- C# Basics(1)
- Sorted Lists(1)
- Stacks(1)
- Queues(1)
- Bandreject Filters(1)
- Bandpass filters(1)
- Notch Filters(1)
- Intensity Slicing and Color Coding(1)
- Color Slicing(1)
- Histogram Processing Color Images(1)
- Color Image Smoothing And Sharpening(1)
- Using Color In Image Segmentation(1)
- Digital Image Watermarking(1)

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.

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.

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

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;`

` }`

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.

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 / (d
```*d - d0*d0), 2 * n)));
}
}
}
` return filtered;`

` }`

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((d*d - d0*d0) / (d * W), 2))));
}
}
}
` return filtered;`

` }`

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.

## Comments