Convolution - Rectangular Kernel

 

Convolution would be the one of the most important operations in engineering math. If you don't have any prior study and understanding on how Convolution works, it would not be easy to make sense out of it. For those who is not familiar with Convolution, I would suggest you to go through my descriptive note here.

 

The operation in this tutorial can be illustrated as follows. The value at each and every point in the covoluted plot (the bottom plot) is calculated by this operation (i.e, element by element multiplication and summation).

 

The process goes as follows :

  • Data (Step 1): Shows the input signal or data that will be processed. This is usually a time series or spatial data represented in discrete intervals.
  • Kernel (Step 2): Represents the kernel (also known as a filter), which is a small matrix used to highlight certain features in the input data, such as edges in image processing or specific frequencies in signal processing.
  • Flip (Step 3): Illustrates the flipping of the kernel, which is a necessary step in the convolution operation. Convolution requires the kernel to be flipped both horizontally and vertically before starting the process of sliding over the input data.
  • Convolution Process (Step 4): This part of the image shows the kernel being slid across the input data (this operation is known as the sliding dot product). At each position, the kernel's values are multiplied with the overlapping values in the data, and the results are summed up to form a single output value. The equation represents the summing process, where f[m] is the data and g[n−m] represents the flipped kernel.
  • Correlation Result (Step 5): Once the multiplication and summing are done for a particular position, the result is drawn as a bar. This visual bar represents the strength of the correlation between the kernel and that specific part of the input data at that position.
  • Goto Step 4 (Step 6) : Shift the flipped kernel one step to the right and go to Step 4.

 

 

Followings are the code that I wrote in Octave to creates all the plots shown in this page. You may copy these code and play with these codes. Change variables and try yourself until you get your own intuitive understanding.

 

< Code 1 >

 

x = [0 0.4 0.4 0.4 0.4 0.4 0 0 0];

 

sf = 11; % samples per bit

 

p_x = x;

 

if sf > 1

   for i = 2:sf

     p_x= [p_x ; x];

   end

   p_x = reshape(p_x,[],1);

 

end

 

chan = 0.3*[0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0];

%chan(1) = 1;

%chan(30) = 0.5;

 

cs = 123;

 

hFig = figure(1,'Position',[300 300 700 600]);

 

subplot(4,2,1);

stem(p_x,'MarkerFaceColor',[0 0 1],'color','blue');

axis([1 length(p_x) -1.0 1.0]);

title("data");

box on;

set(gca,'xticklabel',[]);

set(gca,'yticklabel',[]);

set(gca,'xtick',[]);

set(gca,'ytick',[]);

 

subplot(4,2,2);

stem(chan,'MarkerFaceColor',[1 0 0],'MarkerEdgeColor',[1 0 0],'color','red');

axis([1 length(p_x) -1.0 1.0]);

title("kernel");

box on;

set(gca,'xticklabel',[]);

set(gca,'yticklabel',[]);

set(gca,'xtick',[]);

set(gca,'ytick',[]);

 

subplot(4,2,[3 8]);

hold on;

s = p_x;

for i = 0:100

    if i < length(p_x)

       line([i i],[5 5+s(i+1)],'color','blue');

       plot(i,5+s(i+1),'bo');

    end;   

end;

line([-40 140],[5 5],'color','green');

 

k = flip(chan);

ks = length(k);

 

for i = 0:100

    if i < length(k)

       line([i-ks+cs i-ks+cs],[3 3+k(i+1)],'color','red');

       plot(i-ks+cs,3+k(i+1),'ro');

    end;   

end;  

line([-40 140],[3 3],'color','green');

 

y = conv(p_x(1:min([length(p_x) cs])),chan(1:min([length(chan) cs])));

y = y(1:cs);

for i = 0:200

    if i < length(y)

       line([i i],[1 1+y(i+1)],'color','black');

       plot(i,1+y(i+1),'ko');

    end;   

end;

line([-40 140],[1 1],'color','green');

 

rdx = min(cs,length(k));

rdy = 3.0;

rx = cs-rdx;

ry = 2.9;

rectangle('Position',[rx ry rdx rdy]);

 

line([cs cs],[1 1+y(end)],'color','red','LineWidth',3, 'Marker','o');

line([cs cs],[1 1+2],'color','red','LineWidth',0.5);

 

hold off;

axis([-50 150 0 6]);

tStr = sprintf("step = %d",cs);

title(tStr);

box on;

set(gca,'xticklabel',[]);

set(gca,'yticklabel',[]);

set(gca,'xtick',[]);

set(gca,'ytick',[]);