721
edits
No edit summary |
|||
Line 42: | Line 42: | ||
[https://replit.com/@abnutzer/PointersAndAdresses Code Example for Pointers and Addresses] | [https://replit.com/@abnutzer/PointersAndAdresses Code Example for Pointers and Addresses] | ||
[[File:pointers_addresses1.png|800px]] | [[File:pointers_addresses1.png|800px]] | ||
== Homework Task == | |||
This week's task is related to machine learning. You will code the foundation of a classifier: the calculation of the centroid of a point cloud. | |||
A short explanation: We will implement the k-means clustering algorithm for sound objects. This clustering algorithm can find data points that are close to each other and are thus considered to be the same "class" of sounds. In a machine learning context a point typically consists of measurements of multiple features of a real world object. This can be e.g. the physical dimension, the color, the weight, etc. When two real world objects are similar, then typically their taken measurements are similar, too. This means, when you consider two objects as two points on a 2D plane, then, if these objects are similar, their corresponding points on the 2D plane are close to each other. | |||
In our case, we will measure the spectral intensity of different sounds and our classifier will group sounds in accordance to their spectral intensities. Thus, spectrally bright sounds can be separated from sprectrally dull sounds. Depending of what features we present the classifier as our fundamental data, we can discern much more characteristics in our input sounds, e.g. the speed of the attack, or even the fundamental pitch, or the harmonics in our sounds. | |||
The centroid is the center point of such a cluster of points of similar sounds and is very important for a clustering algorithm to decide to which cluster a data point belongs. | |||
To calculate a centroid of a collection of points (our cluster) we need to do two steps: | |||
sum up our points: p0 + p1 + p2 + p3 ... = sum | |||
divide the sum by out number of point: centroid = sum / num_of_points | |||
Note: We sum up points simply by adding up their x and y values. E.g. | |||
p0 = (1,2), p1= (2,4) | |||
p0 + p1 = p2; | |||
(1,2) + (2,4) = (3,6) | |||
Dividing the point by a number, means to divide both dimensions by the same number: | |||
centroid = p2 /3 = (3,6) / 2 = (1.5,3) | |||
In this case our centroid is (1.5,3). If you drew our two points and the centroid on paper, you would notice, that the centroid lies exactly in the middle of our two points. | |||
Programming Task | |||
Extend our Point2D class to have the following functions: | |||
Point2D operator+(Point2D& p2); | |||
This function should add up two points and return a new Point2D, which is the sum of the two points. | |||
If you implement this function you can then simply write: Point2D sum = p0 + p1; (where p0 and p1 are of the class Point2D) | |||
Point2D operator/(float f); | |||
This function should divide a point by the number f and return a new Point2D which is the divided point. | |||
If you implement this function you can then simply write: Point2D div = p0 / 5; (where p0 is of class Point2D) | |||
Write an algorithm that calculates the centroid of the given points | |||
use a for loop to add up any given number of points (for this, out the points into an array and the access the array in the loop!) | |||
If you are curious of the operator+ and operator/ statements, google "cpp operator overloading" ! | |||
Here are the points to calculate the centroid for: | |||
Point2D p0(0,0); | |||
Point2D p1(0,1); | |||
Point2D p2(1,0); | |||
Point2D p3(1,1); | |||
And here is a code template to start coding from: https://repl.it/@abnutzer/Homework-2 | |||
Press the "fork" button to work on your own copy of the code! | |||
Later in the class, our points will not be two dimensional anymore, but contain different features of our sounds. E.g. it could be 3D: A dimension for the loudness of the bass, one for the mids and one for the treble. In general the resolution of the spectral data will be much higher and thus a point of 128 dimensions or more can be considered. |