Dot Product

Mathematical Definition

Assuming two input vectors, x=[x1,x2,...,xn]x = [x_1, x_2, ..., x_n] and y=[y1,y2,...,yn]y = [y_1, y_2, ..., y_n], the dot product is calculated as:

xy=i=1nxiyi=x1y1+x2y2+...+xnynx \cdot y = \sum_{i=1}^n x_i y_i = x_1y_1+x_2y_2+...+x_ny_n

Code

MATLAB Code:

1
2
z = dot(x, y)
z = x * y'

Python Code

1
z = np.dot(x, y)

Convolution

Mathematical Definitions

Discrete Convolution Definition:

Assuming two vectors, x=[x1,x2,...,xn]x = [x_1, x_2, ..., x_n] and h=[h1,h2,...,hn]h = [h_1, h_2, ..., h_n], the convolution result is calculated as:

(xh)[n]=x[n]h[n]=i=+x(i)h(ni)=i=+x(ni)h(i)(x * h)[n] = x[n] * h[n] = \sum_{i=-\infty}^{+\infty} x(i) h(n-i) = \sum_{i=-\infty}^{+\infty} x(n-i) h(i)

Continuous Convolution Definition:

Assuming two continuous functions x(t)x(t) and h(t)h(t), the convolution result is:

(xh)(t)=x(t)h(t)=+x(τ)h(tτ)dτ=+x(tτ)h(τ)dτ(x * h)(t) = x(t) * h(t) = \int_{-\infty}^{+\infty} x(\tau) h(t-\tau)d\tau = \int_{-\infty}^{+\infty} x(t-\tau) h(\tau)d\tau

Explanation and Understanding

Convolution is commonly applied to signals where ‘x’ represents the signal and ‘h’ is the convolution kernel, usually shorter than the signal.

Discrete Convolution: If both the signal ‘x’ and the convolution kernel ‘h’ are discrete, convolution can be understood as ‘h’ sliding along ‘x’, taking a set of data points equal to ‘h’ and performing point-wise multiplication, summing up the results as the first convolution output. The convolution kernel ‘h’ then slides to the right by one unit, repeating the process to obtain subsequent convolution results until the signal ‘x’ is completely traversed.

Continuous Convolution: If both the signal ‘x’ and the convolution kernel ‘h’ are continuous, convolution can be visualized as the overlap between ‘x’ and ‘h’, with the area of overlap representing the first convolution result. The convolution kernel ‘h’ then slides along the signal ‘x’, repeating the process to obtain subsequent convolution results until the signal ‘x’ is entirely covered.

In theory, convolution occurs from negative infinity to positive infinity, but in practice, only non-zero sections of the signal ‘x’ are considered. The length of the resulting convolution might require manual selection, although MATLAB and Python offer options for the convolution result’s length. Padding the signal’s ends with zeros ensures full coverage by the kernel. Post-convolution scaling may alter the amplitude, and to compare with the original signal amplitude, dividing the convolution result by the kernel’s length suffices. Odd-length kernels are typically recommended.

Code

Matlab Code

1
2
y = conv(u, h) % Equivalent to conv(h, u)
y = conv(u, h, 'same') % Options: full, same, valid - see official documentation for usage

Python Code

1
2
y = np.convolve(u, h) # Equivalent to conv(h, u)
y = np.convolve(u, h, 'same') # Options: full, same, valid - see official documentation for usage

! Exercise

Calculate the moving average of a signal using a kernel of length 11.

1
2
3
x = cumsum(randn(1,1000))
h = ones(1,11)
MA = conv(x, h, 'same')/11
1
2
3
4
5
import numpy as np

x = np.cumsum(np.random.randn(1, 1000))
h = np.ones(11)
MA = np.convolve(x, h, mode='same') / 11

Note: The content in this blog is class notes shared for educational purposes only. Some images and content are sourced from textbooks, teacher materials, and the internet. If there is any infringement, please contact aursus.blog@gmail.com for removal.