# DeFreq

### Interference frequency remover

Plugin for Avisynth 2.5
Version 0.7 - April 28, 2006
Copyright (C)2004-2006 Alexander G. Balakhnin aka Fizick.
http://avisynth.org.ru

DeFreq is a filter that can remove some interfering frequencies from video frames.

It can be used for removal of regular vertical, horizontal or inclined stripes from some analog TV captures without any image quality decreasing.

## Sample image with filter's work

Stack vertical:

1 - TV source with interference (cropped);
2 - Defreq v.0.6 show and info mode (2 search windows);
3 - Cleaned output;
4 - Input-output difference.

## Technical info

DeFreq uses Fast Fourier Transform method for frequency selecting and removing.

It is a spatial filter, and works as a follows:

1. get frequency spectrum by forward FFT transform of frame image;
2. search some defined spectrum window (windows) for local peak (maximum, extremum, most intense frequency);
3. remove from spectrum a frequency at this sharp peak and some similar by amplitude neighbors in search window;
4. optional remove highest frequencies by low pass Butterworth filter (for blurring, smoothing);
5. get output frame by inverse FFT transform of cleaned spectrum.

## Function parameters

`Defreq`(clip, float "fx", float "fy", float "dx", float "dy", float "sharp", float "fx2", float "fy2", float "dx2", float "dy2", float "sharp2", float "fx3", float "fy3", float "dx3", float "dy3", float "sharp3", float "fx4", float "fy4", float "dx4", float "dy4", float "sharp4", float "cutx", float "cuty", int "plane", int "show", bool "info", bool "measure")

first parameter - input clip
fx - window center frequency x-component for local peak search (float percent from 0.0 to 100.0, default= 10.0)
fy - window center frequency y-component for local peak search (float percent from -100.0 to 100.0, default= -10.0)
dx - half-width of search window, (float percent, default =1.5).
dy - half-height of search window, (float percent, default =2.0).
sharp - threshold of the peak to background relation (in search window) for cleaning (float, default=50.0)
fx2 - second window center frequency x-component for local peak search (float percent from 0.0 to 100.0, default=0 - disabled)
fy2 - second window center frequency y-component for local peak search (float percent from -100.0 to 100.0, default=0 - disabled)
dx2 - half-width of second search window, (float percent, default =1.5).
dy2 - half-height of second search window, (float percent, default =2.0).
sharp2 - threshold of the peak to background relation (in second window)for cleaning (float, default=50.0)
fx3 - third window center frequency x-component for local peak search (float percent from 0.0 to 100.0, default=0 - disabled)
fy3 - third window center frequency y-component for local peak search (float percent from -100.0 to 100.0, default=0 - disabled)
dx3 - half-width of third search window, (float percent, default =1.5).
dy3 - half-height of third search window, (float percent, default =2.0).
sharp3 - threshold of the maximum frequency to background relation (in third window) for cleaning (float, default=50.0)
fx4 - fourth window center frequency x-component for local peak search (float percent from 0.0 to 100.0, default=0 - disabled)
fy4 - fourth window center frequency y-component for local peak search (float percent from -100.0 to 100.0, default=0 - disabled)
dx4 - half-width of fourth search window, (float percent, default =1.5).
dy4 - half-height of fourth search window, (float percent, default =2.0).
sharp4 - threshold of the peak to background relation (in fourth window) for cleaning (float, default=50.0)
cutx - frequency x-component cutoff for low pass filter (float percent from 0.0 to 300.0, default = 0, disabled)
cuty - frequency y-component cutoff for low pass filter (float percent from 0.0 to 300.0, default = 0, disabled)
plane - color plane to clean, 0-Y, 1-U, 2-V (default = 0)
show - show frequency spectrum surface and search windows (default = 0)
0 - normal processing;
1 - show current frame spectrum;
2 - show temporal average spectrum;
info - type found peaks info on frame (default = false)
measure - select fastest FFT method by speed measure (longer init stage) instead of simple estimation (default=true)

## Features and limitations

