DIP+CV · Programming Exercise

EP05_02 — 🟡 Notch Filter: Removing Periodic Peaks

5.13.2 EP05_02 🟡 Notch Filter: Removing Periodic Peaks

An industrial inspection camera captures images of circuit boards, but the production line’s power supply introduces a periodic electrical interference — a stripe pattern almost imperceptible to the naked eye, yet visible in the Fourier spectrum as pairs of bright peaks symmetrically positioned around the center. The computer vision team cannot redo the capture: they must surgically locate and erase these peak pairs in the spectrum, preserving all other useful image information.

This is the role of the notch reject filter: unlike a low-pass filter (which affects a continuous region), it targets specific points and their symmetric counterparts, leaving the rest of the spectrum untouched.

5.13.2.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns) of the centered magnitude spectrum.
  2. Data: Read the integer values of the magnitude matrix, row by row.
  3. Peaks: Read the integer \(K\) (number of peak pairs to remove).
  4. For each of the \(K\) peaks: read three integers \(\Delta v\), \(\Delta u\), \(r\) — vertical offset, horizontal offset, and notch radius.
  5. Spectrum center: \((c_y, c_x) = (L \mathbin{//} 2,\; C \mathbin{//} 2)\).
  6. Symmetric suppression: for each peak, zero out all positions \((u,v)\) such that the distance to the point \((c_y+\Delta v,\, c_x+\Delta u)\) is \(\le r\), and also all positions with distance \(\le r\) to the symmetric point \((c_y-\Delta v,\, c_x-\Delta u)\).
  7. Output: Display the resulting matrix with dimensions \(L \times C\).

5.13.2.2 📌 Computational Constraints

  • Mandatory symmetry: each reported peak generates two zeroed disks (the point and its symmetric counterpart relative to the center) — forgetting the symmetric point is the most common mistake.
  • Overlap: if two disks overlap, the position remains zeroed (there is no “addition” or restoration).
  • Non-strict comparison: a position is zeroed if \(\text{distance} \le r\).
  • Reading order: the \(K\) peaks must be processed in the order they appear in the input, but the final result is independent of order (zeroing operations are commutative).

5.13.2.3 🧠 Theoretical Foundation

Concept Role in the notch filter
Peak at \((\Delta v, \Delta u)\) Frequency of the periodic interference visually detected in the spectrum
Symmetric point \((-\Delta v,-\Delta u)\) Every DFT of a real signal is Hermitian: peaks always appear in pairs symmetric about the center
Radius \(r\) Controls the “width” of rejection — a large \(r\) removes more energy around the peak, but also useful information

5.13.2.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Following lines: Integer elements of the magnitude matrix (centered), \(L\) lines.
  • Next line: Integer \(K\).
  • Following \(K\) lines: three integers \(\Delta v\), \(\Delta u\), \(r\) (space-separated).

Output:

  • The resulting matrix in \(L\) lines and \(C\) columns, space-separated.

5.13.2.5 📌 Examples

Input Output Observation
5
5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1
1 1 0
1 2 3 4 5
6 0 8 9 10
11 12 13 14 15
16 17 18 0 20
21 22 23 24 25
Center \((c_y, c_x) = (2, 2)\). Reported peak \((\Delta v, \Delta u) = (1, 1)\) generates the point \((3, 3)\) (value 19) and its symmetric counterpart \((1, 1)\) (value 7), both zeroed with \(r=0\) (only the exact points).
🎮 Simulator EP05_02: Notch Filter Symmetric Pair
1
1
0
Move Δv e Δu to choose the peak — note that the symmetric pair is also filtered.
Spectrum 5×5 (Red = Removed by the Filter)
–
Figure 5.33: EP05_02 Simulator: Notch Filter
%%writefile EP05_02.cpp
// your solution
Overwriting EP05_02.cpp
TestSuite("EP05_02.cpp").run()
✔️ EP05_02.cases already exists in casos/
📋 5 case(s) loaded from casos/EP05_02.cases

🔍 Testing C++: EP05_02.cpp
⚠️ EP05_02.cpp: Empty file (fewer than 3 lines). Tests skipped.