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):
Problem statement
Implement conv1d(signal, kernel) returning the valid 1-D convolution (stride 1, no padding).
Input
signal— array-like of lengthN.kernel— array-like of lengthK().
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 .
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
kernelweights — that is weight sharing, and it's why the same detector finds a pattern wherever it occurs (translation equivariance).
▶ 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