Author Topic: Measuring/computing waveform linearity  (Read 7594 times)

0 Members and 1 Guest are viewing this topic.

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Measuring/computing waveform linearity
« on: October 05, 2013, 04:48:21 am »
I can't seem to find much definitive information on the best way to measure the linearity of a slope waveform (triangle, sawtooth, etc.). My best attempt was to save the (heavily averaged) waveform off my DSO, then import into MATLAB, select one half-period, fit a line to that, then compute the residuals.

Just for my own curiosity, this does it on the complete half-period, then does it again after dropping 5% at the beginning and end so that the bit of rounding at the peaks isn't considered.

So... my first instinct was to convert those residuals to percentages. Linearity is always shown as a percentage. But this gives numbers that don't quite make sense - the absolute error is roughly constant over the entire period, so this just magnifies it at lower signal levels. That seems pretty useless - I don't see the point of a measurement that comes out differently if you invert the data! It's more an analysis of how you measured the signal than of the signal itself...

And what exactly is the scalar linearity measurement - average? Maximum? Something else?

Does anybody know of any good reading on this subject? Or is anybody able to quickly point out my stupid mistake?

Disclaimer: Not the best MATLAB code. It's been a while.

Code: [Select]
% Read the file!
file = csvread ('TEK00000.CSV');
t = file(:, 1);
v = file(:, 2);

plot (t, v);
title ('Waveform');
pause;

% Find the maxima and minima. We'll use the SECOND peaks, to avoid mistaking
% the start of the waveform for a peak.
[minima] = peakfinder (v, [], [], -1);
[maxima] = peakfinder (v, [], [], 1);

minimum = minima(2);
maximum = maxima(2);
span = sort ([minimum maximum]);

% Grab the section between these points
section_t = t(span(1):span(2));
section_v = v(span(1):span(2));

% A zero will cause division error, so shift the data above zero
vpp = max(section_v) - min(section_v);
section_v = section_v - min(section_v) + (0.05 * vpp);

plot (section_t, section_v);
title ('Section of Interest');
pause;

% Find the nearest line
p = polyfit (section_t, section_v, 1);
poly_v = polyval (p, section_t);

% Residuals
resid = (section_v - poly_v);

plot (section_t, resid);
title ('Absolute Nonlinearity');
pause;

% Repeat for partial wave
t_start = floor (length (section_t) * 0.05);
t_end = length (section_t) - 2 * t_start;
psection_t = section_t(t_start:t_end);
psection_v = section_v(t_start:t_end);
plot (psection_t, psection_v);
title ('Partial Section of Interest');
pause;

p = polyfit (psection_t, psection_v, 1);
ppoly_v = polyval (p, psection_t);

presid = (psection_v - ppoly_v);

plot (psection_t, presid);
title ('Partial Absolute Nonlinearity');

pause;
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline madshaman

  • Frequent Contributor
  • **
  • Posts: 698
  • Country: ca
  • ego trans insani
Measuring/computing waveform linearity
« Reply #1 on: October 05, 2013, 06:01:59 am »
I don't actually know the answer, linearity can refer to many things, but in this case I believe it's simply:

100 x max( abs( f(t) - g(t) )/val_range

where f(t) is your sample g(t) is your fit line and val_range = p2p magnitude of your signal.  Basically just the linear error of your most errored sample.

They might mean the mean linear error (doubt it) which is just as obvious to compute
To be responsible, but never to let fear stop the imagination.
 

Offline ElectroIrradiator

  • Frequent Contributor
  • **
  • Posts: 614
  • Country: dk
  • More analog than digital.
Re: Measuring/computing waveform linearity
« Reply #2 on: October 05, 2013, 07:56:02 am »
Root-mean-square deviation is frequently used to quantify errors of this type. It gives you a mathematical tool to help compare different adjustments, IE. which one is 'best'.
 

Offline quantumvolt

  • Frequent Contributor
  • **
  • Posts: 395
  • Country: th
Re: Measuring/computing waveform linearity
« Reply #3 on: October 05, 2013, 12:16:07 pm »
Linearity in this case must be based on a constant rate of change (derivative) between vertical/ horizontal values as in Y=aX+b. Nonlinearity for a slope is presence of 2nd or higher order shapes. Linearity at the peak cannot be defined (a negative pulse with infinite spectrum brings the curve back to the base for a new iteration. A practical way is proposed on pg. 40 here http://www.navymars.org/national/training/nmo_courses/nmo1/module9/14181_ch3.pdf. You just have to look around the web for things related to TV deflection, sweep etc. for operational formulas.
 

Offline Marco

  • Super Contributor
  • ***
  • Posts: 6921
  • Country: nl
Re: Measuring/computing waveform linearity
« Reply #4 on: October 05, 2013, 12:37:41 pm »
ADCs and DACs often quote INL as ppm of full scale magnitude, so in the local context that probably "is" linearity.
« Last Edit: October 05, 2013, 12:39:26 pm by Marco »
 

Offline madshaman

  • Frequent Contributor
  • **
  • Posts: 698
  • Country: ca
  • ego trans insani
Measuring/computing waveform linearity
« Reply #5 on: October 05, 2013, 12:39:24 pm »
If he wants a percentage though, rms won't give one.

Linear error usually means comparing the error of a device against a mathematical ideal against the total measurement range.  Although something tells me:

a) the "curved" sections at the peaks should be included

b) he really wants a metric for how straight the line in a waveform with constant slope sections, and there I agree that rms is good metric.

