Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY

SCHOOL OF ELECTRONICS AND TELECOMMUNICATION

MIDTERM PROJECT
DIGITAL COMMUNICATION

Topic:
Convolution and Cross-Correlation
Simulation Using Matlab and IC 8501

Students: VU HOANG LONG 20182926


NGUYEN LE TRUNG 20186076
PHAM DINH DOANH 20182910
PHAM HUY HOANG 20182544
TRUONG LONG BINH 20182906
EUGENIO LUIS UBISSE 20180293
Instructor: PROF.DR. NGUYEN VAN DUC

Hanoi, May 2, 2021


Team 8&9 Midterm Project

2
Convolution and Cross-Correlation Simulation
Using Matlab and 8501
Hoang-Long.Vu, Dinh-Doanh.Pham, Le-Trung.Nguyen,
Long-Binh.Truong, Huy-Hoang.Pham, Eugenio Luis Ubisse
May 2, 2021

3
Team 8&9 Midterm Project

List of Figures

List of Figures
1 Signal Through Multi-path Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Convolution raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Convolution maintain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Convolution drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Convolution off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6 Cross-Correlation raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7 Cross-Correlation maintain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
8 Cross-Correlation drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
9 Cross-Correlation off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10 Continuous Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
11 Convolution at tth time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
12 Convolution’s Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
13 Correlation’s Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
14 Convolution and Cross-Correlation Simulation . . . . . . . . . . . . . . . . . . . . . . . . . 19
15 Convolution and Cross-Correlation Result . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
16 Schematic of connecting LCD to 8051 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
17 Convolution within 8501 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
18 Correlation within 8501 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

4
Midterm Project Team 8&9

Contents

I Introduction 7
1 Problem 7

2 Solution 7

II Theoretical Basis 8
3 Convolution 8
3.1 Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

4 Cross-Correlation 10
4.1 Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.2 Illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

III Coding and Simulation 13


5 Calculation in C++ 13
5.1 Coding and Executing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.1.1 Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.1.2 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.1.3 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.4 Including Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.2 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

6 Simulation in Matlab 17
6.1 Coding and Executing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

7 Installation in 8501 20
7.1 Coding and Executing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7.2 Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

IV Conclusion 24

V Distribution 25
8 Coding and Debugging 25
8.1 C++: Le-Trung.Nguyen, Dinh-Doanh.Pham . . . . . . . . . . . . . . . . . . . . . . . . . . 25
8.2 Matlab: Le-Trung.Nguyen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
8.3 8051: Code: Dinh-Doanh.Pham; Debug: Huy-Hoang.Pham . . . . . . . . . . . . . . . . . 25

9 Theory and Report 25


9.1 First Forming Theory and Composing: Hoang-Long.Vu . . . . . . . . . . . . . . . . . . . 25
9.2 Proofreading and Cover: Long-Binh.Truong . . . . . . . . . . . . . . . . . . . . . . . . . . 25

10 Slides: Long-Binh.Truong, Huy-Hoang.Pham, Hoang-Long.Vu, Eugenio Luis Ubisse 25

11 Presentation: Hoang-Long.Vu, Le-Trung.Nguyen, Huy-Hoang.Pham, Dinh-Doanh.Pham 25

5
Team 8&9 Midterm Project

ABSTRACT
Nowadays, both Convolution and Cross-Correlation play an very important role in the study
of Digital Signal Processing of Digital Communication. Being aware of the huge potential of those
concepts, we tried our best to make a clearly and visual simulation of those theories, also their in-
stallation throughout the microcontroller IC 8501, to be profoundly familiar with them. Accordingly,
we determined this topic as the study of our midterm project.
Associated with the project, this report covers almost the content of our project, so that helps
you to have a visual overview of what we did, also understand more about convolution and Cross-
Correlation. Categorically, this report has four main parts: Part I given the difficulty while simulating
those concepts and the way we will solve it; Part II covers the theoretical basis; Part III reveals our
process; Part IV gives the conclusion of our study.
Due to the limitation of time, we can not guaranteed the optimization of our algorithm, also the
customizablity of what we see and the range of input and output data. Anyway, we hope our project
contribute the enormous effort to help students on the way they try to understand those concepts,
furtherly help handling signals in real life.
We are very pleasure if you concern about our topic and help us to make it be better. If you have
any feedback, please contact us. We appreciate your contribution.
Authors,
Team 8&9.

