Measuring/computing waveform linearity - Page 1
Forum section: electronics / projects

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 04:48:21 am
content: 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 whatexactlyis 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:% 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. = peakfinder (v, [], [], -1); = peakfinder (v, [], [], 1);minimum = minima(2);maximum = maxima(2);span = sort ([minimum maximum]);% Grab the section between these pointssection_t = t(span(1):span(2));section_v = v(span(1):span(2));% A zero will cause division error, so shift the data above zerovpp = 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 linep = polyfit (section_t, section_v, 1);poly_v = polyval (p, section_t);% Residualsresid = (section_v - poly_v);plot (section_t, resid);title ('Absolute Nonlinearity');pause;% Repeat for partial wavet_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;

author: madshaman
author_rank: Frequent Contributor
timestamp: October 05, 2013, 06:01:59 am
content: 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_rangewhere 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

author: ElectroIrradiator
author_rank: Frequent Contributor
timestamp: October 05, 2013, 07:56:02 am
content: Root-mean-square deviationis frequently used to quantify errors of this type. It gives you a mathematical tool to help compare different adjustments, IE. which one is 'best'.

author: quantumvolt
author_rank: Frequent Contributor
timestamp: October 05, 2013, 12:16:07 pm
content: 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 herehttp://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.

author: Marco
author_rank: Super Contributor
timestamp: October 05, 2013, 12:37:41 pm
content: ADCs and DACs often quoteINLas ppm of full scale magnitude, so in the local context that probably "is" linearity.

author: madshaman
author_rank: Frequent Contributor
timestamp: October 05, 2013, 12:39:24 pm
content: 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 includedb) 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)^2I think there are three metrics here (and likely more):1) the linear error of signal vs the ideal; what I mentioned2) 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 mentioned3) the harmonic distortion which gives an idea of how linearly the system responds (in this case the "input signal" is implicit

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 12:45:28 pm
content: Thanks for responding! I wasn't so confident I'd catch anyone with such a boring topicEverything 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...

author: dannyf
author_rank: Super Contributor
timestamp: October 05, 2013, 12:47:30 pm
content: Square-root of the sum of residuals squared from a linear regression.

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 12:49:02 pm
content: Yeah, so RMS deviation.Or not quite... I just woke upAs you can all probably tell, statistics was never my strong pointWouldn'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.

author: sync
author_rank: Frequent Contributor
timestamp: October 05, 2013, 01:23:02 pm
content: 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.

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 01:48:51 pm
content: OK, this is what I've got so far.Code:% 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. = peakfinder (v, [], [], -1); = peakfinder (v, [], [], 1);minimum = minima(2);maximum = maxima(2);span = sort ([minimum maximum]);% Grab the section between these pointssection_t = t(span(1):span(2));section_v = v(span(1):span(2));% Remove DC offset for RMS calculationsvpp = 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 linep = polyfit (section_t, section_v, 1);poly_v = polyval (p, section_t);% Residualsresid = (section_v - poly_v);% Calculate INL, RMS deviationinl = 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 nonlinearityxcent = (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');Quote from: sync on October 05, 2013, 01:23:02 pmThat 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?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.

author: sync
author_rank: Frequent Contributor
timestamp: October 05, 2013, 02:03:45 pm
content: Quote from: c4757p on October 05, 2013, 01:48:51 pmYou can build a external sample and hold circuit.QuoteUse 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.

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 02:07:50 pm
content: Both very good points.Quote from: c4757p on October 05, 2013, 01:48:51 pmAnd 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...

author: sync
author_rank: Frequent Contributor
timestamp: October 05, 2013, 02:29:14 pm
content: 0.24% looks good for a 8 bit ADC. That's less than one LSB.

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 02:33:50 pm
content: Quote from: sync on October 05, 2013, 02:29:14 pmYep... 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**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...

author: sync
author_rank: Frequent Contributor
timestamp: October 05, 2013, 03:57:39 pm
content: 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, ...

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 05:03:33 pm
content: 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)

author: jpb
author_rank: Super Contributor
timestamp: October 05, 2013, 05:11:30 pm
content: 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 aremissing 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-linearityis quoted only as a deviation over 95% of the wave not all of it.As with all statistics people quote/measure what suits best!

author: madshaman
author_rank: Frequent Contributor
timestamp: October 05, 2013, 06:37:55 pm
content: 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.

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 06:40:25 pm
content: 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.

author: madshaman
author_rank: Frequent Contributor
timestamp: October 05, 2013, 06:46:18 pm
content: 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.

author: c4757p
author_rank: Super Contributor
timestamp: October 05, 2013, 06:48:52 pm
content: 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.

author: madshaman
author_rank: Frequent Contributor
timestamp: October 05, 2013, 07:13:30 pm
content: Quote from: c4757p on October 05, 2013, 06:48:52 pmI hear ya! You never know tho, I nabbed an R&S SMIQ 03B that way ^^', still haven't tested it yet.