I can not warp my head around this. I taught that I figured this out, but after some testing it seams that my code doesn't work properly...
So here is what I am doing:
These are standard ITU coeffs for 4x oversampling.
Code:
class CircularBuffer
{
public:
CircularBuffer()
{
}
void SetBufferSize(int _bufferSize)
{
bufferSize = _bufferSize;
buffer.resize(_bufferSize, 0);
}
void AddValue(double value)
{
buffer.push_back(value);
buffer.push_back(0);
buffer.push_back(0);
buffer.push_back(0);
buffer.pop_front();
buffer.pop_front();
buffer.pop_front();
buffer.pop_front();
}
double GetAtIndex(int index)
{
return buffer[index];
}
int GetSize()
{
return bufferSize;
}
private:
double inputValue = 0;
std::deque<double> buffer;
int bufferSize = 0;
};
class youlean_true_peak
{
public:
youlean_true_peak()
{
// For now it should be dividable with 4
coefficients =
{
0.0017089843750,
-0.0291748046875,
-0.0189208984375,
-0.0083007812500,
0.0109863281250,
0.0292968750000,
0.0330810546875,
0.0148925781250,
-0.0196533203125,
-0.0517578125000,
-0.0582275390625,
-0.0266113281250,
0.0332031250000,
0.0891113281250,
0.1015625000000,
0.0476074218750,
-0.0594482421875,
-0.1665039062500,
-0.2003173828125,
-0.1022949218750,
0.1373291015625,
0.4650878906250,
0.7797851562500,
0.9721679687500,
0.9721679687500,
0.7797851562500,
0.4650878906250,
0.1373291015625,
-0.1022949218750,
-0.2003173828125,
-0.1665039062500,
-0.0594482421875,
0.0476074218750,
0.1015625000000,
0.0891113281250,
0.0332031250000,
-0.0266113281250,
-0.0582275390625,
-0.0517578125000,
-0.0196533203125,
0.0148925781250,
0.0330810546875,
0.0292968750000,
0.0109863281250,
-0.0083007812500,
-0.0189208984375,
-0.0291748046875,
0.0017089843750
};
coeff_size = coefficients.size();
audioBuffer.SetBufferSize(coeff_size + 4);
}
double get_true_peak(double audio)
{
audioBuffer.AddValue(audio);
max_true_peak = 0;
output_x1 = 0;
output_x2 = 0;
output_x3 = 0;
output_x4 = 0;
for (int i = 0; i < coeff_size; i++)
{
output_x1 += coefficients[i] * audioBuffer.GetAtIndex(i);
output_x2 += coefficients[i] * audioBuffer.GetAtIndex(i + 1);
output_x3 += coefficients[i] * audioBuffer.GetAtIndex(i + 2);
output_x4 += coefficients[i] * audioBuffer.GetAtIndex(i + 3);
}
return IPMAX(IPMAX(IPMAX(std::abs(output_x1), std::abs(output_x3)), std::abs(output_x3)), std::abs(output_x4));
}
private:
CircularBuffer audioBuffer;
std::vector <double> coefficients;
double output_x1 = 0.f, output_x2 = 0.f, output_x3 = 0.f, output_x4 = 0.f,
max_true_peak = 0.f;
int coeff_size = 0;
};
What is not clear to me is how should I use these coeffs? I tried also making audio buffer coeff_size / 4 lenght, than getting first value in audio buffer and multiplying that value to first coeff and then storing that in output_x1, then multiplying first value in audio buffer with second coeff than storing that in output_x2....
Any help would be much appreciated. Also I was resampler.h, so can I use that to calculate true peak value?
EDIT: Found what it is. It was a some bug with IPMAX outside this code.