6
Midterm Project Team 8&9

Part I
Introduction
1 Problem
While we approach with the concept convolution, most of us know how to calculate it but do not
know how it actually works and what it is used for, and the same with the concept Cross-Correlation.
Therefore, what we need is a visual simulation of convolution and Cross-Correlation, also its installation
via a microcontroller IC so that we can deeply understand what they are, also their application in real
life problem, particularly the subject Signal Processing.
The difficulty here is that there is no available detailed instruction for us to simulate this calculation
step by step via a programming language. Throughout this project, we will introduce our approach to
this problem in detail, so that you will have a visual overview of those concepts, and help us to improve
our results.

2 Solution
To deal with this problem, we propose a simulation system based on the combination of different
expansion versions of the programming language C: C++, Matlab and C on 8501 microcontroller IC,
where C++ is used for obtaining the result, Matlab is for simulation and 8501 helps to apply this
algorithm.

7
Team 8&9 Midterm Project

Part II
Theoretical Basis
3 Convolution
In mathematics (in particular, functional analysis), convolution is a mathematical operation on two
functions (f and g) that produces a third function f ∗ g that expresses how the shape of one is modified
by the other. The term convolution refers to both the result function and to the process of computing
it. It is defined as the integral of the product of the two functions after one is reversed and shifted. The
integral is evaluated for all values of shift, producing the convolution function1 .
In digital signal processing and electrical engineering the term convolution shows and illustration of
how the signals are affected by the systems and transmission lines. Particularly, the convolution of the
input signals with the impulse response (as the convolution of two functions) derives the output of a linear
time-invariant (LTI) system. Visually, the impulse response represents for the handling of the system,
the noise of the transmission line, the distortion of the spectra, etc.

Figure 1: Signal Through Multi-path Channel

By the definition of convolution, it is illustrated that all the output signal at any given point of time
is an accumulated effect of all the prior values of the input function, with the most recent values typically
having the most influence (expressed as a multiplicative factor). The impulse response function provides
that factor as a function of the elapsed time since each input value occurred. 2

3.1 Formula
The convolution operator of two functions (f and g) is denote as f ∗ g, is the integral of the product
of the two functions after one is reversed and shifted.
Z ∞
(1) (f ∗ g)(t) , f (τ )g(t − τ )dτ.
−∞

Within the range of our project, we almost use the definition of Discrete Convolution for our algorithm,
due to the behavior of every digital system that considers continuous signal as discrete signal

X
(2) (f ∗ g)[n] = f [m]g[n − m].
m=−∞

or equivalently,

X
(3) (f ∗ g)[n] = f [n − m]g[m].
m=−∞

1 wikipedia
2 wikipedia

8
Midterm Project Team 8&9

Associated with the possibility of the calculation, our systems works with two finite sequences:

M
X
(4) (f ∗ g)[n] = f [n − m]g[m].
m=−M

3.2 Illustration
When the triangle pulse begins to cross the rectangle one, their convolution starts to raise:

Figure 2: Convolution raise

When the triangle is totally inside the rectangle one, their convolution maintains constant:

Figure 3: Convolution maintain

When the triangle starts to come over the rectangle one, their convolution begins to drop:

Figure 4: Convolution drop

9
Team 8&9 Midterm Project

When the triangle totally escapes the rectangle one, their convolution remains zero:

Figure 5: Convolution off

4 Cross-Correlation

The term Cross-Correlation is simply referred to the differences between two handled signals. In other
words, it it the measure of similarity of two series as a function of the displacement of one relative to the
other3 .
The calculation of Cross-Correlation is almost like how to do with Convolution, the differences is just
that we do not reverse the second signal.

4.1 Formula

Continuous Cross-Correlation:
Z ∞
(5) (f ∗ g)(τ ) , f (t)g(t + τ )dt.
−∞

which is equivalent to

Z ∞
(6) (f ∗ g)(τ ) , f (t − τ )g(t)dt.
−∞

Discrete Cross-Correlation:


