# 卷积神经网络代写 | ECE 4/574 Homework 5.5

Convolution:
In what follows when we say “1D signal”, we mean a 1-dimensional array of type logic[31:0].
By “2D signal” we mean a 2D-dimensional array of type logic[31:0] (i.e. each entry in the array is of this
type).
When we only say “signal” we mean a 1D signal.

We define a simplified version of convolution, in which the signal does not get inverted. Specifically,
given signal x of length N, and a shorter signal y of length 2*L+1 (for some integer L), the convolution of
the two is a third signal z given by

(Eq. 1) z[n] = x[n-L]*y[0] + x[n-(L-1)]*y[1] + … + x[n]*y[L] + x[n+1]*y[L+1] + … + x[n+L]*y[2L]
where 0 <= n <= N-1

To calculate z[n], you can imagine that you take the y signal, center it on x[n], take the product of over-
lapping elements, and add the products.

Border cases: if Eq.1 refers to out-of-bound elements, you can consider them to be 0. E.g. if n<L, then
consider that x[n-L] = 0 and if n+L>N-1 then consider that x[n+L]=0.

Example
Say x = (1,1,1,1,1,1) and y = (2,3,4). So N=4 and L = 1. Then
z[0] = x[0-1]*y[0] + x[0]*y[1] + x[1]*y[2] = 0*2 + 1*3 + 1*4 = 7. (border case)
z[1] = x[1-1]* y[0] + x[1]*y[1] + x[2]*y[2] = 9.
z[2] = x[2-1]* y[0] + x[2]*y[1] + x[3]*y[2] = 9.

z[5] = x[5-1]*y[0] + x[5]*y[1] + x[6]*y[2] = 1*2 + 1*3 + 0*4 = 5. (border case)

A 2D convolution is similar, but this time the signals are 2D arrays. 2D convolutions are used for image
processing, in which case x is called the image and y is called the kernel.
Given a 2D signal x which is N-by-N and a smaller signal y which is (2L+1)-by-L(2L+1), their convolution is
a third 2D signal z. The signal y is often called the “kernel” in machine learning.

(Here we are indexing from the top-left, so z[0][0] is the top-left (first row, first column), z[0][1] is first
row, 2nd column, and z[1][0] is 2nd row, first column).

To calculate z[n,m], center y (often called the kernel) on x[n,m]; take the product of overlapping terms.
Then add the products. Same as before, entries outside the matrix bounds are considered to be 0.

Example:
x = [1 1 1 1
2 2 2 2
3 3 3 3] (semi-colons separate the rows)
y = [0.5 0.5 0.5
0.5 0.5 0.5
0.5 0.5 0.5]

z = [3 4.5 4.5 3
etc]
(z[0,0] = 0.5*(1+1+2+2), etc)
Work to do:
Your design will implement the 1-dimensional (1D) convolution.

Your design will implement the 2-D convolution, and write the code that collects coverage.

1) Module interface
Your module’s interface must conform to the following. Three dots indicate something you need to fill
in, like the data type, size, etc.
module convolution … (
input …. signal_x … ,
input … signal_y …,
input logic start,
input logic clk,
output … signal_z …,
output logic done);

The start and done signals are as usual: the module starts computing when start is asserted by the
testbench (for one clock cycle), and any further assertions of start while the module is computing are
ignored. When done computing, the module asserts done for one clock cycle.
Your module must be parametrized with parameter N: this is the size of image signal x. (recall x is length
N in 1D and N-by-N in 2D). The kernel signal y will always be of length 3 (in 1D) or 3-by-3 (in 2D). Default
value of N = 9.
Put your module in a file called convolution.sv

E-mail: itcsdx@outlook.com  微信:itcsdx