Skip to content

Commit 66849fe

Browse files
wagenadlmedengineer
authored andcommitted
Slight improvement to plotting in LfpViewer Beta: Canvas now plots average rather than two-point interpolation if there are two or more samples per pixel.
1 parent 3eb2811 commit 66849fe

1 file changed

Lines changed: 29 additions & 37 deletions

File tree

Plugins/LfpDisplayNodeBeta/LfpDisplayCanvas.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void LfpDisplayCanvas::updateScreenBuffer()
323323

324324
if (nSamples < 0) // buffer has reset to 0 -- xxx 2do bug: this shouldnt happen because it makes the range/histogram display not work properly/look off for one pixel
325325
{
326-
nSamples = (displayBufferSize - dbi) + index +1;
326+
nSamples += displayBufferSize;
327327
// std::cout << "nsamples 0 " ;
328328
}
329329

@@ -333,16 +333,15 @@ void LfpDisplayCanvas::updateScreenBuffer()
333333

334334
float ratio = sampleRate[channel] * timebase / float(getWidth() - leftmargin - scrollBarThickness); // samples / pixel
335335
// this number is crucial: converting from samples to values (in px) for the screen buffer
336-
int valuesNeeded = (int) float(nSamples) / ratio; // N pixels needed for this update
336+
int valuesNeeded(nSamples / ratio); // N pixels needed for this update
337337

338338
if (sbi + valuesNeeded > maxSamples) // crop number of samples to fit canvas width
339339
{
340340
valuesNeeded = maxSamples - sbi;
341341
}
342342
float subSampleOffset = 0.0;
343343

344-
dbi %= displayBufferSize; // make sure we're not overshooting
345-
int nextPos = (dbi + 1) % displayBufferSize; // position next to displayBufferIndex in display buffer to copy from
344+
// dbi %= displayBufferSize; // make sure we're not overshooting
346345

347346
// if (channel == 0)
348347
// std::cout << "Channel "
@@ -362,40 +361,39 @@ void LfpDisplayCanvas::updateScreenBuffer()
362361
if (!lfpDisplay->isPaused)
363362
{
364363
float gain = 1.0;
365-
float alpha = (float) subSampleOffset;
366-
float invAlpha = 1.0f - alpha;
367364

368365
screenBuffer->clear(channel, sbi, 1);
369366
screenBufferMin->clear(channel, sbi, 1);
370367
screenBufferMax->clear(channel, sbi, 1);
371368

372-
dbi %= displayBufferSize; // just to be sure
373-
374-
// interpolate between two samples with invAlpha and alpha
375-
screenBuffer->addFrom(channel, // destChannel
376-
sbi, // destStartSample
377-
displayBuffer->getReadPointer(channel, dbi), // source
378-
1, // numSamples
379-
invAlpha*gain); // gain
380-
381-
382-
screenBuffer->addFrom(channel, // destChannel
383-
sbi, // destStartSample
384-
displayBuffer->getReadPointer(channel, nextPos), // source
385-
1, // numSamples
386-
alpha*gain); // gain
369+
// dbi %= displayBufferSize; // just to be sure
387370

388371
// same thing again, but this time add the min,mean, and max of all samples in current pixel
389372
float sample_min = 10000000;
390373
float sample_max = -10000000;
391374

392-
int nextpix = (dbi +(int)ratio +1) % (displayBufferSize+1); // position to next pixels index
393-
394-
if (nextpix <= dbi) { // at the end of the displaybuffer, this can occur and it causes the display to miss one pixel woth of sample - this circumvents that
395-
// std::cout << "np " ;
396-
nextpix=dbi;
375+
int nextpix = dbi + int(ceil(ratio));
376+
if (nextpix > displayBufferSize)
377+
nextpix = displayBufferSize;
378+
379+
if (nextpix - dbi > 1) {
380+
// multiple samples, calculate average
381+
float sum = 0;
382+
for (int j = dbi; j < nextpix; j++)
383+
sum += displayBuffer->getSample(channel, j);
384+
screenBuffer->addSample(channel, sbi, sum*gain / (nextpix - dbi));
385+
} else {
386+
// interpolate between two samples with invAlpha and alpha
387+
/* This is only reasonable if there are more pixels
388+
than samples. Otherwise, we should calculate average. */
389+
float alpha = (float) subSampleOffset;
390+
float invAlpha = 1.0f - alpha;
391+
float val0 = displayBuffer->getSample(channel, dbi);
392+
float val1 = displayBuffer->getSample(channel, (dbi+1)%displayBufferSize);
393+
float val = invAlpha * val0 + alpha * val1;
394+
screenBuffer->addSample(channel, sbi, val*gain);
397395
}
398-
396+
399397
for (int j = dbi; j < nextpix; j++)
400398
{
401399

@@ -420,7 +418,7 @@ void LfpDisplayCanvas::updateScreenBuffer()
420418
if (channel < nChans) // we're looping over one 'extra' channel for events above, so make sure not to loop over that one here
421419
{
422420
int c = 0;
423-
for (int j = dbi; j < nextpix & c < MAX_N_SAMP_PER_PIXEL; j++)
421+
for (int j = dbi; j < nextpix && c < MAX_N_SAMP_PER_PIXEL; j++)
424422
{
425423
float sample_current = displayBuffer->getSample(channel, j);
426424
samplesPerPixel[channel][sbi][c]=sample_current;
@@ -442,15 +440,9 @@ void LfpDisplayCanvas::updateScreenBuffer()
442440

443441
subSampleOffset += ratio;
444442

445-
while (subSampleOffset >= 1.0)
446-
{
447-
if (++dbi > displayBufferSize)
448-
dbi = 0;
449-
450-
nextPos = (dbi + 1) % displayBufferSize;
451-
subSampleOffset -= 1.0;
452-
}
453-
443+
int steps(floor(subSampleOffset));
444+
dbi = (dbi + steps) % displayBufferSize;
445+
subSampleOffset -= steps;
454446
}
455447

456448
// update values after we're done

0 commit comments

Comments
 (0)