X
(7) (f ∗ g)[n] , f [m]g[m + n].
m=−∞

which is equivalent to


X
(8) (f ∗ g)[n] , f [m − n]g[m].
m=−∞

3 wikipedia

10
Midterm Project Team 8&9

4.2 Illustration

When the triangle pulse begins to cross the rectangle one, their Cross-Correlation starts to raise:

Figure 6: Cross-Correlation raise

When the triangle is totally inside the rectangle one, their Cross-Correlation maintains constant:

Figure 7: Cross-Correlation maintain

When the triangle starts to come over the rectangle one, their Cross-Correlation begins to drop:

Figure 8: Cross-Correlation drop

11
Team 8&9 Midterm Project

When the triangle totally escapes the rectangle one, their Cross-Correlation remains zero:

Figure 9: Cross-Correlation off

Completely, we got the final figure of both convolution and Cross-Correlation:

Figure 10: Continuous Convolution

12
Midterm Project Team 8&9

Part III
Coding and Simulation
5 Calculation in C++
5.1 Coding and Executing
5.1.1 Importing and Exporting Data
First and foremost, we have to declare and define a class named signal contains an array of signal’s
element, represented as vector<double> type, also its root’s position. The class also contains the defined
method for importing and exporting data, named inputArray() and print():

class signal {
public:
vector<double> array;
int rootPosition;
public:
void inputArray() {...}
void print() {...}
};

The input data has the form {a1 (a2) a3 a4...} where (a2) is the root. Therefore we need to define the
data importing function as below:

