0

again I am still trying to get my lowpass filter running, but I am at a point where I do not know why this is still not running. I oriented my code according to FFT Filters and my previous question FFT Question in order to apply an ideal low pass filter to the image. The code below just makes the image darker and places some white pixels in the resulting image.

// forward fft the result is in freqBuffer
fftw_execute(forward);

for (int y = 0; y < h; y++)
{
    for (int x = 0; x < w; x++)
    {
        uint gid = y * w + x;

        // shifting coordinates normalized to [-0.5 ... 0.5]
        double xN = (x - (w / 2)) / (double)w;
        double yN = (y - (h / 2)) / (double)h;

        // max radius
        double maxR = sqrt(0.5f * 0.5f + 0.5f * 0.5f);

        // current radius normalized to [0 .. 1]
        double r = sqrt(xN * xN + yN * yN) / maxR ;

        // filter response
        double filter = r > 0.7f ? 0.0f : 1.0f;

        // applying filter response
        freqBuffer[gid][0] *= filter;
        freqBuffer[gid][1] *= filter;
    }
}

// normlization (see fftw scaling)
for (uint i = 0; i < size; i++)
{
    freqBuffer[i][0] /= (float)size;
    freqBuffer[i][1] /= (float)size;
}

// backward fft
fftw_execute(backward);

Some help would be appreciated.

Wolf

Community
  • 1
  • 1
DerHandwerk
  • 99
  • 3
  • 10
  • For arbitrary content, a brick-wall filter (zeroing FFT bins) is far from ideal. – hotpaw2 May 01 '12 at 22:10
  • As far as I see it this behavior is called "ideal" see [Ideal Low Pass](https://ccrma.stanford.edu/~jos/sasp/Ideal_Lowpass_Filter.html) – DerHandwerk May 01 '12 at 22:45
  • Note that this filter is continuous, which can behave very differently from FFT bins (where the response can have huge overshoots between bin centers instead of being flat, as per the diagram). – hotpaw2 May 01 '12 at 23:04
  • That's exactly what I experienced. So I replaced it by a filter with cosine transition, which is much smoother. The result is much more ideal than the result of the ideal filter. :-D – DerHandwerk May 01 '12 at 23:15

1 Answers1

1

If you have a filter with a step response in the frequency domain then you will see significant sin(x)/x ringing in the spatial domain. This is known as the Gibbs Phenomenon. You need to apply a window function to the desired frequency response to mitigate this.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • I recently discovered that if I toggle the operator from `filter = r > 0.7f ? 0.0f : 1.0f` to `filter = r < 0.7f ? 0.0f : 1.0f` it behaves like a low pass in this case with the ringing effect you mentioned. The code as it is written above applies a high pass filter, although I expected the opposite. Could it be that this is the result of the low frequencies which are located at the edges? – DerHandwerk May 01 '12 at 22:49
  • It depends on which FFT you are using as to how the spatial frequencies are arranged in the FFT output array - see the FFTW documentation for this - if you're using a real-to-complex 2D FFT though then I think that DC is at (0,0), not in the centre. – Paul R May 02 '12 at 07:01
  • Yes fftw places them at the edge, so I have to compute `1 - r` then it works! – DerHandwerk May 02 '12 at 18:59