From 7cb3d05834c0117a988092886ff50bb60ef278a7 Mon Sep 17 00:00:00 2001 From: Anders Lindvall Date: Fri, 25 Apr 2014 14:41:51 +0200 Subject: [PATCH] Small adjustment to the alignment commit (01a3af8d6a). Added compensation for the delay in the analog hardware so that the analog and digital signals are correctly aligned. --- app/device/labtool/labtoolcapturedevice.cpp | 90 ++++++++++++++------- app/device/labtool/labtoolcapturedevice.h | 6 ++ 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/app/device/labtool/labtoolcapturedevice.cpp b/app/device/labtool/labtoolcapturedevice.cpp index 7835a52..fc1a903 100644 --- a/app/device/labtool/labtoolcapturedevice.cpp +++ b/app/device/labtool/labtoolcapturedevice.cpp @@ -389,6 +389,58 @@ void LabToolCaptureDevice::calibrate(QWidget *parent) } } +/*! + Removes \a numToRemove elements from the \a s list of signal samples. + The parameter \a removeFromStart dictates if the samples should be + removed from the start or end of the list. If the list contains + fewer than \a numToRemove elements all will be removed. +*/ +template +void LabToolCaptureDevice::trimSignalData(QVector *s, int numToRemove, bool removeFromStart) const +{ + if ((s != NULL) && (numToRemove > 0)) { + + if (s->size() <= numToRemove) { + s->clear(); + } else if (removeFromStart) { + s->remove(0, numToRemove); + } else { + s->remove(s->size() - numToRemove, numToRemove); + } + } +} + +/*! + Compensates for the delay in the analog hardware so that the analog and + digital signals line up. A few samples are removed from the start of the + signal (if digital) or end of the signal (if analog). This compensation + is only needed when both analog and digital signals are sampled. + The parameter \a s is the list of samples, parameter \a isAnalogSignal + is used to determine if the samples should be removed from the start + or end of the list. +*/ +template +void LabToolCaptureDevice::compensateForAnalogHardware(QVector *s, bool isAnalogSignal) const +{ + if (!mAnalogSignalList.isEmpty() && !mDigitalSignalList.isEmpty()) { + int numToRemove = 0; + + // The delay is roughly 200ns but was adjusted in detail using an + // external oscilloscope. As there is a limit on maximum sample + // rate when sampling both analog and digital signals of 20MHz + // there is no point in correcting higher sample rates. For sample + // rates below 5MHz the resolution is already below 200ns so + // correcting even one sample is too much. + switch (mUsedSampleRate) { + case 5000000: numToRemove = 3; break; + case 10000000: numToRemove = 4; break; + case 20000000: numToRemove = 5; break; + } + + trimSignalData(s, numToRemove, !isAnalogSignal); + } +} + /*! Scans the list of digital samples and locates the first entry with the correct level and returns it's index. The parameter \a s is the list of digital @@ -692,24 +744,15 @@ void LabToolCaptureDevice::convertDigitalInput(const quint8 *pData, quint32 size } } + // Compensate for the delay in the analog hardware so that the analog and digital signals line up. + compensateForAnalogHardware(s, false); + if (signalTrim < 0) { // remove abs(signalTrim) samples from the start of the data - if (s->size() >= -signalTrim) { - s->remove(0, -signalTrim); - //qDebug("D: Trim %d, removed %d samples from the start of the digital data", signalTrim, -signalTrim); - } else { - //qDebug("D: Trim %d, removed all %d samples from the digital data", signalTrim, s->size()); - s->clear(); - } + trimSignalData(s, -signalTrim, true); } else if (signalTrim > 0){ // remove abs(signalTrim) samples from the end of the data - if (s->size() >= signalTrim) { - s->remove(s->size()-signalTrim-1, signalTrim); - //qDebug("D: Trim %d, removed %d samples from the end of the digital data", signalTrim, signalTrim); - } else { - //qDebug("D: Trim %d, removed all %d samples from the digital data", signalTrim, s->size()); - s->clear(); - } + trimSignalData(s, signalTrim, false); } @@ -1052,24 +1095,15 @@ void LabToolCaptureDevice::convertAnalogInput(const quint8 *pData, quint32 size, if (mAnalogSignalData[id] == NULL) continue; + // Compensate for the delay in the analog hardware so that the analog and digital signals line up. + compensateForAnalogHardware(mAnalogSignalData[id], true); + if (signalTrim < 0) { // remove abs(signalTrim) samples from the start of the data - if (mAnalogSignalData[id]->size() >= -signalTrim) { - mAnalogSignalData[id]->remove(0, -signalTrim); - //qDebug("A: Trim %d, removed %d samples from the start of the analog data", signalTrim, -signalTrim); - } else { - //qDebug("A: Trim %d, removed all %d samples from the analog data", signalTrim, mAnalogSignalData[id]->size()); - mAnalogSignalData[id]->clear(); - } + trimSignalData(mAnalogSignalData[id], -signalTrim, true); } else if (signalTrim > 0){ // remove abs(signalTrim) samples from the end of the data - if (mAnalogSignalData[id]->size() >= signalTrim) { - mAnalogSignalData[id]->remove(mAnalogSignalData[id]->size()-signalTrim-1, signalTrim); - //qDebug("A: Trim %d, removed %d samples from the end of the analog data", signalTrim, -signalTrim); - } else { - //qDebug("A: Trim %d, removed all %d samples from the analog data", signalTrim, mAnalogSignalData[id]->size()); - mAnalogSignalData[id]->clear(); - } + trimSignalData(mAnalogSignalData[id], signalTrim, false); } // Deallocation: diff --git a/app/device/labtool/labtoolcapturedevice.h b/app/device/labtool/labtoolcapturedevice.h index 601d593..43a5f12 100644 --- a/app/device/labtool/labtoolcapturedevice.h +++ b/app/device/labtool/labtoolcapturedevice.h @@ -98,6 +98,12 @@ private slots: QTimer* mReconfigTimer; + template + void trimSignalData(QVector *s, int numToRemove, bool removeFromStart) const; + + template + void compensateForAnalogHardware(QVector *s, bool isAnalogSignal) const; + int locateFirstLevel(QVector *s, int level, int offset); int locatePreviousLevel(QVector *s, int level, int offset);