If you compute the rms voltage of the signal (Signal_rms) and the rms of the error (Err_rms), you can also compute the THD as:

(Err_rms/Signal_rms)^2

I think there are three metrics here (and likely more):

1) the linear error of signal vs the ideal; what I mentioned
2) the rms which expresses the deviation of the signal from an ideal function (in this case a line), so gives an idea of how "line-like" the signal is; what you mentioned
3) the harmonic distortion which gives an idea of how linearly the system responds (in this case the "input signal" is implicit
To be responsible, but never to let fear stop the imagination.
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #6 on: October 05, 2013, 12:45:28 pm »
Thanks for responding! I wasn't so confident I'd catch anyone with such a boring topic ;D

Everything said makes sense to me. I think, since there are a few "competing" ways to make this measurement, perhaps it would be best to make and list all of them. No harm in additional data...
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Measuring/computing waveform linearity
« Reply #7 on: October 05, 2013, 12:47:30 pm »
Square-root of the sum of residuals squared from a linear regression.
================================
https://dannyelectronics.wordpress.com/
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #8 on: October 05, 2013, 12:49:02 pm »
Yeah, so RMS deviation. Or not quite... I just woke up :P

As you can all probably tell, statistics was never my strong point :-DD

Wouldn't it be better to do a proper RMS and average them rather than sum? Surely the result shouldn't be proportional to the number of data points I have.
« Last Edit: October 05, 2013, 01:05:23 pm by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline sync

  • Frequent Contributor
  • **
  • Posts: 799
  • Country: de
Re: Measuring/computing waveform linearity
« Reply #9 on: October 05, 2013, 01:23:02 pm »
Look at the HP 3312A and 3325A manuals (performance tests). They basically use a very fast (S/H) DMM sampling different portions of the waveform.
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #10 on: October 05, 2013, 01:48:51 pm »
OK, this is what I've got so far.

Code: [Select]
% Read the file!
file = csvread ('TEK00000.CSV');
t = file(:, 1);
v = file(:, 2);

plot (t, v);
title ('Waveform');
pause;

% Find the maxima and minima. We'll use the SECOND peaks, to avoid mistaking
% the start of the waveform for a peak.
[minima] = peakfinder (v, [], [], -1);
[maxima] = peakfinder (v, [], [], 1);

minimum = minima(2);
maximum = maxima(2);
span = sort ([minimum maximum]);

% Grab the section between these points
section_t = t(span(1):span(2));
section_v = v(span(1):span(2));

% Remove DC offset for RMS calculations
vpp = max(section_v) - min(section_v);
section_v = section_v - mean (section_v);

plot (section_t, section_v);
title ('Section of Interest');
pause;

% Find the nearest line
p = polyfit (section_t, section_v, 1);
poly_v = polyval (p, section_t);

% Residuals
resid = (section_v - poly_v);

% Calculate INL, RMS deviation
inl = max (section_v - poly_v);
inl_pct = 100 * inl / vpp;
rms_dev = sqrt (mean ((section_v - poly_v) .^ 2));
rms_wave = sqrt (mean (section_v .^ 2));
rms_pct = 100 * rms_dev / rms_wave;

% Display nonlinearity
xcent = (max (section_t) + min (section_t)) / 2;
ytop = max (resid) * 0.9;

plot (section_t, resid);
title ('Absolute Nonlinearity');
text (xcent, ytop, sprintf ('INL = %.4fV on %.2fV = %.2f%%\n', inl, ...
    vpp, inl_pct), ...
    'HorizontalAlignment', 'center');
text (xcent, ytop * 0.9, sprintf (...
    'RMS distortion = %.4fV on %.2fV = %.2f%%\n', rms_dev, rms_wave, ...
    rms_pct), 'HorizontalAlignment', 'center');

Look at the HP 3312A and 3325A manuals (performance tests). They basically use a very fast (S/H) DMM sampling different portions of the waveform.

That would work, and I'm sure I could rig up something similar. But I'd need to find a way to reliably trigger one of my voltmeters at the right point (none of them has a logic-level external trigger input, though they can do it over serial). It would be a pretty complicated setup for me, and it seems like it wouldn't be as necessary now as it was in 1974/78... It looks like my TDS-380 gives enough precision in its CSV output to do the calculation, I just have to turn the averaging to the maximum number of samples so the noise is removed.

I like that method as well because I can also look at the linearity at higher frequencies.

Obviously that calls into question the linearity of the scope's ADC... Isn't the world of measurement wonderful? |O

Speaking of the 3325A... that's perfect, because that is the function generator I used to generate the test pattern. I have no reason to believe that mine is out of spec at all. I think I'll try to replicate their test conditions and see if I come up with something similar.
« Last Edit: October 05, 2013, 02:01:00 pm by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline sync

  • Frequent Contributor
  • **
  • Posts: 799
  • Country: de
Re: Measuring/computing waveform linearity
« Reply #11 on: October 05, 2013, 02:03:45 pm »
That would work, and I'm sure I could rig up something similar. But I'd need to find a way to reliably trigger one of my voltmeters at the right point (none of them has a logic-level external trigger input, though they can do it over serial).
You can build a external sample and hold circuit.

Quote
Obviously that calls into question the linearity of the scope's ADC... Isn't the world of measurement wonderful? |O
Use a inverting amp and compare the measurements. This isn't perfect but can give you some confidence. And add some DC level. This should move the scope linearity error relative to the generator one.
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #12 on: October 05, 2013, 02:07:50 pm »
Both very good points.

Speaking of the 3325A... that's perfect, because that is the function generator I used to generate the test pattern. I have no reason to believe that mine is out of spec at all. I think I'll try to replicate their test conditions and see if I come up with something similar.

And a good experiment to try. Because rather than anything near the 0.05% that I should be seeing, I'm getting 0.24% INL under those test conditions! Well, I guess that's that, this method is useless. ::) Even trying to cancel out the scope's nonlinearity with inversions and shifts and whatnot, I can't see pulling a 0.05% signal out of that 0.24% noise...
« Last Edit: October 05, 2013, 02:13:19 pm by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline sync

  • Frequent Contributor
  • **
  • Posts: 799
  • Country: de
