Author Topic: Bode Plot from Swept Sine Input and Scope Captured Data.........  (Read 3856 times)

0 Members and 1 Guest are viewing this topic.

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 3216
  • Country: us
  • Not An Expert
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.pdf

Code 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.
« Last Edit: October 14, 2023, 11:48:47 pm by Smokey »
 
The following users thanked this post: RoGeorge, RAPo

Online moffy

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #1 on: October 13, 2023, 07:35:29 am »
Awesome looks very useful. :)
 

Offline Someone

  • Super Contributor
  • ***
  • Posts: 5225
  • Country: au
    • send complaints here
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #2 on: October 13, 2023, 09:29:38 am »
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.
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 7174
  • Country: ro
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #3 on: October 13, 2023, 10:37:40 am »
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.

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 3216
  • Country: us
  • Not An Expert
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #4 on: October 13, 2023, 06:51:59 pm »
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.
« Last Edit: October 13, 2023, 08:14:26 pm by Smokey »
 

Offline mawyatt

  • Super Contributor
  • ***
  • Posts: 4290
  • Country: us
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #5 on: October 14, 2023, 12:28:42 am »
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.

Very 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 effort :-+

Best,
« Last Edit: October 14, 2023, 12:31:35 am by mawyatt »
Curiosity killed the cat, also depleted my wallet!
~Wyatt Labs by Mike~
 
The following users thanked this post: RoGeorge

Offline CatalinaWOW

  • Super Contributor
  • ***
  • Posts: 5660
  • Country: us
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #6 on: October 14, 2023, 12:48:49 am »
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.
 

Offline mawyatt

  • Super Contributor
  • ***
  • Posts: 4290
  • Country: us
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #7 on: October 14, 2023, 01:16:37 am »
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.

Well 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,
« Last Edit: October 14, 2023, 01:27:57 am by mawyatt »
Curiosity killed the cat, also depleted my wallet!
~Wyatt Labs by Mike~
 

Offline CatalinaWOW

  • Super Contributor
  • ***
  • Posts: 5660
  • Country: us
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #8 on: October 14, 2023, 02:30:44 am »
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.

Well 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,

Agree 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.
 

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 3216
  • Country: us
  • Not An Expert
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #9 on: October 14, 2023, 03:37:54 am »
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? 
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 7174
  • Country: ro
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #10 on: October 14, 2023, 04:20:06 am »
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.

Interesting 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.

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5664
  • Country: gw
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #11 on: October 14, 2023, 07:23:52 am »
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 :)
« Last Edit: October 14, 2023, 07:31:59 am by iMo »
Readers discretion is advised..
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 7174
  • Country: ro
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #12 on: October 14, 2023, 09:19:07 am »
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.

Online 2N3055

  • Super Contributor
  • ***
  • Posts: 7698
  • Country: hr
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #13 on: October 14, 2023, 10:13:26 am »
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.

Dynamic 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..
"Just hard work is not enough - it must be applied sensibly."
Dr. Richard W. Hamming
 

Offline Martinn

  • Frequent Contributor
  • **
  • Posts: 349
  • Country: ch
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #14 on: October 14, 2023, 12:17:33 pm »
https://pages.hmc.edu/mspencer/e151/AutoBodeGuide.pdf

Code included.  Sweet.  I'm not a Matlab guy, but it looks like the magic happens in the iddata and tfest functions.
They are using tfest https://ch.mathworks.com/help/ident/ref/tfest.html
for 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, see https://www.thinksrs.com/downloads/PDFs/ApplicationNotes/SR1_SweptSine.pdf
and avoid the detour via a transfer function fit.
 
The following users thanked this post: Smokey, RoGeorge, 2N3055, jasonRF

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 17639
  • Country: us
  • DavidH
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #15 on: October 14, 2023, 07:19:59 pm »
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.
 

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 3216
  • Country: us
  • Not An Expert
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #16 on: October 14, 2023, 07:48:12 pm »
https://pages.hmc.edu/mspencer/e151/AutoBodeGuide.pdf

Code included.  Sweet.  I'm not a Matlab guy, but it looks like the magic happens in the iddata and tfest functions.
They are using tfest https://ch.mathworks.com/help/ident/ref/tfest.html
for 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, see https://www.thinksrs.com/downloads/PDFs/ApplicationNotes/SR1_SweptSine.pdf
and avoid the detour via a transfer function fit.

Great 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. 
 

Offline Martinn

  • Frequent Contributor
  • **
  • Posts: 349
  • Country: ch
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #17 on: October 14, 2023, 08:26:10 pm »
Great post.  I was curious about why they did the transfer function estimation.  That seems unnecessary. 
Who 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.
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 7174
  • Country: ro
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #18 on: October 14, 2023, 08:55:50 pm »
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 page https://octave.sourceforge.io/signal/function/tfestimate.html it is mentioned the method used:
Quote
Welch (1967) periodogram/FFT method
https://en.wikipedia.org/wiki/Welch%27s_method

I 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.

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 3216
  • Country: us
  • Not An Expert
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #19 on: October 14, 2023, 08:58:35 pm »
...
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.

Yes, that!

With that said...
https://pypi.org/project/tfest/
https://stackoverflow.com/questions/28462144/python-version-of-matlab-signal-toolboxs-tfestimate
 

Offline MathWizard

  • Super Contributor
  • ***
  • Posts: 1841
  • Country: ca
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #20 on: October 14, 2023, 11:22:07 pm »
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.
« Last Edit: October 14, 2023, 11:25:57 pm by MathWizard »
 

Offline Martinn

  • Frequent Contributor
  • **
  • Posts: 349
  • Country: ch
Re: Bode Plot from Swept Sine Input and Scope Captured Data.........
« Reply #21 on: October 15, 2023, 08:46:08 am »
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 page https://octave.sourceforge.io/signal/function/tfestimate.html it is mentioned the method used:
Quote
Welch (1967) periodogram/FFT method
https://en.wikipedia.org/wiki/Welch%27s_method
Matlab 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.html
txy = 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.html

The 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-1
This 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.py https://github.com/giuliovv/tfest/blob/main/tfest/tfest.py looks interesting.

Why is it ok to be sweeping the frequency, rather that stopping on a bunch of steps?
I'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.
 
The following users thanked this post: Smokey, RoGeorge


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf