Bode Plot from Swept Sine Input and Scope Captured Data.........  - Page 1
Forum section: electronics / projects

author: Smokey
author_rank: Super Contributor
timestamp: October 13, 2023, 07:21:40 am
content: I've always thought the typical Bode plot method of a series of discrete fixed frequency input steps with data capture at those steps is pretty coarse. And it takes forever.Conceptually I get that a swept sine wave input signal will rather quickly go through the whole frequency range without skipping any potential resonances or other issues the discrete steps could miss. If you capture the continuous input and output waveforms, that has all the information you need to construct a Bode Plot (with a little FFT magic mixed in there)...Well check this out. This dude put that swept sine Bode plot system together with some Matlab code, and a DSO-X 2024A:https://pages.hmc.edu/mspencer/e151/AutoBodeGuide.pdfCode included. Sweet. I'm not a Matlab guy, but it looks like the magic happens in the iddata and tfest functions.How hard would this would be to recreate in python? This would be a killer app for those 12 bit scopes.

author: moffy
author_rank: Super Contributor
timestamp: October 13, 2023, 07:35:29 am
content: Awesome looks very useful.

author: Someone
author_rank: Super Contributor
timestamp: October 13, 2023, 09:29:38 am
content: https://www.eevblog.com/forum/testgear/faster-fra-from-scope-by-external-control-over-visa/Thats "just" stepped sine waves, but it doesn't have to be slow. FFTs wont magically improve the speed or reduce noise, for low dynamic range the primary constraint is how long you capture for. Higher dynamic range with sine waves uses the scope front end to quickly/cheaply reduce noise. Dont bother with chirps unless you have low dynamic range or fast time correlation demands, may as well go for noise ahead of chirps in most use cases.

author: RoGeorge
author_rank: Super Contributor
timestamp: October 13, 2023, 10:37:40 am
content: Sweeping is good for speed, but in practice an oscilloscope will have very little dynamic range. If you need more than 10-20dB, than the only way to go is to step through frequency, then autorange for amplitude.Another aspect to consider if you go and implement the idea from this topic, will be an adjustable wait-time before measuring the amplitude/phase, for circuits with high Q.Resonators can ring for many ms up to many seconds even when they are swiped with slowly variable frequency. For example this 32768Hz resonator from a former wrist watch is swiped between 32750 and 32780Hz over 10 seconds. All those crests in the amplitude response are not really there. Those are artifacts because the resonator does not have enough time to settle.

author: Smokey
author_rank: Super Contributor
timestamp: October 13, 2023, 06:51:59 pm
content: Those are good points. But even with high Q circuits the swept sine would still be useful for a first pass over the full frequency range to identify where there are issues. That would allow you to dig in and target those ranges for higher resolution measurements.Also, about the dynamic range... I feel like a huge number of Bode plots are just used to find the 3dB point for something. As long as your circuit isn't super peaky, 8bit oscilloscopes should be fine for dynamic range.

author: mawyatt
author_rank: Super Contributor
timestamp: October 14, 2023, 12:28:42 am
content: Quote from: RoGeorge on October 13, 2023, 10:37:40 amVery good point about the effective "Q" of the DUT wrt the sweep speed. Often overlooked, one can "smear" the results, or completely obliterate the effects, with too fast a sweep or too wide a step with a high "Q" DUT. Long ago SPICE created what we called the "look ahead algorithm", where the time steps were increased to shorten simulation time. This often stepped over subtle circuit effects and created erroneous/misleading results, Cadence developed a much improved "look ahead algorithm" which caught hints of many of these effects, stepped back in time and did a more respectable simulation shortened time sweep across these subtle effects.Makes one consider if a similar concept could be employed wrt to Bode plots with DUT high "Q" effects. Since phase is making quick changes nearing a high "Q" zone, seems a clever algorithm could sense such, back up in frequency, then proceed slowly across the high "Q" zone, then return to the initial sweep rate. Of course if frequency stepping rather than sweeping, a similar concept would apply to the frequency step akin to the SPICE time steps mentioned.Reminds of sweeping across large frequency spans with narrowband filters trying to detect buried signals. With a respectable amount of math behind this, so might be a worthwhile project (Bode that is) for a PhD dissertation or post Doc effortBest,

author: CatalinaWOW
author_rank: Super Contributor
timestamp: October 14, 2023, 12:48:49 am
content: Since Q is only meaningful in second order or higher circuits it doesn't really require a very clever algorithm to find that a course step has skipped over something interesting. Just check for a phase difference of more than 90 degrees from the previous step. That plus an amplitude check to find the cases where Murphy caused one of the frequency steps to land on or very near a resonance should catch all but the most diabolical situations.

author: mawyatt
author_rank: Super Contributor
timestamp: October 14, 2023, 01:16:37 am
content: Quote from: CatalinaWOW on October 14, 2023, 12:48:49 amWell a simple quartz crystal might pose a problem since it can transition between series and parallel resonance in a very short frequency span and could easily be overlooked wrt > 90 degrees phase magnitude change between frequency steps over a wider range. Same goes for narrow band filters in wide band circuits/systems, with a relatively narrow phase change window over a broad frequency span, one can easily step or sweep over the area of interest if the steps are too large or the sweep too fast.For lower "Q" circuits, agree the phase change is more gradual and easily predictable with simple algorithms. This is very similar to the mentioned SPICE time stepping "Look Ahead Algorithm" which Ken Kundert solved brilliantly with Cadence. This would catch the most subtle circuit disturbances and glitches, whereas other flavors of SPICE would simply ignore and step over no matter how well you tried to "coach" them by massaging the various SPICE control parameters!!Anyway, that's a problem for other younger folks to consider, thus the dissertation mention (no students, retired, and not an adjunct prof anymore)!!Best,

author: CatalinaWOW
author_rank: Super Contributor
timestamp: October 14, 2023, 02:30:44 am
content: Quote from: mawyatt on October 14, 2023, 01:16:37 amAgree about your cases, but if you are dealing with such circuits you should be aware of their general character. Perhaps when reverse engineering black boxes this isn't true, but even reverse engineering usually physically examines the circuitry and has substantial clues about the nature of the circuit.

author: Smokey
author_rank: Super Contributor
timestamp: October 14, 2023, 03:37:54 am
content: How often are you guys actually running into a high Q circuit where this would be a problem? All of the Bode plots that I've generated have all been on amplifiers and this has never been an issue (that I've been aware of at least). I totally get that if characterizing crystals is your thing then you care, but is that not a minority?

author: RoGeorge
author_rank: Super Contributor
timestamp: October 14, 2023, 04:20:06 am
content: Quote from: mawyatt on October 14, 2023, 12:28:42 amInteresting idea.I wonder if that would be possible to implement all analog, with a feedback loop that measures the phase shift, and a VCO as a frequency generator. The analog feedback has to reduce the default frequency ramp of the VCO, such that the change in phase speed is kept under a specified value. The control loop has to ensure a default VCO slope, and a minimum slope, so the frequency sweep won't stop and won't go back if the phase flips.

