Skip to content

Commit

Permalink
Adding a bunch of new functionalities.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohabouje committed Aug 2, 2018
1 parent a273397 commit c197c15
Show file tree
Hide file tree
Showing 24 changed files with 879 additions and 4 deletions.
39 changes: 39 additions & 0 deletions include/easy/dsp/statistics/arithmetic_mean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: arithmetic_mean.hpp
* Author: Mohammed Boujemaoui
* Date: 2018-06-13
*/
#ifndef EASYDSP_STATISTICAL_MEAN_H
#define EASYDSP_STATISTICAL_MEAN_H

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <numeric>

namespace easy { namespace dsp { namespace statistics {
template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type mean(InputIterator first, InputIterator last) {
using namespace boost::accumulators;
accumulator_set<value_type, features<tag::mean>> acc;
acc = std::for_each(first, last, acc);
return boost::accumulators::mean(acc);
}
}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_MEAN_H
41 changes: 41 additions & 0 deletions include/easy/dsp/statistics/centroid.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: centroid.hpp
* Author: Mohammed Boujemaoui
* Date: 17/6/2018
*/
#ifndef EASYDSP_STATISTICAL_CENTROID_HPP
#define EASYDSP_STATISTICAL_CENTROID_HPP

#include <iterator>

namespace easy { namespace dsp { namespace statistics {
template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type centroid(InputIterator first, InputIterator last) {
value_type weighted_sum = static_cast<value_type>(0);
value_type unweighted_sum = static_cast<value_type>(0);
for (value_type i = 0; first != last; ++first, ++i) {
weighted_sum += i * (*first);
unweighted_sum += *first;
}
return weighted_sum / unweighted_sum;
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_CENTROID_HPP
38 changes: 38 additions & 0 deletions include/easy/dsp/statistics/crest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: crest.hpp
* Author: Mohammed Boujemaoui
* Date: 14/6/2018
*/
#ifndef EASYDSP_STATISTICAL_CREST_HPP
#define EASYDSP_STATISTICAL_CREST_HPP

#include "arithmetic_mean.hpp"

namespace easy { namespace dsp { namespace statistics {

template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type crest(InputIterator first, InputIterator last) {
const value_type computed_mean = mean(first, last);
const value_type computed_max = *std::max_element(first, last);
return computed_max / computed_mean;
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_CREST_HPP
42 changes: 42 additions & 0 deletions include/easy/dsp/statistics/decrease.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: decrease.hpp
* Author: Mohammed Boujemaoui
* Date: 17/6/2018
*/
#ifndef EASYDSP_STATISTICAL_DECREASE_HPP
#define EASYDSP_STATISTICAL_DECREASE_HPP

#include <iterator>

namespace easy { namespace dsp { namespace statistics {
template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type decrease(InputIterator first, InputIterator last) {
const auto size = std::distance(first, last);
value_type weighted_sum = static_cast<value_type>(0);
value_type unweighted_sum = static_cast<value_type>(0);
for (value_type i = 0, initial = *first; first != last; ++first, ++i) {
weighted_sum += (1 / i) * ((*first) - initial);
unweighted_sum += *first;
}
return weighted_sum / unweighted_sum;
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_DECREASE_HPP
41 changes: 41 additions & 0 deletions include/easy/dsp/statistics/entropy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: entropy.hpp
* Author: Mohammed Boujemaoui
* Date: 17/6/2018
*/
#ifndef EASYDSP_STATISTICAL_ENTROPY_HPP
#define EASYDSP_STATISTICAL_ENTROPY_HPP

#include <cmath>
#include <numeric>

namespace easy { namespace dsp { namespace statistics {
template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type entropy(InputIterator first, InputIterator last) {
const auto sum = std::accumulate(first, last, static_cast<value_type>(0));
const auto predicate = [sum](const value_type accumulated, const value_type current) {
const auto normalized = current / sum;
return (accumulated + std::log2(normalized) * normalized);
};
return std::accumulate(first, last, static_cast<value_type>(0), predicate);
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_ENTROPY_HPP
39 changes: 39 additions & 0 deletions include/easy/dsp/statistics/flatness.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: flatness.hpp
* Author: Mohammed Boujemaoui
* Date: 14/6/2018
*/
#ifndef EASYDSP_STATISTICAL_FLATNESS_H
#define EASYDSP_STATISTICAL_FLATNESS_H

#include "arithmetic_mean.hpp"
#include "geometric_mean.hpp"

namespace easy { namespace dsp { namespace statistics {

template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type flatness(InputIterator first, InputIterator last) {
const value_type computed_gmean = geometric_mean(first, last);
const value_type computed_mean = mean(first, last);
return geometric_mean / computed_mean;
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_FLATNESS_H
62 changes: 62 additions & 0 deletions include/easy/dsp/statistics/flux.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: flux.hpp
* Author: Mohammed Boujemaoui
* Date: 17/6/2018
*/
#ifndef EASYDSP_STATISTICAL_FLUX_HPP
#define EASYDSP_STATISTICAL_FLUX_HPP

#include <type_traits>
#include <iterator>

namespace easy { namespace dsp { namespace statistics {

enum FluxType { ManhattanDistance, EuclideanDistance, Logarithmic };

template <FluxType Type>
struct Flux {
template <typename InputIterator,
typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type operator()(InputIterator first, InputIterator last, InputIterator first_other) const {
const auto size = std::distance(first, last);
if constexpr (std::is_same_v<Type, FluxType::ManhattanDistance>) {
value_type accumulated = static_cast<value_type>(0);
for (; first != last; ++first, ++first_other) {
accumulated += meta::manhattan_distance(*first, *first_other);
}
return std::sqrt(accumulated) / static_cast<value_type>(size);

} else if constexpr (std::is_same_v<Type, FluxType::EuclideanDistance>) {
value_type accumulated = static_cast<value_type>(0);
for (; first != last; ++first, ++first_other) {
accumulated += meta::euclidean_distance(*first, *first_other);
}
return accumulated / static_cast<value_type>(size);
} else if constexpr (std::is_same_v<Type, FluxType::Logarithmic>) {
value_type accumulated = static_cast<value_type>(0);
for (; first != last; ++first, ++first_other) {
accumulated += std::log(std::abs(*first) / std::abs(*first_other));
}
return accumulated / static_cast<value_type>(size);
}
}
};
}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_FLUX_HPP
45 changes: 45 additions & 0 deletions include/easy/dsp/statistics/generalized_mean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: generalized_mean.hpp
* Author: Mohammed Boujemaoui
* Date: 2018-06-13
*/
#ifndef EASYDSP_STATISTICAL_GENERALIZED_MEAN_H
#define EASYDSP_STATISTICAL_GENERALIZED_MEAN_H

#include <numeric>
#include <cmath>
#include <iterator>

namespace easy { namespace dsp { namespace statistics {

template <typename InputIterator, typename Integer,
typename value_type = typename std::iterator_traits<InputIterator>::value_type,
typename = typename std::enable_if<std::is_integral<Integer>::value>::type>
inline value_type generalized_mean(InputIterator first, InputIterator last, Integer beta) {
const auto predicate = [beta](const value_type prev, const value_type current) {
return static_cast<value_type>(prev + std::pow(current, beta));
};
const value_type accumulated = std::accumulate(first, last, value_type(), predicate);
const value_type temp = accumulated / static_cast<value_type>(std::distance(first, last));
return static_cast<value_type>(std::pow(temp, 1 / static_cast<value_type>(beta)));
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_GENERALIZED_MEAN_H
39 changes: 39 additions & 0 deletions include/easy/dsp/statistics/geometric_mean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* EasyDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along withº
* this program. If not, see <http://www.gnu.org/licenses/>
*
* Filename: geometric_mean.hpp
* Author: Mohammed Boujemaoui
* Date: 2018-06-13
*/
#ifndef EASYDSP_STATISTICAL_GEOMETRIC_MEAN_H
#define EASYDSP_STATISTICAL_GEOMETRIC_MEAN_H

#include <numeric>
#include <cmath>
#include <iterator>

namespace easy { namespace dsp { namespace statistics {

template <typename InputIterator, typename value_type = typename std::iterator_traits<InputIterator>::value_type>
inline value_type geometric_mean(InputIterator first, InputIterator last) {
const value_type accumulated = std::accumulate(first, last, value_type(), std::multiplies<value_type>());
return static_cast<value_type>(std::pow(accumulated, 1 / static_cast<value_type>(std::distance(first, last))));
}

}}} // namespace easy::feature::statistical

#endif // EASYDSP_STATISTICAL_GEOMETRIC_MEAN_H
Loading

0 comments on commit c197c15

Please sign in to comment.