You are on page 1of 18

Reconfigurable Computing Assignment

Sorting based on Euclidean distance from centroid

Submitted by-
Aakash Guwalani – 2021H1230235H
Arpit Goel– 2021H1230219H
Kalicheti Vishnuvardhan Reddy – 2021H1230221H
Mimanshu Singh – 2021H1230207H
Submitted to-
Mr. Karri Babu Ravi Teja (Assistant Prof. BITS Pilani)
ABSTRACT

In coordinate geometry, Euclidean distance is the distance between two points. To find the two points on a
plane, the length of a segment connecting the two points is measured. We derive the Euclidean distance
formula using the Pythagoras theorem.

What Is Euclidean Distance Formula?

The Euclidean distance formula, as its name suggests, gives the distance between two points (or) the straight
line distance. Let us assume that (x1,y1)(x1,y1) and (x2,y2)(x2,y2) are two points in a two-dimensional plane.
Here is the Euclidean distance formula.

Euclidean Distance Formula

The Euclidean distance formula says:

d = √[ (x22 – x11)2 + (y22 – y11)2]

where,

 (x11, y11) are the coordinates of one point.


 (x22, y22) are the coordinates of the other point.
 d is the distance between (x11, y11) and (x22, y22).
The aim of this assignment is to sort 32 data points in an XY plane represented using the cartesian coordinate
system. All the points fall within or on the square region bounded by the straight lines |x|=1 and |y|=1.The
sorting is to be done based on the metric of Euclidean distance of a point from the centroid of all the points.
The expression used for calculation of centroid (xc,yc) for N data points (xi,yi) i=1,2, ... ,N is given by

Here we are using half precision floating point notation for data point representation.

Applications:

Distance measures play an important role in machine learning.


They provide the foundation for many popular and effective machine learning algorithms like k-nearest
neighbour for supervised learning and k-means clustering for unsupervised learning.

The k-means clustering algorithm mainly performs two tasks:

o Determines the best value for K center points or centroids by an iterative process.
o Assigns each data point to its closest k-center. Those data points which are near to the particular k-
center, create a cluster.

How does the K-Means Algorithm Work?


The working of the K-Means algorithm is explained in the below steps:
Step-1: Select the number K to decide the number of clusters.
Step-2: Select random K points or centroids. (It can be other from the input dataset).
Step-3: Assign each data point to their closest centroid, which will form the predefined K clusters.
Step-4: Calculate the variance and place a new centroid of each cluster.
Step-5: Repeat the third steps, which means reassign each datapoint to the new closest centroid of each cluster.
Step-6: If any reassignment occurs, then go to step-4 else go to FINISH.
Step-7: The model is ready.
FICr. 1. Floivchai’t foi’ K-means cliistei’iug.
VERIFICATION OF OUTPUT IN HIGH LEVEL LANGUAGE

(C LANGUAGE)

 C Program for Decimal to floating point conversion.


 C program for calculating Euclidean distance.

Conversion of Decimal Number System to Half Precision (16 bit)

1. Algorithm-
 Convert decimal number to binary representation.
 Shifting binary number for mantissa calculation.
 Exponent calculation by modulus division.
 Inserting zero at remaining bits.

2. Code-

#include <stdio.h>
int
main (void)
{
float num,n1;
printf ("enter number=");
scanf ("%f", &n1);
if(n1<0)
num = -n1;
int i, j, bin, z, b[100], a[100];
j = 0;
bin = 0; //initial binary number =0
// binary calculation
for (i = 0; i < 4; i++)
{
num = num * 2;
j = num;
bin = bin * 10 + j;
if (bin == 0)
z = z + 1; //for shifting of bits.
if (j == 1)
num = num - 1;
}
// exponent part calculation.
z = 63 + z;
for (i = 0; i <= 5; i++)
{
a[i] = z % 2;
z = z / 2;
}
if (n1 >= 0) // for positive number represntation
{
printf ("0");
for (i = 0; i <= 5; i++)
{
printf ("%d", a[i]);
}
printf ("%d", bin);
for (int i = 0; i < 6 - j; i++)
printf ("%d", 0);
}
else //negative number
{
printf ("1");
for (i = 0; i <= 5; i++)
{
printf ("%d", a[i]);
}
printf ("%d", bin);
for (int i = 0; i < 6 - j; i++)
printf ("%d", 0);
}
3. Output: -
EUCLIDEAN DISTANCE CALCULATION

1. Algorithm (for Euclidean distance)-


 Taking n number of inputs from user.
 Calculating centroid of n inputs.
 Calculating Euclidean distance from centroid.
 Sorting all the Euclidean distance from centroid in ascending order.

2. Code-

#include <stdio.h
#include <math.h>
int
main ()
{
int n;
printf ("Enter total number of input:");
scanf ("%d", &n); //user defined n inputs
float x[n], y[n], ed[n], sumx, sumy, t; //variable for point x,y ,euclidean distance, sum of all x,sum of all point
sumx = 0;
sumy = 0;
for (int i = 0; i < n; i++)
{
printf ("enter point x%d y%d \n", i, i);
scanf ("%f %f", &x[i], &y[i]);
}
for (int i = 0; i < n; i++)
{
sumx = sumx + x[i];
sumy = sumy + y[i];
}
sumx = sumx / n; // centroid calculation of all
sumy = sumy / n; // centroid calculation of all y
for (int i = 0; i < n; i++)
{
x[i] = x[i] - sumx;
y[i] = y[i] – sumy
ed[i] = sqrt (x[i] * x[i] + y[i] * y[i]); //Euclidean distance
}
// Sorting
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (ed[i] > ed[j])
{
t = ed[i];
ed[i] = ed[j];
ed[j] = t;
}
}
}
printf ("Sorted Euclidean Distance");
for (int i = 0; i < n; i++)
printf ("%f\t", ed[i]);
}