void inputArray() {
string c;
cout << "Input: "; getline(cin, c);
string tmp = ""; int count = 0;

if (c[0] >= 48 && c[0] <= 57) {


count++;
tmp = tmp + c[0];
}
if (c[0] == ’(’) rootPosition = count;

for (int i = 1; i < c.size(); i++) {


if (c[i] >= 48 && c[i] <= 57 && (c[i - 1] < 48 || c[i - 1] > 57)) count++;
if (c[i] == ’(’) {
rootPosition = count;
tmp = tmp + " ";
continue;
}
if (c[i] == ’)’) continue;
tmp = tmp + c[i];
}
stringstream temp(tmp);
double n;
while (temp >> n) array.push_back(n);
}

Accordingly, the exporting data function is defined as the following code:

void print() {
for (int i = 0; i < array.size(); i++) {
if (i == rootPosition) {
cout << "(" << array[i] << ") ";
continue;
}
cout << array[i] << " ";
}
cout << endl;
}

13
Team 8&9 Midterm Project

5.1.2 Algorithm
To calculate the convolution or cross-correlation, we will rely on the definition as mentioned above.
However, it seems so complex to derive the algorithm if we consider two length-different signals. Accord-
ingly, we firstly normalize the two input signals by using the function sameSize() defined below:

void sameSize(signal* A, signal* B) {


if ((*A).array.size() > (*B).array.size()) {
for (int i = (*B).array.size(); i < (*A).array.size(); i++) {
(*B).array.push_back(0);
}
return;
}
if ((*B).array.size() > (*A).array.size()) {
for (int i = (*A).array.size(); i < (*B).array.size(); i++) {
(*A).array.push_back(0);
}
return;
}

Now we have two normalized signals. To calculate their convolution, we will ”slide” each other together
and obtain the sum of product each time we slide.
For example, at tth time of sliding we get the position of signal A and signal B below:

Figure 11: Convolution at tth time

For this above example, we get (A ∗ B)(t) = A4 B7 + A5 B6 + A6 B5 + A7 B4


There is a fact that, the length of convolution of two discrete signals equals to the sum of the length
of those input signals plus one. Therefore, if we denote the length of the convolution is l, we will ”slide”
the two signals l times.
The root of the result has the position of the sum of the root’s position of two input signals.
Then we define the convolution calculation function:

signal convolutionCal(signal A, signal B) {


sameSize(&A, &B);
signal C; double tmp;
for (int i = 0; i < A.array.size() + B.array.size() + 1; i++) {
tmp = 0;
for (int j = 0; j < B.array.size(); j++) {
tmp += (j < A.array.size() ? A.array[j] : 0) * (i - j < B.array.size() ?
B.array[i - j] : 0);
}
C.array.push_back(tmp);
}
C.rootPosition = A.rootPosition + B.rootPosition;
return C;
}

For calculating the Cross-Correlation of two signals, we do almost the same things, the only difference
is that we have to reverse the second signal, so we use function reverse() from library algorithm to handle
this stuff.

14
Midterm Project Team 8&9

Consequently we derive the source code for Cross-Correlation calculation:

signal correlationCal(signal A, signal B) {


reverse(B.array.begin(), B.array.end());
sameSize(&A, &B);
signal C; double tmp;
for (int i = 0; i < A.array.size() + B.array.size() + 1; i++) {
tmp = 0;
for (int j = 0; j < B.array.size(); j++) {
tmp += (j < A.array.size() ? A.array[j] : 0) * (i - j < B.array.size() ?
B.array[i - j] : 0);
}
C.array.push_back(tmp);
}
C.rootPosition = A.rootPosition + B.array.size() - B.rootPosition - 1;
return C;
}

5.1.3 Plotting

To plot the output signal as the representation of impulse response, we use extended libraries as
mention in the next below section.
Firstly, we have to define a function to draw the individual pulse:

void pulseDraw(vector<double> x, vector<double> y, string str = "plot.png") {


vector<double> X;
vector<double> Y;
for (int j = 0; j < x.size(); j++) {
double tmpy = 0; double tmpx = x[j] - 0.001;
for (int i = 0; i < 10001; i++) {
//cho x chay tu x - 0.000001 den x + 0.000001 voi 10000 gia tri
tmpx += 0.0000000002;
X.push_back(tmpx);
//cho y chay tu 0 den y roi lai ve 0 voi 50 gia tri
if (i < 5000) tmpy += (y[j] / 5000);
if (i >= 5000) tmpy -= (y[j] / 5000);
Y.push_back(tmpy);
}
}

RGBABitmapImageReference* imageRef = CreateRGBABitmapImageReference();


RGBA* a = CreateRGBAColor(255, 0, 0, 1);

DrawScatterPlot(imageRef, 600, 400, &X, &Y);

vector<double>* pngDate = ConvertToPNG(imageRef->image);


WriteToFile(pngDate, str);
DeleteImage(imageRef->image);
}

Then we define a function to plot the entire signal:

void plot(signal A, string str) {


vector<double> x(A.array.size());
for (int i = 0; i < x.size(); i++) {
x[i] = -A.rootPosition;
A.rootPosition--;
}
pulseDraw(x, A.array, str);
}

The plotted results will be shown in the Result section.

15
Team 8&9 Midterm Project

5.1.4 Including Libraries


The source code can not be compiled without the libraries of corresponding function.

#include<iostream>
#include<vector>
#include<sstream>
#include<string>
#include<algorithm>
#include "pbPlots.hpp"
#include "supportLib.hpp"

where,
• the first four ones is for data importing and exporting purpose
• the fifth one is for calling the function reverse() as mention above.
• the last two libraries is for plotting graph.

5.2 Result
Running the program with the following two input signals:
. a(x) = {1, 4̄, −2, 3, 6, 4, −2}
. ¯ 6, 4, 3, −1, 0, 2}
b(x) = {2, −4, −3,
We obtain the below results:

Figure 12: Convolution’s Result

Figure 13: Correlation’s Result

16
Midterm Project Team 8&9

6 Simulation in Matlab
6.1 Coding and Executing
Throughout the above part, we have known about how to programming a tool to calculate the
convolution and Cross-Correlation, also how to draw the signal impulses. Accordingly, we want to use
those obtained results to simulate the process of convolution and Cross-Correlation. It means, how the
signals combines together through each interval of time. Therefore, we need an animation, also the most
difficult part when we tried to simulate those operations.
To deal with this complication, we divide the issue into small sub-problems. First and foremost, we
need to transfer our discrete input signals into continuous signals. The term continuous means the set
of many discrete points within small interval, depend on the sampling frequency. Those discrete points
have the magnitude following the pulse of the original inputs (like the triangle as illustrated below in the
section Result). Sequentially, we draw the two input signals as the continuous signals with their pulse
using the results obtained from the above process. Having those sampled signals, we accomplish the last
step by drawing the results, also the animation of how signals blended together.

Create Pulse
We assume that the sampling frequency fs is given (fS = 200). To sampling the signal’s pulse, we
have to determine the pulse width using The Nyquist Sampling Theory:
A bandpass signal having no spectral components below fL Hz or above fH Hz is specified uniquely by its
values at uniform intervals spaced Ts = 1/fs s apart provided that:
   
Q Q−1
(9) 2B ≤ fS ≤ 2B
N N −1

where B = fH − fL , Q = fH /B, n is a positive integer and n ≤ Q


In our particular problem, we consider that 0.075 is the suitable value for pulse width that satisfies
the above condition. Accordingly, within the give input signals we define the pulse creating function:

function X = pulseCreate_v2(input_signal, t, root)


X = 0;
Ts = 0.075
for i = 1:length(input_signal)
X = X + input_signal(i)*rectpuls(t-(i-1)+(root-1),Ts);
end
end

which helps to transfer the signal to a function of unit impulse δ(n), with the pulse width equals to 0.075
as above
Using this above function, we obtain the following sampled continuous signals:

Y1 = pulseCreate_v2(y1,t,root1);
Y2 = pulseCreate_v2(y2,t,root2);

where t plays the roles as the limitation of points (number, upper & lower bound) of the signal we sample:

fs = 200;
MAX = max(length(y1), length(y2));
MIN = min(length(y1), length(y2));
t = linspace(-MAX, 2*MAX, fs);

Display Signal
After deriving the continuous signals Y 1 and Y 2, we plot those signals and display them:

subplot(4,2,1); plot(t, Y1, ’Color’,’blue’,’LineWidth’,2), axis(ax1); grid on;


xlabel(’t’); ylabel(’y1(t)’);
subplot(4,2,2); plot(t, Y2, ’Color’,’red’,’LineWidth’,2), axis(ax2); grid on;
xlabel(’t’); ylabel(’y2(t)’);

17
Team 8&9 Midterm Project

Where axis ∗ () component helps to format the displayed figure’s window:

ax1 = [-root1, length(y1)-root1+1,bottom_limit(y1),top_limit(y1)];


ax2 = [-root2, length(y2)-root2+1,bottom_limit(y2),top_limit(y2)];

Calculate and Plot Convolution and Cross-Correlation

To illustrate the animation as the requirement of simulation, we have to draw the signal point by
point through each interval of time while using the command pause(0.01) for gradually displaying the
graph.
Firstly, we calculate the convolution (Cross-Correlation symmetrically) at each interval of time (we
denote as i, i = 1:length(t))

moveStep = ((length(y2)+ length(y2))*(2*i-fs)/fs) ;


y2_shifted = pulseCreate(y2, -t + moveStep);
convolution(i) = trapz(Y1.*y2_shifted);

Having the convolution result at each interval of time, we plot the graph of the Result window as a
function of time (referred as t). Simultaneously, we plot the convolution process on the Process window
by consider the shifted variable as a function of time.

%Process
subplot(4,2,3:4)
hold off;
plot(t,Y1,’Color’,’blue’,’LineWidth’,2),axis(ax3);
hold on;
plot(t,y2_shifted,’Color’,’red’ ,’LineWidth’,2),axis(ax3);
grid on;
xlabel(’t’);

%Result
subplot(4,2,7:8)
hold off
plot(t(1:i),convolution(1:i),’Color’,’black’,’LineWidth’,2);set(gca,’xtick’,[]);
ylim([bottom_limit(convv), top_limit(convv)]);
grid on;
xlabel(’t’)
ylabel(’Convolution(y1, y2)(t) ’)

All the above steps were executed i times in a loop, where i = 1:length(t), while we consider i as the
interval of time between two adjacent continuous signal’s elements.
We do the same process for the Cross-Correlation case.

Show off the results

After simulation, the calculation results will be displayed on the terminal window and save to output
files to compare with the figure, also for further purpose of installation in microcontroller IC 8501.

fprintf(’Convolution result is: ’);


for i = 1:length(convv)
if i == root1+root2-1
fprintf(’ (%d) ’, convv(i));
continue;
end
fprintf(’ %d ’, convv(i));
end

The same code with the Cross-Correlation.


Generally we have to emphasize that, the length of the input signals, the value of fS , the width of
rectangle pulse, the convolution time t are related together, so we can calculate any parameter if other
parameters are given.

18
Midterm Project Team 8&9

6.2 Result
Running the program with the following two input signals:
. a(x) = {1, 2̄, 3, 4}
. ¯ 1, 6, 0, 7}
b(x) = {−2,
We obtain the below results:

Figure 14: Convolution and Cross-Correlation Simulation

where the Convolution result and Correlation result is shown in the command window:

Figure 15: Convolution and Cross-Correlation Result

19
Team 8&9 Midterm Project

7 Installation in 8501
7.1 Coding and Executing
We have calculated and simulated the operation of convolution and Cross-Correlation. Accordingly,
we also obtain the results of those processes. Now we use those result to do the installation within the
microcontroller IC 8501.
However, to accomplish a completed version of the installation in 8501, we integrate all the algorithm
of how we calculate the convolution (the same to the Cross-Correlation) to its source code.

int* result_conv(int* a, int size_a, int root_a, int *b, int size_b, int root_b){
int q; int p;
int aa[2*size_a];
int bb[2*size_b];
int c[size_a + size_b + 1];
for (q = 0; q < size_a*2; q++){
if(q < size_a){
aa[q]=a[q];
}
else aa[q] = 0;
}
for (p = 0; p < size_b*2; p++){
if(p < size_b){
bb[p]=b[p];
}
else bb[p] = 0;
}
c[1] = size_a + size_b + 1;
c[0] = (root_a + root_b);
for (q = 2; q < size_a+size_b+1; q++){
c[q] = 0;
for(p = 0;p < size_a; p++){
if(q-p-2 >= 0){
c[q]= c[q] + aa[p] * bb[q-p-2];
}
}
}
free(aa);
free(bb);
return c+2;
}

The main purpose is displaying the results (output signals) on the display LCD1602. Therefore, firstly
we have to initialize the LCD display:

void Lcd_Send_Command(unsigned char cmd) //send command to LCD


{
LCD_RW =0;// Mode write, not read
LCD_RS =0;// reg Instruction, not Data
LCD_DATA =cmd;
LCD_EN = 0; LCD_EN = 1;
if(cmd<=0x02){
Delay_ms(3);
} else Delay_ms(2);
}
void Lcd_Init() //khoi tao LCD
{
Lcd_Send_Command(0x30);
Delay_ms(5);
Lcd_Send_Command(0x30);
Delay_ms(1);
Lcd_Send_Command(0x30);
Lcd_Send_Command(0x38); // 2 line, font 5x8
Lcd_Send_Command(0x01); // clear LCD

20
Midterm Project Team 8&9

Lcd_Send_Command(0x0E);//you can also display and turn on pointer by 0x0E


Lcd_Send_Command(0x06);//auto shift pointer to right after printf a letter
}

To modify what the screen displays, we have to define the pointer navigating function and some special
useful character (digits, space, open/close parentheses)

void Lcd_Send_Letter(char c)// display a character


{
LCD_RW =0;
LCD_RS =1; // reg of Data, not instruction
LCD_DATA =c;
LCD_EN = 0;
LCD_EN =1;
Delay_ms(1);
}

We give the same definition for other following functions:

void Lcd_Space_Letter();//display space character


void Lcd_Ngoac_Open();//display open parentheses
void Lcd_Ngoac_Close();//display close parentheses

just by replacing c by 0x20, 0x28, 0x29 correspondingly.

Now we have all the essential tools to show the results. Here is the source code of convolution
displaying, the same to Cross-Correlation:

void convolution()
{
int* c = result_conv();
//int root = 0;// root of the array, mean 0-position
TMOD = 0x01;//delete all content in previous execution
Lcd_Init();
Lcd_Goto(0,0);// go to first position
Delay_ms(20);

for (i=0;i<(c[-1]-2);i++){
if(i== c[-2]){
Lcd_Ngoac_Open();
}
sprintf(buffer, "%d", c[i]);//convert int to string
Lcd_Send_String(buffer);//make lcd print the number
if(i== c[-2]){
Lcd_Ngoac_Close();
}
Lcd_Space_Letter();//print space to make number be clear
}
Delay_ms(1000);
Lcd_Goto(1,0);
for (i=0;i<(c[-1]-2)*2;i++){
Lcd_Send_Command(0x18);//shift content to right
Delay_ms(650);
}

free(c);
}

We have almost done. While showing the results on the display, we consider a more convenient way to
distinguish the two individual results: we install an external led, which shows ON-state when the screen
illustrate the convolution, otherwise, it is OFF. To do that, in the main function we denote an output

21
Team 8&9 Midterm Project

pin (referred as P1 1) which is connected to a LED, of which value is 1 when the function convolution()
is called, otherwise it is 0.

int main(){
P1_1 = 1;
while(1){
P1_1 = 1;
convolution();
P1_1 =0;
correlation();
}
}

7.2 Schematic
After compiling and debugging the code and upload it to the microcontroller 8051, now we move to
the design section

Figure 16: Schematic of connecting LCD to 8051

22
Midterm Project Team 8&9

7.3 Result
Running the program with the following two input signals:
. a(x) = {3, 4̄, −1, 2}
. b(x) = {1, 2̄, 3}
We obtain the below results:

Figure 17: Convolution within 8501

Figure 18: Correlation within 8501

23
Team 8&9 Midterm Project

Part IV
Conclusion
Overall, throughout the working process, we see that while comparing three types of programming
language we have used: C, C++ and Matlab, C can be used for microcontrollers, meanwhile C++ is
easy to be familiar with and coding but not convenient for simulating purpose, which could be solved by
Matlab, that supports well for working with mathematics and graph.
We have worked hard and achieve a desirable accomplishment. Our Calculator and Simulator work
properly with all the test cases we have tried. Moreover, throughout this assignment, we have gained a
deeper knowledge about those concept. Anyway, nothing is perfect, perhaps our performance is not the
most optimized way against this topic. Nevertheless, we hope our project contributes a significant part
to the Digital Signal Process Branch, also helps student shorten their approach with Convolution and
Cross-Correlation.
The process of learning and learning how to build a system, a term of simulation that seems easy
but very complex and a feat to achieve professionalism. However, under the guidance of Prof. Nguyen
Van Duc, our team has learned the steps to create an economical project with practical application in
the most effective way. Along with that, teamwork also helps us to perfect more skills such as arranging
time, assigning work to each member, using software for teamwork, ... Our team sincerely thanks for
the direction Very practical and straightforward instructions and suggestions from teachers during class
presentations, through which we have a more accurate and clear orientation on the way to build products
that meet market needs and have practical application. Due to the limited time, the simulation we build
is still flawed, we look forward to having your feedback on the product so that we can try harder in the
next projects. Once again, my group would like to thank you very much.
Thank you!

24
Midterm Project Team 8&9

Part V
Distribution
8 Coding and Debugging
8.1 C++: Le-Trung.Nguyen, Dinh-Doanh.Pham
8.2 Matlab: Le-Trung.Nguyen
8.3 8051: Code: Dinh-Doanh.Pham; Debug: Huy-Hoang.Pham

9 Theory and Report


9.1 First Forming Theory and Composing: Hoang-Long.Vu
9.2 Proofreading and Cover: Long-Binh.Truong

10 Slides: Long-Binh.Truong, Huy-Hoang.Pham, Hoang-Long.Vu,


Eugenio Luis Ubisse
11 Presentation: Hoang-Long.Vu, Le-Trung.Nguyen, Huy-Hoang.Pham,
Dinh-Doanh.Pham

25
Team 8&9 Midterm Project

Reference
1. https://en.wikipedia.org/wiki/Convolution
2. https://en.wikipedia.org/wiki/correlation and dependence
3. https://www.tutorialspoint.com/matlab/index.htm
4. https://github.com/InductiveComputerScience/pbPlots https://deepbluembedded.com/8501-lcd-16x2-
tutorial-library-alphanumeric-lcd-16x2-interfacing/
5. Ian A. Glover and Peter M. Grant, Digital Communications, Prentice Hall, 2009.
6. Allan V. Oppenheim, Signals and Systems, Prentice Hall, 1997.

26

You might also like