Skip to content

Commit

Permalink
Add hps functions #3
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Jan 4, 2023
1 parent fa74817 commit ba13e1e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/voyx/etc/Convert.HPS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <voyx/Header.h>

// http://musicweb.ucsd.edu/~trsmyth/analysis/Harmonic_Product_Spectrum.html

namespace $$
{
template<typename T>
std::vector<T> hpsmul(const std::vector<T>& vector, const size_t depth, const T empty = 0)
{
std::vector<T> product(vector.begin(), vector.end());

for (size_t step = 2; step < depth + 2; ++step)
{
std::vector<T> harmonic(vector.size(), empty);

for (size_t i = 0, j = 0; i < vector.size(); i += step, j += 1)
{
harmonic[j] = vector[i];
}

for (size_t i = 0; i < vector.size(); ++i)
{
product[i] /* multiply */ *= harmonic[i];
}
}

return product;
}

template<typename T>
std::vector<T> hpsadd(const std::vector<T>& vector, const size_t depth, const T empty = -120)
{
std::vector<T> product(vector.begin(), vector.end());

for (size_t step = 2; step < depth + 2; ++step)
{
std::vector<T> harmonic(vector.size(), empty);

for (size_t i = 0, j = 0; i < vector.size(); i += step, j += 1)
{
harmonic[j] = vector[i];
}

for (size_t i = 0; i < vector.size(); ++i)
{
product[i] /* add */ += harmonic[i];
}
}

return product;
}
}
1 change: 1 addition & 0 deletions src/voyx/etc/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <voyx/etc/Convert.DFT.h>
#include <voyx/etc/Convert.FFT.h>
#include <voyx/etc/Convert.MIDI.h>
#include <voyx/etc/Convert.HPS.h>

#include <voyx/etc/Convert.String.h>
#include <voyx/etc/Convert.Type.h>

0 comments on commit ba13e1e

Please sign in to comment.