Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not reduce alias: Tried to add a lowpass filter before downsampling. #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dsp/RecursiveLinearFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ void recursive_linear_filter::HighShelf::SetParams(const recursive_linear_filter

this->_AssignCoefficients(a0, a1, a2, b0, b1, b2);
}

void recursive_linear_filter::LowPassBiquad::SetParams(const recursive_linear_filter::BiquadParams& params)
{
const double omega0 = params.GetOmega0();
const double cosw0 = std::cos(omega0);
const double alpha = params.GetAlpha(omega0);

const double b0 = (1.0 - cosw0) / 2.0;
const double b1 = 1.0 - cosw0;
const double b2 = (1.0 - cosw0) / 2.0;
const double a0 = 1.0 + alpha;
const double a1 = -2.0 * cosw0;
const double a2 = 1.0 - alpha;

// Normalize the coefficients by a0 and assign them
this->_AssignCoefficients(a0, a1, a2, b0, b1, b2);
}
7 changes: 7 additions & 0 deletions dsp/RecursiveLinearFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,11 @@ class LowPass : public Base
}
};

class LowPassBiquad : public Biquad
{
public:
LowPassBiquad() : Biquad() {}
void SetParams(const BiquadParams& params) override;
};

}; // namespace recursive_linear_filter
15 changes: 15 additions & 0 deletions dsp/ResamplingContainer/ResamplingContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ iPlug 2 includes the following 3rd party libraries (see each license info):
#include "Dependencies/WDL/ptrlist.h"

#include "Dependencies/LanczosResampler.h"
#include "AudioDSPTools/dsp/RecursiveLinearFilter.h"


namespace dsp
{
Expand Down Expand Up @@ -133,6 +135,14 @@ class ResamplingContainer
mResampler1 = std::make_unique<LanczosResampler>(mInputSampleRate, mRenderingSampleRate);
mResampler2 = std::make_unique<LanczosResampler>(mRenderingSampleRate, mInputSampleRate);


// Initialize the LowPassBiquad filter with appropriate parameters
double cutoffFrequency = std::min(mInputSampleRate, mRenderingSampleRate) / 2.0 * 0.9; // For example
double qualityFactor = 0.707; // A common choice for a Butterworth filter
double gainDB = 0; // Typically, no gain change for a low-pass filter
recursive_linear_filter::BiquadParams params(mRenderingSampleRate, cutoffFrequency, qualityFactor, gainDB);
mLowPassFilter.SetParams(params);

// Zeroes the scratch pointers so that we warm up with silence.
ClearBuffers();

Expand Down Expand Up @@ -182,6 +192,9 @@ class ResamplingContainer
{
throw std::runtime_error("Got more encapsulated samples than the encapsulated DSP is prepared to handle!");
}
mLowPassFilter.Process(mEncapsulatedOutputPointers.GetList(), 1, populated1);


func(mEncapsulatedInputPointers.GetList(), mEncapsulatedOutputPointers.GetList(), (int)populated1);
// And push the results into the second resampler so that it has what the external context requires.
mResampler2->PushBlock(mEncapsulatedOutputPointers.GetList(), populated1);
Expand All @@ -201,6 +214,8 @@ class ResamplingContainer
int GetLatency() const { return mLatency; }

private:
recursive_linear_filter::LowPassBiquad mLowPassFilter; // Declaration of the LowPassBiquad filter

static inline int LinearInterpolate(T** inputs, T** outputs, int inputLen, double ratio, int maxOutputLen)
{
// FIXME check through this!
Expand Down