Essentia  2.1-beta6-dev
peak.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef PEAK_H
21 #define PEAK_H
22 
23 #include <utility> // std::pair
24 #include "types.h"
25 
26 namespace essentia {
27 namespace util {
28 
29 class Peak {
30  public:
33 
34  Peak() : position(), magnitude() {}
35  Peak(const Peak& p) : position(p.position), magnitude(p.magnitude) {}
36 
37  template<typename T, typename U>
38  Peak(const T& pos, const U& mag) : position(pos), magnitude(mag) {}
39 
40  template<typename T, typename U>
41  Peak(const std::pair<T,U>& p) : position(p.first), magnitude(p.second) {}
42 
43  bool operator ==(const Peak& p) const {
44  return (position == p.position) && (magnitude == p.magnitude);
45  }
46 
47  bool operator !=(const Peak& p) const {
48  return (position != p.position) || (magnitude != p.magnitude);
49  }
50 
51  bool operator< (const Peak& p) const { return magnitude < p.magnitude; }
52  bool operator> (const Peak& p) const { return magnitude > p.magnitude; }
53  bool operator<=(const Peak& p) const { return magnitude <= p.magnitude; }
54  bool operator>=(const Peak& p) const { return magnitude >= p.magnitude; }
55 
56  Peak& operator=(const Peak& p) {
58  return *this;
59  }
60 
61  template<typename T, typename U>
62  Peak& operator=(const std::pair<T, U>& p) {
63  position = p.first;magnitude = p.second;
64  return *this;
65  }
66 };
67 
68 // peak comparison:
69 
70 // comparing by position, by default sorts by ascending position and in case
71 // the positions are equal it sorts by descending magnitude
72 template<typename Comp1=std::less<Real>,
73  typename Comp2=std::greater_equal<Real> >
74 class ComparePeakPosition : public std::binary_function<Real, Real, bool> {
75  Comp1 _cmp1;
76  Comp2 _cmp2;
77  public:
78  bool operator () (const Peak& p1, const Peak& p2) const {
79  if (_cmp1(p1.position, p2.position)) return true;
80  if (_cmp1(p2.position, p1.position)) return false;
81  return _cmp2(p1.magnitude, p2.magnitude);
82  }
83 };
84 
85 // comparing by magnitude, by default sorts by descending magnitude and in case
86 // the magnitudes are equal it sorts by ascending position
87 template<typename Comp1=std::greater<Real>,
88  typename Comp2=std::less_equal<Real> >
89 class ComparePeakMagnitude : public std::binary_function<Real, Real, bool> {
90  Comp1 _cmp1;
91  Comp2 _cmp2;
92  public:
93  bool operator () (const Peak& p1, const Peak& p2) const {
94  if (_cmp1(p1.magnitude, p2.magnitude)) return true;
95  if (_cmp1(p2.magnitude, p1.magnitude)) return false;
96  return _cmp2(p1.position, p2.position);
97  }
98 };
99 
100 // from 2 vector<Real> to vector<Peak>:
101 inline std::vector<Peak> realsToPeaks(const std::vector<Real>& pos,
102  const std::vector<Real>& mag) {
103  int size = pos.size();
104  if (size != int(mag.size())) {
105  throw EssentiaException("realsToPeaks: position vector size != magnitude vector size");
106  }
107  std::vector<Peak> peaks(size);
108  for (int i=0; i<size; i++) {
109  peaks[i] = Peak(pos[i], mag[i]);
110  }
111  return peaks;
112 }
113 
114 // from vector<Peak> to 2 vector<Real>
115 inline void peaksToReals(const std::vector<Peak>& peaks,
116  std::vector<Real>& pos, std::vector<Real>& mag) {
117  int size = peaks.size();
118  if (size != int(pos.size())) pos.resize(size);
119  if (size != int(mag.size())) mag.resize(size);
120 
121  for (int i=0; i<size; i++) {
122  pos[i] = peaks[i].position;
123  mag[i] = peaks[i].magnitude;
124  }
125 }
126 
127 inline std::ostream& operator<<(std::ostream& out, const Peak& p) {
128  return out << "position: " << p.position << ", magnitude: " << p.magnitude;
129 }
130 
131 } // namespace util
132 } // namespace essentia
133 
134 #endif // PEAK_H
Definition: types.h:77
Comp2 _cmp2
Definition: peak.h:91
bool operator()(const Peak &p1, const Peak &p2) const
Definition: peak.h:93
Comp1 _cmp1
Definition: peak.h:90
Comp2 _cmp2
Definition: peak.h:76
bool operator()(const Peak &p1, const Peak &p2) const
Definition: peak.h:78
Comp1 _cmp1
Definition: peak.h:75
Definition: peak.h:29
Peak & operator=(const std::pair< T, U > &p)
Definition: peak.h:62
bool operator==(const Peak &p) const
Definition: peak.h:43
bool operator<(const Peak &p) const
Definition: peak.h:51
bool operator!=(const Peak &p) const
Definition: peak.h:47
bool operator>=(const Peak &p) const
Definition: peak.h:54
bool operator>(const Peak &p) const
Definition: peak.h:52
Peak & operator=(const Peak &p)
Definition: peak.h:56
Real position
Definition: peak.h:31
bool operator<=(const Peak &p) const
Definition: peak.h:53
Peak(const std::pair< T, U > &p)
Definition: peak.h:41
Peak()
Definition: peak.h:34
Peak(const Peak &p)
Definition: peak.h:35
Peak(const T &pos, const U &mag)
Definition: peak.h:38
Real magnitude
Definition: peak.h:32
void peaksToReals(const std::vector< Peak > &peaks, std::vector< Real > &pos, std::vector< Real > &mag)
Definition: peak.h:115
std::ostream & operator<<(std::ostream &out, const Peak &p)
Definition: peak.h:127
std::vector< Peak > realsToPeaks(const std::vector< Real > &pos, const std::vector< Real > &mag)
Definition: peak.h:101
Definition: algorithm.h:28
float Real
Definition: types.h:69