Skip to content

Commit

Permalink
Update with sr-dependent visualizer calculation and big refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbb committed Apr 8, 2019
1 parent 19c0645 commit 2c2d705
Show file tree
Hide file tree
Showing 11 changed files with 2,909 additions and 2,727 deletions.
232 changes: 132 additions & 100 deletions Source/ARModeler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,120 +20,152 @@
#ifndef AR_MODELER_H_INCLUDED
#define AR_MODELER_H_INCLUDED

#include "../../../JuceLibraryCode/JuceHeader.h"

class ARModeler {
public:
ARModeler(int order = 1, int length = 2, int strideIn = 1, bool* success = nullptr)
{
bool s = setParams(order, length, strideIn);
if (success != nullptr)
#include <BasicJuceHeader.h>

namespace PhaseCalculator
{
class ARModeler {
public:
ARModeler(int order = 1, int length = 2, int strideIn = 1, bool* success = nullptr)
{
*success = s;
bool s = setParams(order, length, strideIn);
if (success != nullptr)
{
*success = s;
}
}
}

~ARModeler() { }
~ARModeler() { }

// returns true if successful.
bool setParams(int order, int length, int strideIn)
{
int newStridedLength = calcStridedLength(length, strideIn);
if (order < 1 || newStridedLength < order + 1)
void reset()
{
jassertfalse;
return false;
hasBeenUsed = false;
}
arOrder = order;
inputLength = length;
stride = strideIn;
stridedLength = newStridedLength;
reallocateStorage();
return true;
}

void fitModel(const Array<double>& j_inputseries, Array<double>& j_coef)
{
jassert(j_inputseries.size() == inputLength);
jassert(j_coef.size() == arOrder);
double t1, t2;
int n;

// get raw pointers to improve performance
const double* inputseries = j_inputseries.begin();
double* coef = j_coef.begin();
double* per = j_per.begin();
double* pef = j_pef.begin();
double* h = j_h.begin();

// reset per and pef
resetPredictionError();

for (n = 1; n <= arOrder; n++)

bool hasBeenFit()
{
double sn = 0.0;
double sd = 0.0;
int j;
int jj = stridedLength - n;
return hasBeenUsed;
}

for (j = 0; j < jj; j++)
// returns true if successful.
bool setParams(int order, int length, int strideIn)
{
int newStridedLength = calcStridedLength(length, strideIn);
if (order < 1 || newStridedLength < order + 1)
{
t1 = inputseries[stride * (j + n)] + pef[j];
t2 = inputseries[stride * j] + per[j];
sn -= 2.0 * t1 * t2;
sd += (t1 * t1) + (t2 * t2);
jassertfalse;
return false;
}
arOrder = order;
inputLength = length;
stride = strideIn;
stridedLength = newStridedLength;
reallocateStorage();
return true;
}

t1 = sn / sd;
coef[n - 1] = t1;
if (n != 1)
{
for (j = 1; j < n; j++)
h[j - 1] = coef[j - 1] + t1 * coef[n - j - 1];
for (j = 1; j < n; j++)
coef[j - 1] = h[j - 1];
jj--;
}
void getModel(Array<double, CriticalSection>& modelOut)
{
jassert(hasBeenUsed);

// copies using internal mutex
modelOut = coefficients;
}

void fitModel(const Array<double>& j_inputseries_reverse)
{
jassert(j_inputseries_reverse.size() >= inputLength);
double t1, t2;
int n;

// get raw pointers to improve performance
const double* inputseries_last = j_inputseries_reverse.begin() + inputLength - 1;
double* coef = j_coef_temp.begin();
double* per = j_per.begin();
double* pef = j_pef.begin();
double* h = j_h.begin();

// reset per and pef
resetPredictionError();

for (j = 0; j < jj; j++)
for (n = 1; n <= arOrder; n++)
{
per[j] = per[j] + t1 * pef[j] + t1 * inputseries[stride * (j + n)];
pef[j] = pef[j + 1] + t1 * per[j + 1] + t1 * inputseries[stride * (j + 1)];
double sn = 0.0;
double sd = 0.0;
int j;
int jj = stridedLength - n;

for (j = 0; j < jj; j++)
{
t1 = inputseries_last[-stride * (j + n)] + pef[j];
t2 = inputseries_last[-stride * j] + per[j];
sn -= 2.0 * t1 * t2;
sd += (t1 * t1) + (t2 * t2);
}

t1 = sn / sd;
coef[n - 1] = t1;
if (n != 1)
{
for (j = 1; j < n; j++)
h[j - 1] = coef[j - 1] + t1 * coef[n - j - 1];
for (j = 1; j < n; j++)
coef[j - 1] = h[j - 1];
jj--;
}

for (j = 0; j < jj; j++)
{
per[j] = per[j] + t1 * pef[j] + t1 * inputseries_last[-stride * (j + n)];
pef[j] = pef[j + 1] + t1 * per[j + 1] + t1 * inputseries_last[-stride * (j + 1)];
}
}

// save (using built-in mutex)
coefficients.swapWith(j_coef_temp);

hasBeenUsed = true;
}

private:

void reallocateStorage()
{
j_h.resize(arOrder - 1);
j_coef_temp.resize(arOrder);
coefficients.resize(arOrder);
resetPredictionError();
}

void resetPredictionError()
{
j_per.clearQuick();
j_per.insertMultiple(0, 0, stridedLength);
j_pef.clearQuick();
j_pef.insertMultiple(0, 0, stridedLength);
}

static int calcStridedLength(int inputLength, int stride)
{
jassert(stride > 0);
return (inputLength + (stride - 1)) / stride;
}
}

private:

void reallocateStorage()
{
j_h.resize(arOrder - 1);
resetPredictionError();
}

void resetPredictionError()
{
j_per.clearQuick();
j_per.insertMultiple(0, 0, stridedLength);
j_pef.clearQuick();
j_pef.insertMultiple(0, 0, stridedLength);
}

static int calcStridedLength(int inputLength, int stride)
{
jassert(stride > 0);
return (inputLength + (stride - 1)) / stride;
}

int arOrder;
int inputLength;
int stridedLength;
int stride;
Array<double> j_per;
Array<double> j_pef;
Array<double> j_h;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ARModeler);
};

int arOrder;
int inputLength;
int stridedLength;
int stride;
Array<double> j_per;
Array<double> j_pef;
Array<double> j_h;

Array<double, CriticalSection> j_coef_temp;
Array<double, CriticalSection> coefficients;

bool hasBeenUsed = false;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ARModeler);
};
}

#endif // AR_MODELER_H_INCLUDED
Loading

0 comments on commit 2c2d705

Please sign in to comment.