author: iMo
author_rank: Super Contributor
timestamp: October 14, 2023, 07:23:52 am
content: The NanoVNA is using 16bit ADC (I/Q) only (no range switching), a square wave signal, it sweeps with constant freq step, and the results are ok even when measuring a "quartz" (you'll get nice serial and parallel resonance peaks, the dynamic range is of course limited by the ADC and its ENOB).PS: with Android Rigols you may install GNU Octave

author: RoGeorge
author_rank: Super Contributor
timestamp: October 14, 2023, 09:19:07 am
content: I guess that's possible without ringing because of the 50ohms load? I assume the low impedance load will dissipate very fast any energy stored in the resonator, so will dampen the ringing. Don't have a nanoVNA, that is only my wild guess.Probably won't be possible to measure a RTC resonator with nanoVNA (apart from the frequency range) because RTC resonators are in fact tuning-forks, very easy to overdrive or even brake (in comparison with a normal quartz cut). The 32768Hz resonators can only support very little power. Typical driving power must stay less than 1uW (and typical Q in the range of 50-100 thousands), while a normal quartz can take 100-1000 times more power.0.1uW on a 50 ohm load would be only 2.2mVRMS, hard to see on my scope, so I've used the probe itself as a load. The measuring circuit was the generator out (sinus) in series with a 10k (not sure about the value), then in series with the RTC resonator, then in series with the oscilloscope probe at 1x.

author: 2N3055
author_rank: Super Contributor
timestamp: October 14, 2023, 10:13:26 am
content: Quote from: Smokey on October 13, 2023, 06:51:59 pmDynamic range is gained by automatic input channel attenuation settings... But noise floor will be better with lower noise 12 bit (or more) systems.Picoscope has Hexamer's FRA app and that combined with 16 bit Picoscope does interesting work..

author: Martinn
author_rank: Frequent Contributor
timestamp: October 14, 2023, 12:17:33 pm
content: Quote from: Smokey on October 13, 2023, 07:21:40 amThey are using tfesthttps://ch.mathworks.com/help/ident/ref/tfest.htmlfor estimating a transfer function based on the sampled data. Besides the fact that for this particular matlab function, you'd need a system identification toolbox license, I'm not sure why you would want to do that.You only need closed models as laplace or z transform polynomials if you want to do system simulation, control system synthesis or maybe kalman filter design.Having used transfer function estimation based on swept sine or impulse response data for modal (vibration) analysis, I would keep clear of this, as higher order fits are highly unstable IIRC. Starting with - what are the number of poles and zeros you'd like to assign?Sure, for a simple RC this will work, but real life circuits are much more complex.So instead of trying to fit a polynomial to the sampled data, I'd either stay with stepped sine (my RTB2K does that, including automatically adjusting frontend gain/attenuation for each step) or if you want chirps, extract phase/amplitude from there, seehttps://www.thinksrs.com/downloads/PDFs/ApplicationNotes/SR1_SweptSine.pdfand avoid the detour via a transfer function fit.

author: David Hess
author_rank: Super Contributor
timestamp: October 14, 2023, 07:19:59 pm
content: A lower dynamic range alternative is to apply a step input, then differentiate the output, and then calculate an FFT which will return the magnitude and phase response. This is essentially a real time method although the low frequency limit depends on capturing for an equivalent duration in time.Of course you need an oscilloscope which will return the FFT phase information instead of discarding it.

author: Smokey
author_rank: Super Contributor
timestamp: October 14, 2023, 07:48:12 pm
content: Quote from: Martinn on October 14, 2023, 12:17:33 pmGreat post. I was curious about why they did the transfer function estimation. That seems unnecessary.With my hand wavy understanding of signals and systems, I had the FFT(input)/FFT(output) method in mind like you posted in that paper there. I had all the function gen/scope instrumentation and single shot input/output data collection set up at one point, but I could never get python to spit out anything that looked like the Bode plot I expected. I'm sure the problem was on my end, but it's worth pointing out that there are a few steps between "FFT(input)/FFT(output)" and a working python program that makes Bode plots.

author: Martinn
author_rank: Frequent Contributor
timestamp: October 14, 2023, 08:26:10 pm
content: Quote from: Smokey on October 14, 2023, 07:48:12 pmWho knows. Maybe there is some hidden magic in the tfest function that leads to more robust or accurate results compared the FFT version. I am not familiar with this particular function and the documentation does not enlighten us to its innards.But, as we are interested into the bode plot only (amplitude/phase), I honestly don't see the need for trying to squeeze out transfer function polynomials. Maybe if you would know the exact structure of your circuit (like a filter) and would like to extract a polynomial fit for that - but this is not the typical use case in a scope bode plot, I'd assume. My usage is mostly amplifier frequency response or power supply stability verification (3 dB cutoff or stability margin).Mainly - polynomial fitting loses data. So I'd rather see the raw amplitude/phase output than a polynomial fit.

author: RoGeorge
author_rank: Super Contributor
timestamp: October 14, 2023, 08:55:50 pm
content: The free equivalent of Matlab, Octave, has a tfestimate() function. By the name (never used either of tfest or tfestimate), I guess tfestimate from Octave is the similar of tfest from Matlab. In its doc pagehttps://octave.sourceforge.io/signal/function/tfestimate.htmlit is mentioned the method used:Quotehttps://en.wikipedia.org/wiki/Welch%27s_methodI see these estimators are available from Python, too, so the initial Matlab program should be possible to translate into Python code almost 1:1.To increase the dynamic range it should be possible to swipe the frequency repeatedly, then to overlap/average them together before calling the tfest().Should work, but I didn't try in practice any of these.

author: Smokey
author_rank: Super Contributor
timestamp: October 14, 2023, 08:58:35 pm
content: Quote from: Martinn on October 14, 2023, 08:26:10 pmYes, that!With that said...https://pypi.org/project/tfest/https://stackoverflow.com/questions/28462144/python-version-of-matlab-signal-toolboxs-tfestimate

author: MathWizard
author_rank: Super Contributor
timestamp: October 14, 2023, 11:22:07 pm
content: Why is it ok to be sweeping the frequency, rather that stopping on a bunch of steps ? Is the way it's sampled digitally or with analog, that it acts like taking a bunch of steps and it just doesn't matter that the frequency is sweeping ?? But there's more to it then, since at a bunch of steps, you would miss a bunch of interesting points. So yeah that's not it.

author: Martinn
author_rank: Frequent Contributor
timestamp: October 15, 2023, 08:46:08 am
content: Quote from: RoGeorge on October 14, 2023, 08:55:50 pmMatlab tfest and tfestimate are different things:sys = tfest(tt,np) estimates the continuous-time transfer function sys with np poles, using all the input and output signals in the timetable tt.https://ch.mathworks.com/help/ident/ref/tfest.htmltxy = tfestimate(x,y) finds a transfer function estimate between the input signal x and the output signal y evaluated at a set of frequencies.https://ch.mathworks.com/help/signal/ref/tfestimate.htmlThe Welch method simply calculates a power spectral density estimate for a given time discrete sample vector. You use it if you have a very long input vector and would calculate a shorter FFT, like 10M samples going into a 1024 point FFT. The input vector is split in FFT size chunks (typically with 50% overlap) and the resulting FFT results are averaged. Very useful detail is that you can apply detrending methods to the chunks in oder to clean up data with slow drifts that would otherwise contaminate the spectrum with the window function spectrum. I have used this quite successfully to dig out features from very noisy mesurements.The method cited in the initial post is tfest, which gives a continuous time transfer function in polynomial form. I did Mathworks an injustice when I claimed they would not explain how tfest works. I just missed that part in the docs:https://ch.mathworks.com/help/ident/ref/tfest.html#btfb8zb-1This also explains why one gladly pays for their toolbox licenses: Even just reading the cited papers will cost you more (if you are paid by the hour) than the toolbox license.tfest.pyhttps://github.com/giuliovv/tfest/blob/main/tfest/tfest.pylooks interesting.Quote from: MathWizard on October 14, 2023, 11:22:07 pmI'd say with stepped sine you deal with a single, constant known frequency measurement at a time. For this it is trivially easy (quadrature demodulation) and very robust to calculate amplitude and phase. Only question I see is when set and actual stimulus frequency don't match exactly, which would be the case if signal generator and oscilloscope run from different clocks (like 100 ppm apart). In an integrated instrument (RTB2K for example) I would expect that both ADC sampling and AWG output clock are derived from the same master clock, no problem there.With swept sine/chirp/noise inputs, the "decoding" (transfer function estimation) algorithm has to extract frequency, amplitude and phase information both from the stimulus and output vector (FFT methods as mentioned earlier), which is more sensitive to nonlinearities and dynamic range. So I guess it depends on the application. For mechanical system modal analysis, you very quickly run into nonlinearities due to assembly details or bearing preloads. For measuring the 3 dB cutoff of an opamp stage, probably anything will do. High Q circuits like the crystals mentioned earlier obviously also won't work using a fast chirp.