revises oscilloscope

This commit is contained in:
tosh 2013-05-19 16:28:49 +02:00
parent fbdda8bce4
commit e99a41f41b
2 changed files with 41 additions and 14 deletions

View file

@ -34,8 +34,9 @@ namespace { // everything in here only exists while compiling this .cpp file
Oscilloscope::Oscilloscope(int w, int h, bool isEnabled) :
_width(w), _height(h),
_samples(0l), _vertices(0l),
_lowpassFactor(0.4f), _downsampleFactor(3),
_samples(0l), _vertices(0l),
// three in -> one out, some filtering (see details in Log.h)
_lowPassCoeff(0.4f), _downsampleRatio(3),
enabled(isEnabled), inputPaused(false) {
// allocate enough space for the sample data and to turn it into
@ -94,9 +95,9 @@ void Oscilloscope::render(int x, int y) {
return;
}
// determine lowpass / downsample factors
int lowpass = -int(std::numeric_limits<short>::min()) * _lowpassFactor;
unsigned downsample = _downsampleFactor;
// fetch low pass factor (and convert to fix point) / downsample factor
int lowPassFixPt = -int(std::numeric_limits<short>::min()) * _lowPassCoeff;
unsigned downsample = _downsampleRatio;
// keep half of the buffer for writing and ensure an even vertex count
unsigned usedWidth = min(_width, MAX_SAMPLES_PER_CHANNEL / (downsample * 2)) & ~1u;
unsigned usedSamples = usedWidth * downsample;
@ -115,7 +116,7 @@ void Oscilloscope::render(int x, int y) {
inPtr = endPtr;
}
// read and (eventually) filter sample
sample += ((*--inPtr - sample) * lowpass) >> 15;
sample += ((*--inPtr - sample) * lowPassFixPt) >> 15;
// write every nth as y with a corresponding x-coordinate
if (i % downsample == 0) {
*outPtr++ = short(--x);

View file

@ -17,17 +17,43 @@ public:
~Oscilloscope();
void addSamples(unsigned ch, short const* data, unsigned n);
void render(int x, int y);
static unsigned const MAX_CHANNELS = 3;
static unsigned const MAX_SAMPLES_PER_CHANNEL = 4096;
// Switches: On/Off, Stop Time
volatile bool enabled;
volatile bool inputPaused;
void setLowpass(float w) { assert(w > 0.0f && w <= 1.0f); _lowpassFactor = w; }
void setDownsampling(unsigned f) { assert(f > 0); _downsampleFactor = f; }
// Limits
static unsigned const MAX_CHANNELS = 3;
static unsigned const MAX_SAMPLES_PER_CHANNEL = 4096;
// Controls a simple one pole IIR low pass filter that is provided to
// reduce high frequencies aliasing (to lower ones) when downsampling.
//
// The parameter sets the influence of the input in respect to the
// feed-back signal on the output.
//
// +---------+
// in O--------------|+ ideal |--o--------------O out
// .---|- op amp | |
// | +---------+ |
// | |
// o-------||-------o
// | |
// | __V__
// -------------|_____|-------+
// : : |
// 0.0 - 1.0 (GND)
//
// The values in range 0.0 - 1.0 correspond to "all closed" (input has
// no influence on the output) to "all open" (feedback has no influence
// on the output) configurations.
void setLowpassOpenness(float w) { assert(w >= 0.0f && w <= 1.0f); _lowPassCoeff = w; }
// Sets the number of input samples per output sample. Without filtering
// just uses every nTh sample.
void setDownsampleRatio(unsigned n) { assert(n > 0); _downsampleRatio = n; }
private:
// don't copy/assign
@ -42,8 +68,8 @@ private:
short* _vertices;
unsigned _writePos[MAX_CHANNELS];
float _lowpassFactor;
unsigned _downsampleFactor;
float _lowpassCoeff;
unsigned _downsampleRatio;
};
#endif /* defined(__interface__oscilloscope__) */