v9.1.1
Menu

1D Convolution

easyFREECNNs & Sequence ModelsMachine Learning 2

Implement 1D convolution (valid mode, no padding).

Given a 1D signal and a kernel, slide the kernel across the signal and compute the dot product at each position:

output[i]=∑j=0k−1signal[i+j]⋅kernel[j]\text{output}[i] = \sum_{j=0}^{k-1} \text{signal}[i+j] \cdot \text{kernel}[j]

Return the convolution result, rounded to 4 decimal places.

Example

Input
signal = [1, 2, 3, 4, 5]
kernel = [1, 0, -1]
Output
[-2, -2, -2]
Reasoning
  • Inputs: signal is a sequence of n=5n = 5 samples, 1,2,3,4,51, 2, 3, 4, 5; kernel holds k=3k = 3 weights, 1,0,−11, 0, -1. Valid mode keeps only placements fully inside the signal: n−k+1=3n - k + 1 = 3 positions.
  • Position 0 covers samples 1,2,31, 2, 3: 1⋅1+2⋅0+3⋅(−1)=1+0−3=−21 \cdot 1 + 2 \cdot 0 + 3 \cdot (-1) = 1 + 0 - 3 = -2.
  • Position 1 covers 2,3,42, 3, 4: 2⋅1+3⋅0+4⋅(−1)=−22 \cdot 1 + 3 \cdot 0 + 4 \cdot (-1) = -2. Position 2 covers 3,4,53, 4, 5: 3−5=−23 - 5 = -2.
  • Output: three values, [-2, -2, -2], already exact at 4 decimal places. With weights 1,0,−11, 0, -1, each output is signal[i]−signal[i+2]\text{signal}[i] - \text{signal}[i+2], a difference of samples two steps apart.
  • Notice: the signal climbs a steady 11 per step, so every window gives the same −2-2: a slope detector on a straight line. The kernel is applied as written, not flipped; flipping this asymmetric kernel would turn every value into +2+2.

Constraints

  • signal: 1D list of numbers
  • kernel: 1D list of numbers (length <= len(signal))
  • Return 1D list of length (len(signal) - len(kernel) + 1)
  • Round each value to 4 decimal places

Learn this in

More from Machine Learning 2

solution.py

Test results

Sign in to run your code against the test cases.