#131Convolution — slide a 1D filter (weight sharing)Medium

Convolution — slide a 1D filter (weight sharing)

Background

The whole point of convolution is that one small filter is reused at every position (weight sharing). The clearest place to see it is 1-D: slide a length-K kernel across a length-N signal, and at each position compute the dot product (cross-correlation, the deep-learning convention — no kernel flip):

yi=j=0K1xi+jwj,i=0,,NKy_i = \sum_{j=0}^{K-1} x_{i+j}\,w_j, \qquad i = 0, \ldots, N-K

Problem statement

Implement conv1d(signal, kernel) returning the valid 1-D convolution (stride 1, no padding).

Input

  • signal — array-like of length N.
  • kernel — array-like of length K (KNK \le N).

Output

Returns an np.ndarray of length N - K + 1: the feature map.

Examples

Example 1 — a moving sum

Input:  signal = [1, 2, 3, 4], kernel = [1, 1]
Output: [3. 5. 7.]

Explanation: adjacent sums 1+2,2+3,3+41+2, 2+3, 3+4.

Example 2 — an edge detector

Input:  signal = [0, 0, 5, 5, 0], kernel = [-1, 1]
Output: [0. 5. 0. -5.]

Explanation: the kernel [-1, 1] responds to changes; it spikes at the 0→5 and 5→0 transitions.

Constraints

  • Use the cross-correlation convention (no kernel flip).
  • Output length is N - K + 1.
  • Return an np.ndarray.

Notes

  • Every output reuses the same kernel weights — that is weight sharing, and it's why the same detector finds a pattern wherever it occurs (translation equivariance).
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example — moving sum kernel
  • Reference — edge detector [-1,1]
  • Sample — single window when K==N