forked from BauhausUniversity/EasyListener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointND.cpp
102 lines (88 loc) · 1.91 KB
/
PointND.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* EasyListener for Teensy Audio Library
*
* (c) 2020 Clemens Wegener
* Bauhaus-Universität Weimar
* Department Interface Design
*
* This library uses machine learning to detect previously
* presented audio signals on the Teensy board.
*
* BSD license
*/
#include "PointND.h"
#include <cmath>
using namespace std;
PointND::PointND(int dimensions)
{
for(int i=0; i<dimensions; i++)
{
_pointN.push_back(0.0);
}
}
PointND::PointND(const vector<float>& newN){
_pointN = newN;
}
PointND::PointND(const PointND& p2){
_pointN = p2._pointN;
}
void PointND::push_back(float val)
{
_pointN.push_back(val);
}
float PointND::getDim(unsigned n_dimension) const
{
//assert(n_dimension<_pointN.size() && n_dimension>=0)
if(n_dimension<_pointN.size() && n_dimension>=0)
{
return _pointN[n_dimension];
}else
{
return -1;
}
}
void PointND::setDim(unsigned nth_dimension, float value){
_pointN[nth_dimension] = value;
}
float PointND::getDistance(const PointND &otherPoint){
if(size() == otherPoint.size()){
float distance = 0;
for(unsigned i=0; i<_pointN.size(); i++){
distance += (getDim(i) - otherPoint.getDim(i) ) * (getDim(i) - otherPoint.getDim(i) ) ;
}
distance = sqrt(distance);
return distance;
}else{
return -1;
}
}
PointND PointND::operator+(const PointND &p2){
//asssert(size() == p2.size())
vector<float> vres;
for(unsigned i=0; i<_pointN.size(); i++)
{
vres.push_back( getDim(i) + p2.getDim(i) );
}
PointND res(vres);
return res;
}
PointND PointND::operator/(float f){
vector<float> vres;
for(unsigned i=0; i<_pointN.size(); i++){
vres.push_back(_pointN[i]/f);
}
PointND res(vres);
return res;
}
void PointND::normalize(){
float max = -32768;
for(int i=0; i<size(); i++){
float dim = getDim(i);
if(max<dim){
max = dim;
}
}
for(int i=0; i<size(); i++){
setDim(i, getDim(i)-max);
}
}