1. Works only in YV12 and YUY2 color format.
2. Directly works with progressive clips. For interlaced sources, probably you must use SeparateFields etc.
3. Tested with Avisynth v2.5.5-2.5.6.
4. Defreq uses fast external FFTW library version 3 (http://www.fftw.org) as Windows binary DLL (compiled with gcc under MinGW by Alessio Massaro), which support for threads and have AMD K7 (3dNow!) support in addition to SSE/SSE2.It may be downloaded from ftp://ftp.fftw.org/pub/fftw/fftw3win32mingw.zip. You MUST put FFTW3.DLL file from this package to some directory in path (for example, system folder C:\WINNT\SYSTEM32). Defreq will NOT work without it!
5. Not very fast, not assembler optimized.
6. Since v.0.4, horizontal frequency is relative to spectrum image width (about half of plane width), from 0 to 100 %, and vertical frequency is relative to spectrum image half-height (plane height), from -100 to 100%.
7. Since version 0.4, frequency values fx,fy, etc are independent from crop, but must be scaled after resize!

## Simple sample script

    Avisource("input.avi")
    loadplugin("defreq.dll")
    DeFreq(fx=30,fy=-40)

Attention! It is calling syntax example only. Unlike most another filters, there are NO good DeFreq parameter values for general use!
It will not clean anything (but slow down the processing), if its settings are not exactly correspondent to your picture.

## How to use

You must temporary add show and info parameters, visually analyze frequency spectrum surface, and adjust parameters fx,fy to place rectangular search window at local peak of spectrum (bright spot or cross), which is correspondent to interference frequency.

Firstly look to generated sample frequency stripes at the right top corner, and try make them similar to your picture interference pattern (right bottom area). The sample stripe slope is dependent from fy/fx ratio, and the step between sample stripes is dependent from fx,fy absolute values (hint - you may use some AviSynth script parameter to preserve fy/fx ratio).

Then look to spectrum sufrace, and fine tune the search window position.
The small fxis near left edge of the spectrum image, and the large fx (up to 100) is near right edge.
Since v0.6, the large negative fy values is at bottom of the spectrum image, the large positive fy (up to 100) is at top of the spectrum image, the small fy is at middle of height.

Set fx=0 for strictly horizontal frequency interference stripes, or set fy=0 for strictly vertical stripes.

Mode show=2 make frequency spectrum peaks more visible by temporal averaging of frames power spectral density. Just play your video clip (preferrably dynamic scenes), and tune fx,fy.

You may tune other parameters to set sharpness threshold, color plane, add other search windows, add low pass filter.
Hint - "sharp" word is become UPPERCASE (in info mode), when peak with value above threshold is detected.

## Additional info

The filter discussion is at http://forum.doom9.org/showthread.php?s=&threadid=82978

There are also similar new filters FanFilter and FQuiver (by vcmohan).

## License

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Documentation is distributed under CreativeCommons BY-SA 3.0 license.

Please consider to make some donation.

## Version changes:

- Version 0.1, 22 September 2004 - not public alpha. Thanks to Mil_Alex for preliminary discussion.
- Version 0.2, 26 September 2004 - add second window, set other planes to 128 (first public beta).
- Version 0.3, 28 September 2004 - change cleaning method to adaptive in window, add third and fourth search windows, change default sharp=50.0 (was 100.0 really) (not public)
- Version 0.4, 30 September 2004 - change fx,fy,dx,dy,cutx,cuty parameters to (float) percent of FFT size
- Version 0.4.1, 01 October 2004 - fixed bug with init
- Version 0.5, 02 October 2004 - add show sample frequency stripes, add Measure mode, some bugs fixed (still beta). Thanks to Mug Funky and JuanC for bugs report and discussion
- Version 0.5.0.1, 06 October 2004 - add Russian doc
- Version 0.5.1, 07 October 2004 - more fast processing of frames which are not needed in cleaning, source released under GPL.
- Version 0.5.2, 7 November 2004 - fixed strange bug for separated fields. Thanks to johnmeyer for report.
- Version 0.5.3, 7 November 2004 - finally (?) fixed bug for separated fields. Thanks to johnmeyer for report.
- Version 0.5.4, 3 December 2004 - Fixed bug for second peak for YUY2. Thanks to oledoe for report. Manual corrected. Thanks to Palmnut for report.
- Version 0.6, August 2, 2005 - added temporal average mode show=2 (thanks to lumier for idea), changed show parameter to integer type; reorganized view of the spectrum window; cleaned a code.
- Version 0.7, April 28, 2006 - changed cleaning method: more frequencies are decreased near interference peak.
  Chanded static linking of FFTW3.DLL library to dynamic loading.

### Download DeFreq version 0.7

Return to main page