Re: Measuring/computing waveform linearity
« Reply #13 on: October 05, 2013, 02:29:14 pm »
0.24% looks good for a 8 bit ADC. That's less than one LSB.
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #14 on: October 05, 2013, 02:33:50 pm »
0.24% looks good for a 8 bit ADC. That's less than one LSB.

Yep... I was hoping it would turn out better with 256 averages. I guess not.* I think I'll grab myself a couple LF398A and see if I can put together a decent sample-and-hold system. (Does anybody know of a better SHA that doesn't cost $30 in one-off quantity...? Analog Devices has a couple good-looking chips if you want to spend $50 on a goddamn amplifier... :-\)

*The values in the CSV file are the typical floating-point format which gives no indication of the precision, and I can't find any information in the manual on the precision of saved waveforms in averaging mode. It looked at a glance to be better than 8 bit. Perhaps I should count the unique values - I'll probably come up with 256 |O

**995 unique values (and I was close to maximizing the ADC range), so roughly 10-bit. Well... it's better than 8-bit... but 256 averages should give me 16 bits :-\

***And as the coffee finally kicks in this morning I'm realizing how stupid it was to think that averaging would magically make the ADC resolution go away. Whoops... there's 10 IQ points...
« Last Edit: October 05, 2013, 02:45:45 pm by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline sync

  • Frequent Contributor
  • **
  • Posts: 799
  • Country: de
