Skip to content

Commit

Permalink
Merge pull request #68 from sp-nitech/mode
Browse files Browse the repository at this point in the history
Add mode
  • Loading branch information
takenori-y authored Oct 30, 2024
2 parents 0313fa8 + 349aad2 commit df51c1b
Show file tree
Hide file tree
Showing 11 changed files with 650 additions and 70 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ set(CC_SOURCES
${SOURCE_DIR}/math/matrix.cc
${SOURCE_DIR}/math/matrix2d.cc
${SOURCE_DIR}/math/minmax_accumulation.cc
${SOURCE_DIR}/math/mode_accumulation.cc
${SOURCE_DIR}/math/principal_component_analysis.cc
${SOURCE_DIR}/math/real_valued_fast_fourier_transform.cc
${SOURCE_DIR}/math/real_valued_inverse_fast_fourier_transform.cc
Expand Down Expand Up @@ -331,6 +332,7 @@ set(MAIN_SOURCES
${SOURCE_DIR}/main/minmax.cc
${SOURCE_DIR}/main/mlpg.cc
${SOURCE_DIR}/main/mlsacheck.cc
${SOURCE_DIR}/main/mode.cc
${SOURCE_DIR}/main/mpir2c.cc
${SOURCE_DIR}/main/mseq.cc
${SOURCE_DIR}/main/msvq.cc
Expand Down
4 changes: 4 additions & 0 deletions doc/main/minmax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ minmax

.. doxygenfile:: minmax.cc

.. seealso::

:ref:`mode`

.. doxygenclass:: sptk::MinMaxAccumulation
:members:
13 changes: 13 additions & 0 deletions doc/main/mode.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. _mode:

mode
====

.. doxygenfile:: mode.cc

.. seealso::

:ref:`minmax`

.. doxygenclass:: sptk::ModeAccumulation
:members:
24 changes: 12 additions & 12 deletions include/SPTK/math/minmax_accumulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#ifndef SPTK_MATH_MINMAX_ACCUMULATION_H_
#define SPTK_MATH_MINMAX_ACCUMULATION_H_

#include <list> // std::list
#include <utility> // std::pair
#include <functional> // std::greater, std::less
#include <map> // std::multimap

#include "SPTK/utils/sptk_utils.h"

Expand Down Expand Up @@ -48,8 +48,8 @@ class MinMaxAccumulation {
}

int position_;
std::list<std::pair<int, double> > minimum_;
std::list<std::pair<int, double> > maximum_;
std::multimap<double, int, std::greater<double> > minimum_;
std::multimap<double, int, std::less<double> > maximum_;

friend class MinMaxAccumulation;
DISALLOW_COPY_AND_ASSIGN(Buffer);
Expand Down Expand Up @@ -80,26 +80,26 @@ class MinMaxAccumulation {
/**
* Get @f$n@f$-th minimum value and its position.
*
* @param[in] buffer Buffer.
* @param[in] rank Rank @f$n@f$.
* @param[out] position Position of minimum value.
* @param[in] buffer Buffer.
* @param[out] value Minimum value.
* @param[out] position Position of the minimum value.
* @return True on success, false on failure.
*/
bool GetMinimum(const MinMaxAccumulation::Buffer& buffer, int rank,
int* position, double* value) const;
bool GetMinimum(int rank, const MinMaxAccumulation::Buffer& buffer,
double* value, int* position) const;

/**
* Get @f$n@f$-th maximum value and its position.
*
* @param[in] buffer Buffer.
* @param[in] rank Rank @f$n@f$.
* @param[out] position Position of maximum value.
* @param[in] buffer Buffer.
* @param[out] value Maximum value.
* @param[out] position Position of the maximum value.
* @return True on success, false on failure.
*/
bool GetMaximum(const MinMaxAccumulation::Buffer& buffer, int rank,
int* position, double* value) const;
bool GetMaximum(int rank, const MinMaxAccumulation::Buffer& buffer,
double* value, int* position) const;

/**
* Clear buffer.
Expand Down
116 changes: 116 additions & 0 deletions include/SPTK/math/mode_accumulation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// ------------------------------------------------------------------------ //
// Copyright 2021 SPTK Working Group //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ------------------------------------------------------------------------ //

#ifndef SPTK_MATH_MODE_ACCUMULATION_H_
#define SPTK_MATH_MODE_ACCUMULATION_H_

#include <map> // std::multimap
#include <unordered_map> // std::unordered_map

#include "SPTK/utils/sptk_utils.h"

namespace sptk {

/**
* Compute mode given data sequence.
*/
class ModeAccumulation {
public:
/**
* Buffer for ModeAccumulation.
*/
class Buffer {
public:
Buffer() {
}

virtual ~Buffer() {
}

private:
void Clear() {
count_.clear();
maximum_.clear();
}

std::unordered_map<double, int> count_;
std::multimap<int, double> maximum_;

friend class ModeAccumulation;
DISALLOW_COPY_AND_ASSIGN(Buffer);
};

/**
* @param[in] num_best Number of modes.
*/
explicit ModeAccumulation(int num_best);

virtual ~ModeAccumulation() {
}

/**
* @return Number of modes.
*/
int GetNumBest() const {
return num_best_;
}

/**
* @return True if this object is valid.
*/
bool IsValid() const {
return is_valid_;
}

/**
* Get @f$n@f$-th mode value and its count.
*
* @param[in] rank Rank @f$n@f$.
* @param[in] buffer Buffer.
* @param[out] value Mode value.
* @param[out] count Count of the mode value.
* @return True on success, false on failure.
*/
bool GetMode(int rank, const ModeAccumulation::Buffer& buffer, double* value,
int* count) const;

/**
* Clear buffer.
*
* @param[out] buffer Buffer.
*/
void Clear(ModeAccumulation::Buffer* buffer) const;

/**
* Accumulate mode.
*
* @param[in] data Input data.
* @param[in,out] buffer Buffer.
* @return True on success, false on failure.
*/
bool Run(double data, ModeAccumulation::Buffer* buffer) const;

private:
const int num_best_;

bool is_valid_;

DISALLOW_COPY_AND_ASSIGN(ModeAccumulation);
};

} // namespace sptk

#endif // SPTK_MATH_MODE_ACCUMULATION_H_
12 changes: 6 additions & 6 deletions src/main/minmax.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ bool WriteMinMaxValues(
if (kMinimumAndMaximum == output_format || kMinimum == output_format) {
for (int rank(1); rank <= num_best; ++rank) {
for (int vector_index(0); vector_index < vector_length; ++vector_index) {
int position;
double value;
if (!minmax_accumulation.GetMinimum(buffer[vector_index], rank,
&position, &value)) {
int position;
if (!minmax_accumulation.GetMinimum(rank, buffer[vector_index], &value,
&position)) {
return false;
}
if (NULL != stream_for_position &&
Expand All @@ -107,10 +107,10 @@ bool WriteMinMaxValues(
if (kMinimumAndMaximum == output_format || kMaximum == output_format) {
for (int rank(1); rank <= num_best; ++rank) {
for (int vector_index(0); vector_index < vector_length; ++vector_index) {
int position;
double value;
if (!minmax_accumulation.GetMaximum(buffer[vector_index], rank,
&position, &value)) {
int position;
if (!minmax_accumulation.GetMaximum(rank, buffer[vector_index], &value,
&position)) {
return false;
}
if (NULL != stream_for_position &&
Expand Down
Loading

0 comments on commit df51c1b

Please sign in to comment.