3. Output:
MODULE 1- FLOATING POINT ADDER

Architecture-

Note- This architecture is illustrated for 24 bits. We have implemented this for half precision floating point
numbers i.e., 16 bits.

Algorithm used-

 We need to add inputs A and B to form Out. Out=A+B


 Here, A and B need to have the same exponents if they are to be added i.e., EA=EB.
 EA and EB are fed to the subtractor and the borrow if 0 shows A>B and B has to be right shifted.
 The exponent difference (EA-EB) provides us with a measure of the shift required in the lower
exponent input.
 Right shift the mantissa of the lower exponent input by the exponent Difference. Thus, both the
inputs have the same exponent as a result.
 The final mantissa is taken to be maximum of the exponents of the input and is further incremented
depending on the carry of the adder.
 Depending on the carry output of the adder, the final mantissa is right shifted by 1 or 0;
 The final exponent and mantissa are thus calculated.
Simulation output-

RTL Synthesis-
MODULE 2- 16 BIT HALF PRECISION SQUARE ROOT

An easy-to-implement square root algorithm for an 16 bit positive integer. Before understanding the algorithm
please reassure yourself that the square root of an 8 bit integer can have at most 8 bits in its whole part (√255
= 15.9687). Thus the square root calculated by this algorithm has a fixed 8.8 format i.e. 8 bits are reserved for
whole part and 8 for decimal. In this algorithm, we guess a trial square root and check whether its square is
greater than the input, x. If it is, we decrease the trial square root and try again. The process takes as many
clock cycles as the number of bits in the input, hence 16 in this case.

Schematic
Simulation Output
MODULE 3- 16 BIT HALF PRECISION COMPARATOR

The architecture of this module is designed in such a manner that only the mantissa bits (bit 0 to bit 9) is
compared for the two inputs, while making the exponent bits (bit 10 to bit 14) same. The initial step is to
compare the exponents of A and B. This is achieved by subtracting the exponents of B from A. If the borrow
is generated it indicates that exponent of B is greater than exponent of A, otherwise exponent of A is greater
than exponent of B. In either case the exponent with greater value is assigned to both the exponents of A and
B. After the exponents of both inputs are same, we right shift the smaller number. The right shifting operation
is done as many times equal to the difference generated from the subtraction of exponents of A and B. Then
we compare the mantissa bits of A and B to generate the output.
Test Bench
Case 1:
A = 1.414 0 01111 0110101000 exp_A=15-15=0 man_A= 0.414
B = 0.8 0 01110 1001100110 exp_B=14-15= -1 man_B= 0.6
Case 2:
A = 1.8 0 01111 1100110011 exp_A=15-15=0 man_A= 0.8
B = 0.3 0 01101 0011001101 exp_B=13-15= -2 man_B= 0.2

Case 3:
A = 1.4 0 01111 0110011010 exp_A=15-15=0 man_A= 0.4
B = 1.3 0 01111 0100110011 exp_B=15-15=0 man_B= 0.3
Case 4:
A = 1.4 0 01111 0110011010 exp_A=15-15=0 man_A= 0.4
B = 1.4 0 01111 0110011010 exp_B=15-15= 0 man_B= 0.4
Case 5:

A = 0.4 0 01101 1001100110 exp_A=13-15= -2 man_A= 0.4

B = 0.8 0 01110 1001100110 exp_B=14-15= -1 man_B= 0.8

In this case note that mantissa bits of A and B are same.

Case 6:

A = 0.9 0 01110 1100110011 exp_A=14-15= -1 man_A= 0.9

B = 0.98 0 01110 1111010111 exp_B=14-15= -1 man_B= 0.98


Simulated Output-

Schematic-
Module 4- SORTING 16BIT
Algorithm-
 We are considering 5 values for sorting each of size 16 bits.
 In the module we are giving inputs as clock, unsorted values and getting output as sorted output.
 We have also declared a sorted_bus register from which we are taking our outputs.
 Then we are implementing bubble sort algorithm on data to achieve sorted output.
SIMULATED OUTPUT-

Here we are giving random inputs- 3524, 5e01, d609, 5663, 7b0d
Getting sorted outputs after 10nsec as – 3524, 5663, 5e01, 7b0d, d609

RTL-Schematic-

You might also like