Re: Measuring/computing waveform linearity
« Reply #15 on: October 05, 2013, 03:57:39 pm »
I would try a good sound card. Perhaps it's just working.

Another idea. Maybe totally impractical. Use your 3325A to generate a inverse waveform of your DUT. Add them with two resistors. Ideally the waves will cancel each other out. So you can use the scope at high gain levels. But the generators have to be synced, the amplitude must be stable, ...
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #16 on: October 05, 2013, 05:03:33 pm »
I like the sound card idea, I always forget that's an option. I think I've got a decent USB sound card lying around from an old project - might be able to modify that to work.

Pretty sure the inverse waveform idea is impractical - neither the 3325A nor the DUT has phase lock capability. (Well, the 3325A can phase lock to a reference, but it has to be one of the predefined references - IIRC, 1, 2, 5, 10 MHz)
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline jpb

  • Super Contributor
  • ***
  • Posts: 1771
  • Country: gb
Re: Measuring/computing waveform linearity
« Reply #17 on: October 05, 2013, 05:11:30 pm »
I guess the issue with taking an RMS error is this might reflect errors in the time axis as well as amplitude.

You probably want to adjust the time scale/frequency to minimize the RMS error.

For example with a square wave, a small amount of jitter (in the time domain) will lead to sharp peaks in error in the amplitude (voltage) domain.

I think with triangular waves it is normally assumed that the time axis is perfect and the non-linearity arises because the higher order harmonics are
missing or attenuated so the sharp points are rounded but as this affects the pointy bits of the waveform more than the linear part the non-linearity
is quoted only as a deviation over 95% of the wave not all of it.

As with all statistics people quote/measure what suits best!
 

Offline madshaman

  • Frequent Contributor
  • **
  • Posts: 698
  • Country: ca
  • ego trans insani
Measuring/computing waveform linearity
« Reply #18 on: October 05, 2013, 06:37:55 pm »
Warning, most sound cards are chocked full of distortion.  Some are very good, like the Apogee Ensemble we use for our band, but they're not cheap.  So not cheap that I'm going to build my next audio interface instead buy it.

Not only do you get harmonic distortion from the card's non-linear response, you get phase distortion from your soundcard's unstable (relatively) clock source.

Pro gear like the ensemble let you supply an external word clock to overcome this and usually have decent clock sources built in.
To be responsible, but never to let fear stop the imagination.
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #19 on: October 05, 2013, 06:40:25 pm »
I think I'll go the sample-and-hold route. That'll be useful for other things in the future, too. I'll just throw a microcontroller on there to lock to the frequency of the signal and select a sample phase.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline madshaman

  • Frequent Contributor
  • **
  • Posts: 698
  • Country: ca
  • ego trans insani
Measuring/computing waveform linearity
« Reply #20 on: October 05, 2013, 06:46:18 pm »
You need a DSA :-), I found a "cheap" one on eBay (HP3562A) for about $500.

It works perfectly and I *still* haven't explored all its built-in measurement programs, *and* it lets you program your own (it's like a self-contained scriptable automated test instrument).

I highly recommend nabbing one at some point.
To be responsible, but never to let fear stop the imagination.
 

Offline c4757pTopic starter

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Measuring/computing waveform linearity
« Reply #21 on: October 05, 2013, 06:48:52 pm »
I'd love one. As a student, $500 is well within "that could buy me gas to school for almost three months!" territory, so that's on my "holy shit, that auction's about to close at $5 without a single bid!" list.

Sadly, this list has a large intersection with the "shipping costs $1500, your left forearm and your soul" list.
« Last Edit: October 05, 2013, 06:53:45 pm by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline madshaman

  • Frequent Contributor
  • **
  • Posts: 698
  • Country: ca
  • ego trans insani
Measuring/computing waveform linearity
« Reply #22 on: October 05, 2013, 07:13:30 pm »

I'd love one. As a student, $500 is well within "that could buy me gas to school for almost three months!" territory, so that's on my "holy shit, that auction's about to close at $5 without a single bid!" list.

Sadly, this list has a large intersection with the "shipping costs $1500, your left forearm and your soul" list.

I hear ya!  You never know tho, I nabbed an R&S  SMIQ 03B that way ^^', still haven't tested it yet.
To be responsible, but never to let fear stop the imagination.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf