EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: RoGeorge on November 04, 2022, 10:37:35 am

Title: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 04, 2022, 10:37:35 am
VNA = Vector Network Analyser
DSO = Digital Storage Oscilloscope
AWG = Arbitrary Waveform Generator
ADC = Analog to Digital Converter
PC = Personal Computer


The plan is to measure the response of an unknown circuit over a range of frequencies.
- the input/reference signal is a sinusoidal frequency sweep, with constant amplitude and with all its parameters well known
- the unknown circuit to measure can introduce phase shifts, changes in amplitude, and can produce harmonics
- reference signal is considered ideal, measured signal can become very noisy

In practice
- there is an AWG that generates a frequency sweep from Fmin to Fmax in time T
- the ADC of a DSO is triggered at the beginning of T, and it samples the frequency sweep at a constant sampling rate, S, over the entire period, T
- the reference signal is considered noiseless, but the measured signal along a sweep can become very small and noisy
- after the frequency sweep is completed, the ADC samples are transferred to a PC for post processing

All the parameters are well known, Fmin, Fmax, T, S, same as the variation type between Fmin and Fmax (usually logarithmic).  The reference signal can be either sampled together with the unknown response (meh), or reconstructed in software starting from the known sweep and its Fmin, Fmax and T (preferable).

Quantitatively, about 10-20 million of 8 bit ADC samples can be taken during a frequency sweep, and the data transfer alone (from the DSO to the PC) takes a few minutes, so the data post processing in the PC doesn't have to be real time, but precision and resolution are very important.


How to post process the ADC samples so to extract the spectrum, the amplitudes and the phases at any moment along the given frequency sweep, with best precision and resolution?
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 04, 2022, 06:44:50 pm
How to post process the ADC samples so to extract the spectrum, the amplitudes and the phases at any moment along the given frequency sweep, with best precision and resolution?
As you know, the root is in Fourier analysis (https://en.wikipedia.org/wiki/Fourier_analysis), which converts a signal from the time domain to the frequency domain.  Here, you will be using a discrete Fourier transform (https://en.wikipedia.org/wiki/Discrete_Fourier_transform) on the ADC samples to obtain the discrete spectrum.

The problem is that you really need to work in a mixed time-frequency domain, with a time-varying signal.  We achieve this by applying a window function (https://en.wikipedia.org/wiki/Window_function) to the samples being considered, before doing the DFT.  The size of the window (number of samples) sets the minimum frequency (since it cannot capture information on waveforms longer than the window itself), and the shape of the window defines the spectral leakage (https://en.wikipedia.org/wiki/Spectral_leakage).  In essence, we use a window to split time into "frames", and grab the spectrum from each "frame".  Note that the windows are not sequential (except for rectangular windowing), and typically overlap.  Overlap by one half is typical.

There are a lot of window functions (https://en.wikipedia.org/wiki/Window_function#A_list_of_window_functions) to choose from, with rectangular, Hann (NOT "Hanning"), and Hamming windows probably the most common ones.  Note that with 8-bit input samples, you only have about 48 dB of dynamic range anyway (or rather, you will have a quantization noise at all frequencies at around 48 dB level).  You can also reconstruct the original signal from the windowed spectra by inverse Fourier transform, then windowing those with the same windowing function (the same way), and adding the windowed signals together.  This is exactly how MP3 audio works, after all.

If you implement your own program, then I definitely recommend using FFTW (https://fftw.org/), included in most numerical computing packages.  It is ridiculously fast, especially if you "gather wisdom on the FFT window sizes you use", i.e. tell it to find and then save the fastest way to compute a real-valued DFT of a specific number of samples: this can make an order of magnitude difference.  You will take the input samples, convert them to floating-point while multiplying each sample with the corresponding window function coefficient, do FFT on it, and receive half the number of input samples of complex coefficients, and then zou vill be happy.  Each coefficient corresponds to a frequency, with magnitude (|z| = sqrt(r²+i²) = cabs(z)) describing the intensity/magnitude of that frequency component, and phase angle (arg z = atan2(i, r) = carg(z)) the phase of that frequency.

Because of spectral leakage, you will need to estimate the peak frequency (and optionally the peaks of the harmonics): they won't be nice one-complex wide peaks.  Depending on the width of the peak, there are various methods, but usually a parabola down to half the height of the peak works well.  For the exact peak phase and amplitude, you can use linear interpolation between the bins immediately above and below, or e.g. cubic interpolation; although note that cubic interpolation can overshoot, whereas linear interpolation usually undershoots a bit.



At this point, if I were you, I'd be probably a bit sceptical: so much math.. how can one be sure it all works correctly?
The answer is, one can trivially check and verify.  Generate test signals (as both ADC values as well as "perfect" floating-point samples), and examine if the results correspond to the expected values.  Add white noise (using an excellent PRNG, say the high bits of Xorshift64*), and see how that shows up.  Compare the "digitized" (converted to N-bit ADC values) and the "perfect" (original floating-point) signal, to see how it shows up.

Just do not forget how sinusoidal waveforms work and, say, have the input be "two" sinusoidal signals of the same frequency but at different phases, and then wonder why you get such odd results.  For example, \$sin(x+a) + sin(x+b) = (cos(a) + cos(b)) sin(x) + (sin(a) + sin(b)) cos(x)\$, meaning that you actually then get just a single sine wave whose phase and amplitude depends on the phases (\$a\$ and \$b\$) of the original signals.

In other words, there is no need for trust here, because one can always verify all of this as carefully as one wants.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 04, 2022, 08:40:00 pm
Octave?
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 04, 2022, 08:53:11 pm
I'm familiar with FT and windowing, but don't have much hands on experience.  I've read at some point about the Wavelet Transform (WT), which is about the same as FT just that it still retains the time evolution of the spectrum.  If I got it right after reading mostly the words than the math, WT might give better results than FT+windowing.  (posted the 101 WT some time ago here: https://www.eevblog.com/forum/chat/fun-for-nerds/msg3554257/#msg3554257 (https://www.eevblog.com/forum/chat/fun-for-nerds/msg3554257/#msg3554257) )

Either FT or WT, neither of them can take the advantage of the extra knowledge we have, being that the reference signal is known, and its evolution is synchronous with the unknown measured signal.  Would be a pity to throw away this advantage.  As an example, an interferometer is so sensitive exactly because it takes advantage of the reference signal.  Same does the synchronous rectifier or the lock-in amplifier, they have a clean and synchronous reference they can compare against.  The lock-in amplifier even does the quadrature trick, where synchronous sin and cos signals are generated internally (in lock with the reference signal), thought those are usually working at a constant frequency.

Thinking as we speak, since at any moment the reference frequency/amplitude/phase are known, it should be possible to generate a 90 degree shifted signal with the same sweep (a sin and a cos).  I think having sin and cos of the reference swwep would be an advantage, because sin2+cos2=1 at any moment.  This would mean the amplitude can be known by looking at a single sample point (1 sample from the cos and 1 sample from sin), without waiting for an entire period to detect the peak of the signal.

Knowing the amplitude at the moment of each sample will be of a great advantage, can be averaged later to get rid of the noise.  Synthesizing in software a sin and cos in sync with the reference sweep should be possible, the problem is, can a similar quadrature signal be reconstructed for the measured signal, too, by looking at the entire captured data? 

It feels like it should work, but I might be completely off, have to draw the idea on paper before it vanishes.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 04, 2022, 11:29:14 pm
I've read at some point about the Wavelet Transform (WT), which is about the same as FT just that it still retains the time evolution of the spectrum.
One way to intuitively think about it is that each frequency bin has its own window in most wavelet transforms.

As to wavelet transform compared to windowed Fourier transform, on a sinusoidal input signal, it's actually hard to say.  For example, the Wikipedia article on wavelet transform (https://en.wikipedia.org/wiki/Wavelet_transform) mentions "Wavelets have some slight benefits over Fourier transforms in reducing computations when examining specific frequencies. However, they are rarely more sensitive, and indeed, the common Morlet wavelet is mathematically identical to a short-time Fourier transform using a Gaussian window function. The exception is when searching for signals of a known, non-sinusoidal shape (e.g., heartbeats); in that case, using matched wavelets can outperform standard STFT/Morlet analyses." with a couple of references.

Either FT or WT, neither of them can take the advantage of the extra knowledge we have, being that the reference signal is known, and its evolution is synchronous with the unknown measured signal.
But do we know what the measured signal is?

If we were just measuring the (frequency-dependent) signal delay, we could use (windowed) cross-correlation (https://en.wikipedia.org/wiki/Cross-correlation) of the two signals, as a function of the time delay \$\tau\$.  The peak in this function corresponds to the time difference when the two signals were most alike, for each window, and the value at the peak corresponds to the amplification/attenuation of the signal.  However, correlation does not mean exactly the same, so it does not capture information like frequency dispersion.

No, I do believe that when working with numerical data coming from a sinusoidal reference signal, we're best off with the FFT I outlined.  I actually kinda like it more because it does not make any assumptions about the signal, which makes it easier to test for correctness, and measure actual accuracy et cetera.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Mechatrommer on November 04, 2022, 11:59:10 pm
are you going to measure S21 only? or need S11? if so you need directional coupler and deal with how to get -60dB or lower resolution... btw FFT is the easiest part... https://www.eevblog.com/forum/testgear/admittance-measurements-with-dso-awg-with-bode-function/msg4496851/#msg4496851 (https://www.eevblog.com/forum/testgear/admittance-measurements-with-dso-awg-with-bode-function/msg4496851/#msg4496851)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 05, 2022, 12:36:24 am
This is the principle, talking about the reference signal first:

(https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/?action=dlattach;attach=1631533;image)

- the green trace, V(sin), is the reference signal from the AWG, the one that sweeps in frequency over time
- the yellow trace, V(cos), will have to be reconstructed in software.  This is possible from only one sample, and for each sample, because at any moment we know the value and the frequency of the reference signal.
- then each sample can be square to get the magenta and the grey traces respectively, V(sin2) and V(cos2).  Note how the square signal is also a sinusoidal, always positive, and in antiphase (also has double the frequency of the original reference).
- now if we add these two, we get a constant value proportional with the amplitude of the reference signal, just that we can compute it for each and every signal, even for the moments when the reference signal (green trace) is crossing through zero.

A single sample, even when zero, can tell the amplitude.  ;D



Now, can we do the same for noisy signal to measure, coming from an unknown circuit?  Let's assume at first the unknown circuit is linear (LTI) therefore the unknown circuit can change the phase and/or the amplitude, but can not introduce new harmonics
- the measured signal from the unknown circuit will have noise, a different amplitude and a different phase than the reference signal
- however, the circuit doesn't know the difference between a sin and a cos, so it will alter the reference signal with the same change in amplitude and in phase no matter it's sin or cos.  This means that we can produce (in software) a quadrature signal for the unknown measured signal, similar with how we did for the reference signal.

And here is a little stretch (more assumptions about the measured signal), trying to benefit from additional knowledge we have about the reference signal:
- we know the frequency at any moment
- we know sin and cos are in fact the same function, but shifted in time with 90 degrees (it's easy to observe this in the green and yellow traces)
- so to reconstruct the cos signal in software, we just copy the sin with a pi/2 delay.  This pi/2 (delay) varies with frequency, but the frequency is known at any moment relative to the start of a chirp, it's the same frequency as in the reference signal.

It looks like the same reconstruction can be possible for the measured signal, too, assuming the amplitude and phase doesn't change dramatically over the time of a quarter of a period, which I think it's safe to assume for a slow enough sweep.

TL;DR the amplitude of the unknown signal can be deduced from a single sample (once we have acquired and post-process enough samples along the frequency sweep, so we can copy the sample from pi/2 behind as the cos of the measured sin(x)).

It looks like, there is no need of FFT, or DFT, or WT when DUT is a linear circuits.  A variable time shift with pi/2 to approximate cos(x), then square the two samples for sin(x) and cos(x), then adding their squared value will get the amplitude frequency response at each sample, which amplitude can be averaged to filter out a part of the noise.

Should work.  Or am I missing something?




The tricky part comes if the unknown circuit is non linear.  This will add harmonics, and intermodulation products, and so on.  The previous trick might not be applicable, but at least the fundamental frequency of all the harmonics is known.  Not sure what to do for the non linear circuits.  I need to think more about it, and here it's already early AM hours.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Mechatrommer on November 05, 2022, 01:02:18 am
..the one that sweeps in frequency over time..
ooh ok... thats complicated...
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 05, 2022, 01:40:58 am
a(t)-->f_LTI()-->b(t)

a(t)=k1*sin(w*t+p1)
b(t)=k2*sin(w*t+p2)

wanted: {k2/k1, (p1-p2)}

fft(a(t),w)={ f[-w,w] , phi[w] } = { k1/2, k1/2 }, { p1 }
fft(b(t),w)={ f[-w,w] , phi[w] } = { k2/2, k2/2 }, { p2 }

Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: nctnico on November 05, 2022, 01:59:22 am
How to post process the ADC samples so to extract the spectrum, the amplitudes and the phases at any moment along the given frequency sweep, with best precision and resolution?
As you know, the root is in Fourier analysis (https://en.wikipedia.org/wiki/Fourier_analysis), which converts a signal from the time domain to the frequency domain.  Here, you will be using a discrete Fourier transform (https://en.wikipedia.org/wiki/Discrete_Fourier_transform) on the ADC samples to obtain the discrete spectrum.
For this kind of problem, FFT is likely not the best solution due to noise. I'd look at synchronous sampling / demodulation techniques that tend to cancel noise. Keep in mind that an FFT bin might not be an exact match for the frequency that is used for the measurement. With synchronous sampling / demodulation you can get much better filtering of a specific frequency compared to FFT (which is always a range of frequencies).

My approach would be to implement an IQ demodulator for both stimulus and resulting signal. The IQ signal amplitudes from the stimulus give a phase/frequency error which can be used to adjust the LO frequency used for demodulation. The adjusted LO frequency can then be  used to demodulate the resulting signal into an I and Q signal that provide real and imaginary amplitudes (after a low pass filter).
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 05, 2022, 03:07:25 am
Keep in mind that an FFT bin might not be an exact match for the frequency that is used for the measurement.
That's exactly why I mentioned using a parabola down to half the height of the peak in my first post.

Basically, the windowing function will always cause spectral leakage, so a sinusoidal signal will show up as a multiple-bin wide peak.  Instead of picking the frequency bin corresponding to the highest amplitude, you fit a parabola to the samples comprising the peak, \$y_i = a - b (i - i_0)^2\$, where \$i\$ is the sample index, and \$a\$, \$b\$, and \$i_0\$ are the variables you fit to, using e.g. linear least squares.  \$a\$ is then the peak amplitude, \$i_0\$ (which is a real and not necessarily an integer) is the peak in fractional samples, and \$b\$ is related to the width of the peak.

For example, if you had a simple peak with amplitude \$a_0\$, with preceding sample having amplitude \$a_{-1}\$, and succeeding sample having amplitude \$a_{+1}\$, both preceding and succeeding sample amplitudes less than \$a_0\$, then a parabola fit tells the actual peak is at frequency bin \$x\$ relative to the peak frequency bin,
$$x = \frac{a_{-1} - a_{+1}}{2 a_{+1} - 4 a_0 + 2 a_{-1}}$$
(and you expect \$-1 \lt x \lt +1\$); the peak amplitude itself is \$a\$,
$$a = a_0 - \frac{ \left( a_{-1} - a_{+1} \right)^2 }{ 8 a_{+1} - 16 a_0 + 8 a_{-1} }$$

Do note that I am assuming we are working on already digitized data.  I'm pretty sure there is analog circuitry that can do better, too, especially because 8-bit ADC causes quantization noise at about 48 dB; but I have no idea how to go about it that way, I can only help with the discrete math here.

(This peak fitting stuff is quite common in computational physics.)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: SiliconWizard on November 05, 2022, 03:39:29 am
Yep.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hamster_nz on November 05, 2022, 08:01:59 am
Have you considered passing the samples through a Hilbert transform filter to convert the real 8-bit samples to complex, then convert those values to polar values (e.g. atan2 and magnitude).  The (smoothed) angular rate of change will indicate frequency, the smoothed magnitude will indicate amplitude.

One problem will be if the circuit under test has 'memory' - e.g. the output depend on earlier inputs. For example, a resonant circuit may keep on ringing, so you may get different results if you sweep up or down, or sweep slowly or quickly.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 05, 2022, 08:40:59 am
are you going to measure S21 only?

At first, yes, something like a Bode plot will be great, then add later more VNA-like functionality.




a(t)-->f_LTI()-->b(t)

a(t)=k1*sin(w*t+p1)
b(t)=k2*sin(w*t+p2)

wanted: {k2/k1, (p1-p2)}

fft(a(t),w)={ f[-w,w] , phi[w] } = { k1/2, k1/2 }, { p1 }
fft(b(t),w)={ f[-w,w] , phi[w] } = { k2/2, k2/2 }, { p2 }

Yes, something like that, just that I think I'm up to something new for this particular case, where we have a known reference sweep:  Instead of taking a FFT, which can only be computed on batches of samples, it looks like with the method I've described above it would be possible to compute all on the fly, sample by sample, and with the extra advantages in filtering out noise, much thinner bins than a FFT, and without spectral leakage.




the windowing function will always cause spectral leakage, so a sinusoidal signal will show up as a multiple-bin wide peak

AFAIK a FFT can only be computed on batches of many samples, thus the need for windowing, too, and a limited number of bins.  Looks inescapable, but with the method I was describing in the attached plots before, it will be no need for windowing or binning.  Amplitude and phase can be computed on the fly, sample by sample (at least for LTI circuits).




Have you considered passing the samples through a Hilbert transform filter to convert the real 8-bit samples to complex, then convert those values to polar values (e.g. atan2 and magnitude).  The (smoothed) angular rate of change will indicate frequency, the smoothed magnitude will indicate amplitude.

One problem will be if the circuit under test has 'memory' - e.g. the output depend on earlier inputs. For example, a resonant circuit may keep on ringing, so you may get different results if you sweep up or down, or sweep slowly or quickly.

My calculus skills doesn't include Hilbert transform, I don't know what it does, or how to handle the math for it.  :-\

I'm not sure if my method still holds for something like a resonant circuit, it feels like it might still work, can't say for sure.  Will have to think more about that, and do some simulations to double check, thanks for pointing it out.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: gf on November 05, 2022, 09:30:04 am
I'm not sure if my method still holds for something like a resonant circuit

Generally, you can only capture the complete response of the DUT to a particular stimuls if the measurement interval is longer than the duration of the stimulus + the duration of the DUT's impulse response (until it decays to the noise floor).
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 05, 2022, 11:56:06 am
Have you considered passing the samples through a Hilbert transform filter to convert the real 8-bit samples to complex, then convert those values to polar values (e.g. atan2 and magnitude).  The (smoothed) angular rate of change will indicate frequency, the smoothed magnitude will indicate amplitude.

a(t)-->f_LTI()-->b(t)

a(t)=k1*sin(w*t+p1)
b(t)=k2*sin(w*t+p2)

wanted: {k2/k1, (p1-p2)}

f(t): real ---> H(f(t)) ---> h(s): complex
w: real ---> s: complex
LP: low pass filter

Code: [Select]
get_sample(a(), b());
loop(i=0..n-1)
{
    LP(polar(H(a(t),w)))=polar(LP(cplx(a[s]))={ mod(LP(cplx(a[s])) , ang(LP(cplx(a[s])) } = { k1[i], p1[i] }
    LP(polar(H(b(t),w)))=polar(LP(cplx(b[s]))={ mod(LP(cplx(b[s])) , ang(LP(cplx(b[s])) } = { k2[i], p2[i] }
}
k1' = average_smoother(i=0..n-1){ k1[i] }
k2' = average_smoother(i=0..n-1){ k2[i] }
p1' = average_smoother(i=0..n-1){ p1[i] }
p2' = average_smoother(i=0..n-1){ p2[i] }

interesting approach  :D


One problem will be if the circuit under test has 'memory'

indirectly it's a good way to "test" the circuit memory.
If it has no memory, results are sweep-independent.
If it has memory, results are sweep-dependent

you invented a "memory meter" :o :o :o
(Ask for a patent!  ;D )
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Kleinstein on November 05, 2022, 12:08:51 pm
The hilbert transformation is a bit like an I/Q mixer, generating 2 new signals that can be interpreted as the real and complex part of an intermediate frequency that can be choosen quite low (near zero). One still has to choose resonable low pass filtering / filter window.

If one can limit the memory of the system to a small number of resonances at a time (or close to the frequency of interest) one could in theroy still separate it in relatively short time: One could do a curve fit to a part with the stimulus frequency and an additional exponentially decaying sine. A simple system with just 1 resonant part gives an exponential decay, just the amplitude and phase are kind of unknown.  Especially for a high Q system one should get away with well less than a decay constant worth of data. Still the curve fit needs a low of computational power. Once a resonant part is found the frequency and approximate decay constant would be known. So later fits would not be that bad. The parameters amplitde and phase can be transformed to sine and cosine part and thus linear parameters that do not need an iterative fit. The tricky step would only be finding a new resonance, if present in the frequency range.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 05, 2022, 12:16:55 pm
I think this stuff can be evaluated with Octave (or Matlab, if you prefer).
I mean, before implementing in Python.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: 2N3055 on November 05, 2022, 12:37:56 pm
FRAforPicoscope is open and has a full source code published.
Take a looksee.
Might explain few things.
He used Goertzel variant...
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 05, 2022, 02:18:57 pm
My calculus skills doesn't include Hilbert transform, I don't know what it does, or how to handle the math for it.  :-\
The Hilbert transform just performs a 90 degree phase shift without altering the amplitude and eliminates the DC component. It can be implemented as a Fourier filter -i*sign(f). It is difficult to construct compact time domain convolution filters for the Hilbert transformation because the filter coefficients decay slowly with 1/t. One approach I implemented a long time ago for phase estimation from fringe projection images used anti-symmetric convolution masks with a step size of two in combination with acausal recursive prefilters (same filter as B-spline transform, just a different coefficient). This provides pretty good results for just a few filter coefficients over a wide frequency range, and the frequency range can be extended on the low end using Laplace pyramids. This might be an efficient and accurate method to recover the phase and amplitude for your data.

Do you have an example data set for download somewhere? I'd be happy to give this algorithm a try on your data when I return from travel.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: balnazzar on November 05, 2022, 03:04:23 pm
This discussion is very interesting and fun to read. Keep posting, folks  :)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hamster_nz on November 06, 2022, 03:27:56 am
Made a lame attempt at this....

The program generates 8M points of data for a linear chirp (in data[]), from 0.05*Fs to 0.45*Fs, with amplitude ramping down from 1.0 down to 0.5.

It then performs a Hilbert Transform to get x[] and y[], and prints out 200 samples from the start, center and end of the data, along with magnitude and angular frequency.

Attached graphs are 200 data points towards the end of the chirp, and the results of the analysis (mag ~= 0.5, frequency ~= 0.45 * Fs)

The interesting thing to note is that although the data points appear pretty jumbled, the resulting magnitude and frequency values match the parameters used to generate the data.


Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 06, 2022, 02:21:21 pm
Made a lame attempt at this....

The program generates 8M points of data for a linear chirp (in data[]), from 0.05*Fs to 0.45*Fs, with amplitude ramping down from 1.0 down to 0.5.

It then performs a Hilbert Transform to get x[] and y[], and prints out 200 samples from the start, center and end of the data, along with magnitude and angular frequency.

Attached graphs are 200 data points towards the end of the chirp, and the results of the analysis (mag ~= 0.5, frequency ~= 0.45 * Fs)

The interesting thing to note is that although the data points appear pretty jumbled, the resulting magnitude and frequency values match the parameters used to generate the data.
Seems to work pretty well, I think there is less than 1% error in the selected wave number range. The problem with this type of filter is ringing. I have attached a plot of the imaginary part of the transfer function. The ringing can be reduced with some filter optimization at the expense of the lower and upper cut-off frequency. The second plot shows an example for four filter coefficients, i.e., a 15 coefficient mask including the anti-symmetric half and the zeros.

Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 07, 2022, 09:16:24 am
I took another look at hamster_nz's Hilbert filter. I think there is a small bug in the convolution loop that omits the last coefficient, but that makes little difference in the results. The error is a bit higher than I thought. I computed the amplitude and phase velocity errors outside of the boundary area and got
Code: [Select]
Max phase vel. error [degree]: 0.288886, std.dev: 0.142669
Max amp error: 0.015821, std.dev: 0.002622
I tested the same type of filter (anti-symmetric, step of two) with an optimized filter mask:
Code: [Select]
static double hilbert_kern_64[64] = {
    0.635616,     0.209204,     0.122352,     0.0840508,    0.0619826,    0.0473452,    0.0367619,
    0.0286638,    0.022225,     0.0169707,    0.0126088,    0.00894956,   0.00586386,   0.00325984,
    0.00106942,   -0.00076,     -0.00227075,  -0.00349816,  -0.00447281,  -0.00522194,  -0.00577038,
    -0.00614116,  -0.00635588,  -0.00643492,  -0.00639756,  -0.00626203,  -0.00604542,  -0.00576376,
    -0.00543187,  -0.00506332,  -0.00467047,  -0.00426433,  -0.00385457,  -0.00344956,  -0.00305631,
    -0.00268057,  -0.00232689,  -0.00199862,  -0.00169807,  -0.00142656,  -0.00118454,  -0.000971719,
    -0.000787179, -0.000629464, -0.000496718, -0.000386775, -0.00029733,  -0.000225911, -0.000170083,
    -0.000127445, -9.57244e-05, -7.27986e-05, -5.67725e-05, -4.59548e-05, -3.88904e-05, -3.43821e-05,
    -3.14574e-05, -2.93609e-05, -2.75466e-05, -2.56537e-05, -2.34504e-05, -2.08759e-05, -1.79182e-05,
    -1.46725e-05};
This provides more than two orders of magnitude in error reduction:
Code: [Select]
Max phase vel. error [degree]: 0.000847, std.dev: 0.000380
Max amp error: 0.000046, std.dev: 0.000007
The maximum amplitude error in both cases occurs at the low end for a normalized wave number of ~0.1, where the amplitude is highest (1). The same relative error also occurs at high wave numbers (~0.9), as the transfer function is symmetric w.r.t. half the Nyquist frequency.

The plot below shows the transfer function corresponding to the kernel above in magenta. Note the difference in scale compared to the previous plot from the truncated hyperbola. The plot also contains a second optimized Hilbert filter using an acausal recursive pre-filter. Using this technique, more compact filters with less error and/or a wider frequency range can be constructed. There are limits to this and the recursive pre-filter will become unstable if one attempts to push the upper cut-off too close to the Nyquist frequency. But the lower cut-off is probably of more concern for practical applications. This end can be extended using a band decomposition and subsampling (Laplace pyramid). Also worth mentioning is that the Hilbert filter itself can be used to eliminate DC bias by applying it twice to create the quadrature pair.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hamster_nz on November 07, 2022, 10:25:21 am
I took another look at hamster_nz's Hilbert filter. I think there is a small bug in the convolution loop that omits the last coefficient, but that makes little difference in the results.

Yes, it was a bug/oversight - it should be 

Code: [Select]
      for(int j = -127; j <= 128; j+=2) {

I'm quite pleased that even my lame attempt actually looks like a possible technique for 8-bit data, as I've occasionally pondered on recovering amplitude and frequency from time series data, but never acted on it to now....

One other thing I have no idea on is how the sweeping of frequency will upset things.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 07, 2022, 11:32:15 am
I have no idea on is how the sweeping of frequency will upset things.

step transient

you have it even from a simple differential equation applied to a feedback (G_loop) system with one pole, and not in the origin ...

Such a system can triggers permanent oscillations as well as it can saturate as well as it can dampens early oscillations within a "step transient" time.

When the step transient time is unknown, and the system is assumed to have a nice transient response, I think you can monitor data, looking for stable results. If any.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: gf on November 07, 2022, 03:09:47 pm
I have no idea on is how the sweeping of frequency will upset things.

A chirp of finite duration certainly has discontinuities at the beginning and the end. These discontinuities need to be tapered, for instance by applying a tukey window, having the consequence that only the center (say 99%) of the chirp is usable at the end. However, aside from the problem at the endpoints, a (linear) chirp is a spread spectrum signal, having a nice flat amplitude spectrum, which contains no discrete frequency lines.

But a spread spectrum stimulus is also a disadvantage, because a narrow bandpass filter cannot achieve processing gain. If the bandwidth is reduced, the filter captures less noise power, but also less signal power, and the SNR remains the same.

OTOH, if the stimulus signal energy is concentrated at a single frequency line (or at a limited number of discrete frequency lines), then the signal power passing through a bandpass centered at one of the frequency lines is independent of bandwidth, while the noise power passing through the bandpass still decreases with decreasing bandwidth, therefore the SNR can be improved by reducing the bandwidth, i.e. we can obtain processing gain by capturing a larger number of samples.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Marco on November 07, 2022, 03:50:16 pm
- the unknown circuit to measure can introduce phase shifts, changes in amplitude, and can produce harmonics

Unless the circuit is very fast and within the frequencies of interest only has a simple non-linear transfer curve without significant memory, you need to do something like Volterra series estimation to characterise it (if it is fast enough to have no significant memory, you just need a curve tracer, not this). At best this measurement will tell you what the output is for the exact chirp at the exact amplitude you applied and nothing much else.

Worrying about windowing for characterising a nonlinear circuit is also a bit silly, get all that linear thinking out of here.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 09, 2022, 08:28:21 pm
Did a brief read about the Hilbert Transform (HT) this Sunday, and it looks a lot like what I was heaving in mind.  Now I'm not sure if I should be embarrassed for not knowing what the HT is, or proud for rediscovering the wheel.  :-\

Anyways a bummer, the fun was much bigger reinventing the wheel than learning somebody else's math.  ;D

Just kidding, I'm grateful for being pointed to the HT, thank you all for that.  The math alone seems very intimidating to me, while in practice it looks trivially simple, which makes me think I still don't know what HT does, or what is it for.  :-//

As I was saying, still not sure what HT is, but assuming HT only introduces (two?) 90* phase shift(s) for each frequency present in a signal, then what I was proposing was (probably) using only the real part of the HT from the measured signal.  Not sure.  Also, most of the remarks, advice and example posted here are not yet in my understanding range.

Tried to get the ADC samples from the DSO into PC, to test my idea on real data.  Had to dig into 100+ forks for Python-IVI drivers, and merge the relevant ones.  Then it still didn't work and had to debug it with Wireshark, and so on.  Still working on it, and already have communication with both the AWG and the DSO.  :D

Meanwhile, did a more realistic simulation in LTspice.  This time, emulated an AWG with a logarithmic chirp, then emulated a DSO, then plot the frequency response for two different DUT, an RC low pass, and an RLC circuit.  The observed amplitude Bode plots correspond with the theoretical response of the DUT.

This one is for RC LP:
(https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/?action=dlattach;attach=1635689;image)

Please note that no reference chirp was sampled, and only the measured signal is used to calculate (on-the-fly) the amplitude response of the DUT (the red trace).
 
The chirp is done with logarithmic frequency variation, though it chirps from high to low F, to minimize any errors caused by an eventual phase noise (no noise was added into simulation for now).

The next one is for a resonant RLC:
(https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/?action=dlattach;attach=1635695;image)

There are some artifacts at the ends of the spectrum
- some errors at the beginning of the chirp are caused because there is no data previous to t0.  These can be easily cut out
- other type of errors are caused when the frequency variation of the sweep is too fast, observed at the low frequencies end of the chirp.  Same as before, the interval can be cut away, or the frequency can be swiped slower in the reference chirp, so the frequency will appear as quasi-constant along a quarter of a wavelength.
- the most amplitude wiggle seen in the red trace was because of sweeping too fast, not because of ringing
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 10, 2022, 09:11:35 am
Adding this link just for the docs, because it tells what the Hilbert Transform is/does in a meaningful way:
https://www.comm.utoronto.ca/frank/notes/hilbert.pdf (https://www.comm.utoronto.ca/frank/notes/hilbert.pdf)

I think I finally got it what the HT is about.  :D

What I was doing by reusing the same (measured) signal with a pi/2 wavelength lag, is the same as taking the HT at that particular wavelength only.  The pi/2 lag trick will work only assuming all the following conditions are true:

That's a lot of assumptions/limitations there.
The advantages would be:
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 10, 2022, 11:43:01 am
It took me a while to make sense of the plots until I figured out the chirp is from high to low frequency. Are you sure you used a pi/4 delay?

If you want to use a (time domain convolution) Hilbert filter for this, the maximum number of samples per period you intend to use will be important. If it is above, say 80 samples, generating a reasonably compact filter kernel is not going to be possible.

The optimized kernel I posted earlier can actually be improved a bit, I had an unnecessary constraint in the filter definition. But the size was already pretty big in order to match hamster's kernel. Processing the 8M data points from the example takes about 4 seconds on my very slow laptop without any optimization.

What programming language do you intend to use for this? If it's c++, I can help out with some code and optimized kernels for Hilbert filters.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 10, 2022, 01:17:48 pm
It took me a while to make sense of the plots until I figured out the chirp is from high to low frequency. Are you sure you used a pi/4 delay?

If you want to use a (time domain convolution) Hilbert filter for this, the maximum number of samples per period you intend to use will be important.

I was thinking all the time about "quarter of the circle" while writing everywhere pi/4 instead of pi/2...  :palm:

It's not that I didn't know the full circle is 2*pi.  Now I have to revisit all the code and simulations, to check if I didn't make the same "brain typo" of writing pi/4 (45°) while thinking about pi/2 (90°).   |O

Thank you very much for pointing me to that mistake, I wouldn't have spot it myself too soon.




The language of preference would be Python (with its 'numpy' module for math, which numpy is a wrapper for C/FORTRAN math libs, as far as I've heard).  Python is easier to use for a non programmer like me.  Could use C, too, if needed.  Can't say I know C++, but I might be able to use it code-monkey style, with stackoverflow googling all night long.  ;D

So far still trying to figure which way to go, FFT or with this HT-related time processing, where a copy of the same signal at a distance of 90° in the past might be good enough, instead of a proper sin cos quadrature.

I'm not sure why I like this sin2(x)+cos2(x)=1 trick so much, maybe I should just use FFT.  Playing around with alternatives mostly because it's a hobby project, without fixed performance specs to met or deadlines, IDK.

Got some (buggy) IVI drivers for Python-IVI, so I hope these days to get ADC samples from a physical DUT (using the oscilloscope as an ADC, a Rigol DS1054Z DSO).  I remember you were asking if there is any data set to download, so to evaluate the performance for this method of yours described here:  https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/msg4504627/#msg4504627 (https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/msg4504627/#msg4504627) Will let you know as soon as I got the Python-IVI working.  If the IVI drivers won't work, will give up to the compatibility idea (between various models of instruments), and use low level SCPI for Rigol DS1000Z oscilloscopes only).
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: nctnico on November 10, 2022, 01:56:30 pm
If you are using FFT, you might as well send a burst of noise into the DUT and subtract the FFT of the incoming signal from the resulting signal.

Where it comes to talking to instruments using Python, I can highly recommend using Pyvisa with the internal Visa drivers.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 10, 2022, 02:06:24 pm
The plots look like you used pi/2 where it counts and just mislabeled it.

You could implement the Hilbert transform with the FFT as well and compare the results. That is easy to do in python. I'm not very familiar with python signal processing libraries, I'll take a look and see if there is anything available for this. I'm sure there are convolution filters and you can always include the zeros in the filter mask if you're not concerned about efficiency, but I'm not so sure about the recursive filter and pyramids.

What is the maximum number of samples per period you intend to use?
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: mawyatt on November 10, 2022, 04:23:12 pm
Interesting thread, brings back memory's from ~80 when we tasked with implementing the Chirped Z Transform (CZT) based real time handheld battery powered SA.

The Chirps were created by I and Q custom multiplying DACs where the scaled input waveform was the DAC Reference and the Chirp coefficients were DAC digital words updated from SRAM. This created a Discrete Time Continuous Amplitude (DTCA) Chirp I & Q multiply result which was passed onto I & Q Convolvers created by very long custom Bucket Brigade CCD devices also operating in DTCA fashion. This program was awarded to us after two well known advanced technology companies failed, and like them we had in-house custom IC design and fabrication capability which was necessary with the multiple custom chips involved. The program was highly successful and put us "On the Map" in the advanced technology arena for future programs.

Since all the "computations" were done in custom hardware, the end result back then was very efficient from a SWAP standpoint.

Surpised the CZT hasn't found more appeal as its seems to be a hidden gem within the DSP domains, and certainly interesting to see it discussed here!!

Best,
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: adeuring on November 10, 2022, 09:37:33 pm
The language of preference would be Python (with its 'numpy' module for math, which numpy is a wrapper for C/FORTRAN math libs, as far as I've heard).  Python is easier to use for a non programmer like me.  Could use C, too, if needed.  Can't say I know C++, but I might be able to use it code-monkey style, with stackoverflow googling all night long.  ;D

If you are considering to work with Python and NumPy, I'd strongly recommend to also look at SciPy, a huge library based on NumPy. Especially its "signal" sub-package provides a lot of functions you often use for signal processing.   https://docs.scipy.org/doc/scipy/reference/signal.html shows you what is available.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 11, 2022, 06:06:36 am
What is the maximum number of samples per period you intend to use?

- The oscilloscope I have can not stream the ADC data, it needs to store a trace in its memory first.
- It can capture maximum 24 million ADC samples of 8 bits.  The maximum sample rate is 1GSa/s.
- The oscilloscope analog bandwidth is 100MHz.  This means 10 samples per period at 100MHz in.

In case of too few points/period, or when the 1/4 wavelength delay falls at a moment between two samples, I was thinking of doing interpolation, linear at first, to keep it simple.



Chirped Z Transform (CZT)

Took a brief look at CZT yesterday, very interesting, thank you.


Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 11, 2022, 11:15:33 pm
USB DSOs look more interesting here.
One bulk command, and they return samples.

my old RIGOL DSO doesn't have the ethernet port, it has a nice RS232 port.
I can ask the DSO to export samples.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 11, 2022, 11:43:14 pm
There are DDS chips able to generate A*sin(wt), A = const, w = f(freq ), freq = { 100Hz ... 20Mhz }.
SPI protocol.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 12, 2022, 01:51:06 am
What is the maximum number of samples per period you intend to use?

- The oscilloscope I have can not stream the ADC data, it needs to store a trace in its memory first.
- It can capture maximum 24 million ADC samples of 8 bits.  The maximum sample rate is 1GSa/s.
- The oscilloscope analog bandwidth is 100MHz.  This means 10 samples per period at 100MHz in.

In case of too few points/period, or when the 1/4 wavelength delay falls at a moment between two samples, I was thinking of doing interpolation, linear at first, to keep it simple.
You should not need any interpolation. I don't think I explained very clearly what I meant when I asked about the samples per period. The Hilbert filter transfer functions I posted previously use normalized wave numbers on the x-axis. The normalized wave number 1 corresponds to the Nyquist frequency, i.e., two samples per period. By design, these filters are symmetric around the normalized wave number 0.5, and have an upper and lower cut-off where the transfer function rapidly goes to zero. The upper cut-off is somewhere just over two samples per period, so should not really be a concern since you would normally over sample. The lower cut-off, however, might be more problematic. In the example below, it is around a normalized wave number of 0.033, which corresponds to about 60 samples per period. If you sample with, say, 200 samples per period, this filter will give you large errors. The problem is that you cannot easily reduce the lower cut-off frequency without using a large number of filter coefficients, aside from using pre-filters that amplify the signal at wave numbers 0 and 1. But even using pre-filters, the filter kernel size becomes large for low cut-off frequencies. There are other, more complicated ways to deal with this. The reason I was asking about the maximum number of samples per period you intend to use was to figure out whether you would need the more complicated algorithms or not. If you can keep the maximum number of samples per period below 80, you can probably use just a simple filter. I'll try to implement it with pyramid decomposition once I get over my jet lag. It might take me a little while, it's been over twenty years since I last worked on this type of stuff.

(https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/?action=dlattach;attach=1633618;image)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 12, 2022, 08:35:29 am
That's for sure I didn't understand what yo meant.  ;D

Half of the terms you used to describe what you did there are brand new to me.  Didn't lookup those yet because I'm a beginner in DSP, and the things you mentioned seemed to me like very specialized terms, and that might take many hours or days of reading before understanding.

Will search them one by one these days, very curious to learn about, just that I've sunk all my time from the last couple of days by digging into 108 forks of python-IVI (almost abandonware) then merging and debugging that, and learning how to download the ADC samples from the oscilloscope to the PC.  It works now, just that the download is too slow, almost unusable slow (20 minutes :-\ to download 24 million bytes of ADC samples over a 100Mbps LAN).

So far it seems the bottleneck is the oscilloscope itself, and asked about the download speed in another topic so to keep this one about data post processing only:  https://www.eevblog.com/forum/testgear/download-speed-from-rigol-ds1054z-or-similar-oscilloscope-to-a-pc/ (https://www.eevblog.com/forum/testgear/download-speed-from-rigol-ds1054z-or-similar-oscilloscope-to-a-pc/)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 12, 2022, 08:47:22 am
download is too slow, almost unusable slow

that's why I suggested a usb-dso.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: gf on November 12, 2022, 09:26:50 am
download is too slow, almost unusable slow

that's why I suggested a usb-dso.

With a binary bulk transfer over Gigabit Ethernet it should be basically be possible to transfer 24MBytes in ~0.25 seconds. Or with 100Mbit/s Ethernet, in ~2.5 seconds. Obviously not the medium is the bottleneck here.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: 2N3055 on November 12, 2022, 10:30:53 am
download is too slow, almost unusable slow

that's why I suggested a usb-dso.

With a binary bulk transfer over Gigabit Ethernet it should be basically be possible to transfer 24MBytes in ~0.25 seconds. Or with 100Mbit/s Ethernet, in ~2.5 seconds. Obviously not the medium is the bottleneck here.

Nobody said Ethernet was the bottleneck. Pretty much all embedded scopes (those not made on x86 platform and windows) have slow ethernet speeds because of how it is implemented).
USB scopes (like Picoscope) are generally designed to move data as fast as possible to PC because that is how they work in a first place..  So in case of newer USB3 picos, they shovel data very fast to PC:
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 12, 2022, 11:06:38 am
USB scopes (like Picoscope) are generally designed to move data as fast as possible to PC

Yup. Even cheap ones (40Mhz bandwidth) are able to move up to 20Mbyte/sec.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 12, 2022, 11:08:13 am
There are also DSOs and MSOs able to export the amplitude (peak-peak) and phase measures.
So, this could be a working environment:

Code: [Select]
.          ___________        ________        ________
          |           |      |        |      |       |
freq ---> |           | ---> | system |---> ch0      | ---> { amplitude, phase }
          |    DDS    |      |________|      |       |
          | sin(freq) |                      |  DSO  |
          |           |                      |       |
          |           | ------------------> ch1      | ---> { amplitude, phase }
          |___________|                      |_______|

- SPI, change DDS freq
- loop: RS232/usb/eth, read measures
- goto loop, until stable results
- eval: { gain = (amplidute.ch0 / amplitude.ch1), diff_phase = (phase.ch0 - phase.ch1) }
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: nctnico on November 12, 2022, 11:40:13 pm
It works now, just that the download is too slow, almost unusable slow (20 minutes :-\ to download 24 million bytes of ADC samples over a 100Mbps LAN).
That is because you are doing this wrong. Either the libraries you are using are faulty or you are requesting data in a format that needs to be pro-processed (like CSV). The fastest way for the oscilloscope to serve you data is in binary format so use that. Even then there are large differences between DSOs. From testing a couple of years ago, GW Instek is among the fastest DSOs to get data from.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 14, 2022, 10:52:09 am
GW Instek is among the fastest DSOs to get data from.

which model is? usb? ethernet? is the protocol documented?  :o :o :o
(second hand purchase possible, for me, just "because science")
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 14, 2022, 11:12:52 am
Dealt with the speed issue, the download went from 23 minutes to only 30 seconds for the entire 24MSa of ADC data.

There are many aspects about the download, so to not hijack this topic from its DSP theme, I've opened another topic about the download performance only, please let's keep this one about the DSP processing:
https://www.eevblog.com/forum/testgear/download-speed-from-rigol-ds1054z-or-similar-oscilloscope-to-a-pc/ (https://www.eevblog.com/forum/testgear/download-speed-from-rigol-ds1054z-or-similar-oscilloscope-to-a-pc/)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: hubi on November 20, 2022, 10:29:21 pm
I have been trying to get the Hilbert transform to work on a pyramid, but the error is very high (1-2%). Unfortunately, I cannot find my old code, so it might take me a while to figure this out. Here is what I'm getting for a geometric chirp, the recovered amplitude should be one everywhere. The gray line shows the input wave form. I'll try with some better interpolation filters for pyramid merging and see if that helps.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Marco on November 21, 2022, 12:37:38 am
What is actually the advantage of chirp over white noise or step response measurements?

If you actually wanted to estimate the nonlinear behaviour of the system, I doubt you can beat white noise.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: SiliconWizard on November 21, 2022, 08:16:05 pm
What is actually the advantage of chirp over white noise or step response measurements?

If you actually wanted to estimate the nonlinear behaviour of the system, I doubt you can beat white noise.

Good question.

One point is that you can fully control the bandwidth of a chirp signal. With white noise, it's more problematic. You'll need very good filtering to do this and still keep as flat a spectrum as possible.
Also obviously, generating white noise digitally requires using very good pseudo-random generators. It's not rocket science, but something to keep in mind.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: switchabl on November 21, 2022, 09:14:52 pm
White noise has a high crest factor (actually infinite for ideal white noise). So at a fixed amplitude level, a chirp stimulus has significantly higher power and will give a better SNR.

I would recommend an FFT approach for analyzing the chirp response though. With some care (continuous chirp with correct periodicity) the FFT can be done without windowing. The method discussed above (based on Hilbert transform) will almost certainly lead to artefacts for some DUTs; for example consider what happens when sweeping quickly across a high-Q resonance.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 22, 2022, 12:31:07 am
So. Step back to fft
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Marco on November 22, 2022, 03:39:46 pm
White noise has a high crest factor (actually infinite for ideal white noise).

With digital generated excitation it can be coloured a bit, you are going to be dividing the output by the input spectrum any way to recover phase. No need for perfection. With real-FFT based processing to determine a linear response it can be anything wideband, chirp or noise, white or slightly coloured, it all works.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: switchabl on November 22, 2022, 04:43:53 pm
Yes, basically anything that has sufficient spectral content in the frequency range of interest will work. But as a rule of thumb, at a fixed amplitude, a chirp stimulus will improve SNR by at least 6dB over a noise stimulus (ideal or not). Or put another way, you could increase sweep speed by a factor of 4 with the same result. And speed is usually a major concern if you implement FRA like this because otherwise you could use a stepped sweep with a frequency selective detector.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Marco on November 22, 2022, 06:33:52 pm
Wait a minute ... doesn't uniform white noise have a lower crest factor than a sine?
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 22, 2022, 06:54:41 pm
A sidestep:

Interestingly, if one was going to use sine-squared shaped pulses at a fixed frequency \$f\$ for a duration \$T\$,
$$A(t) = \sin\left(\frac{\pi t}{T}\right)^2 \sin(2 \pi f t)$$
then each pulse would be a single FFT window, and there would be much fewer details to consider.  In particular, its Fourier transform is known in algebraic form,
Code: [Select]
# f = signal frequency
# T = Pulse duration
# Signal = sin(%pi*t/T)^2 * sin(2*%pi*f*t)
# Fourier transform FT, with F as the frequency parameter,
# as a function of the signal duration T and the original frequency f:
FT(F,T,f).real = ( (f-F) * (T*f - F*T - 1) * (T*f - F*T + 1) * cos(2*%pi*T*(f+F) )
                 + (f+F) * (T*f + F*T - 1) * (T*f + F*T + 1) * cos(2*%pi*T*(f-F) )
                 + 2*f * (1 - T^2*(f^2 + 3*F^2))
                 ) / ( 8 * %pi * (f-F) * (f+F) * (T*f - F*T - 1) * (T*f - F*T + 1) * (T*f + F*T - 1) * (T*f + F*T + 1) );
FT(F,T,f).imag = ( (f+F) * (T*f + F*T - 1) * (T*f + F*T + 1) * sin(2*%pi*T*(f-F))
                 - (f-F) * (T*f - F*T - 1) * (T*f - F*T + 1) * sin(2*%pi*T*(f+F))
                 ) / ( 8 * %pi * (f-F) * (f+F) * (T*f - F*T - 1) * (T*f - F*T + 1) * (T*f + F*T - 1) * (T*f + F*T + 1) );
which means that if one samples the signal generator directly, the signal generator itself can be characterized (at that fixed frequency) by comparing the computed FT and the sampled FFT.

Of course, because it is a single fixed frequency pulse, it is no replacement for e.g. chirps (frequency sweeps) at all.  What it might be useful for, is when a system exhibits complex behaviour, like shifts the input frequency, modulates it, and perhaps generates odd harmonics from it; this gives a simple tool for examining suspicious frequencies that a sweep or other analysis indicates may have something funny going on.

I don't know if this is really actually useful, though; I only think it is interesting!
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 22, 2022, 06:54:51 pm
Had to put aside this topic lately, but kept reading and learning and thinking about it.  This weekend stumbled upon these two videos that opened a new perspective about FFT, and convolution, and many other things that clicked together.  Link to them (again) for the docs.

The Fast Fourier Transform (FFT): Most Ingenious Algorithm Ever?
Reducible
https://www.youtube.com/watch?v=h7apO7q16V0 (https://www.youtube.com/watch?v=h7apO7q16V0)

But what is a convolution?
3Blue1Brown
https://www.youtube.com/watch?v=KuXjwB4LzSA (https://www.youtube.com/watch?v=KuXjwB4LzSA)


In terms of FFT speed, tried a FFT for 24 million samples and it's almost instant (1.5 seconds), considering the download alone for the ADC samples takes about 20-30 seconds.
Code: [Select]
$ ipython
Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy as np
In [2]: fake_samples = np.random.random(24_000_000)
In [3]: %%timeit
   ...: fft_samples = np.fft.fft(fake_samples)
1.32 s ± 4.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: RoGeorge on November 22, 2022, 07:33:02 pm
if one was going to use sine-squared shaped pulses at a fixed frequency \$f\$ for a duration \$T\$,
$$A(t) = \sin\left(\frac{\pi t}{T}\right)^2 \sin(2 \pi f t)$$
then each pulse would be a single FFT window, and there would be much fewer details to consider.

Had to draw that, then it became clear:  sin2 is also a sinusoid of twice the frequency and with a DC component in it, DC equal with the amplitude, so A(t) is a sinusoidal carrier 100% modulated by another sinusoid, like the blue plot.

(https://www.eevblog.com/forum/programming/extract-precise-amplitude-and-phase-from-a-frequency-sweep-(vna-from-dsoawg)/?action=dlattach;attach=1646174;image)

I didn't understand the second part.  How to use this particular arrangement, or what would be its strong points?
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 22, 2022, 09:00:56 pm
How to use this particular arrangement, or what would be its strong points?
You send a single pulse of duration T, preceded and followed by silence.  Note that T>>1/f, at least a few hundred per f (i.e. few hundred full waves).
You capture all data, delineated by silence.
(You will wait until the DUT produces no output signal anymore, before you generate the next pulse at a new frequency f.)

The benefit is that a single FFT over the entire signal describes the signal content.  We know analytically its Fourier transform exactly (and can map it to the expected FFT if perfect digital->analog->digital roundtrip).  Comparing it to the generated signal (no DUT) tells us how the signal generation deviates from the expected.  Comparing it to the measured signal tells us what the DUT did to it.

Because there is no windowing (other than in the original signal, the envelope shaping by sin²) –– you can pad the "window" with zeroes to get whatever FFT size you want, or just use a single window of size matching the duration between silences ––, there are no mathematical compensation needed (except maybe for converting a continuous Fourier transform to a discrete one via integration, but that can be done algebraically): no numerical compensation needed, no convolution.  Examination in pure frequency domain.

The downside is that it tells how the DUT reacted to (this sort of a pulse at) a single, specific frequency, on the input.  The upside is that even if the DUT shifts the signal frequency, or generates some weird fractional harmonics of it, and does not pass any of the signal through at the original frequency, all of this shows up perfectly in the FFT.  So, it is not about detecting amplitude or phase change in the input signal, it is about detecting the spectral effects caused by the DUT to a particular frequency.

For example, it might be interesting to do such a test with f = 50 Hz or 60 Hz, to examine exactly what happens if mains frequency gets coupled to the input of the device under test.  Or, if the device has a suspicious dip or spike in its amplitude response in a frequency sweep, you might do this to examine that frequency in more detail.

Anyway, do remember that I only do data, not RF equipment in the real world!  I'm most comfortable when trying to find interesting stuff in data without any preconceptions or expectations.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 22, 2022, 11:51:51 pm
convolution(a(t),b(t)) in time domain is equivalent to multiply(A(s),B(s)) in complex frequency domain, and vice versa.

A(s) = Laplace_transform(a(t))
B(s) = Laplace_transform(b(t))
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: DiTBho on November 22, 2022, 11:53:49 pm
You send a single pulse of duration T, preceded and followed by silence

ideally a pure Dirac pulse  :D
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 23, 2022, 01:29:04 am
convolution(a(t),b(t)) in time domain is equivalent to multiply(A(s),B(s)) in complex frequency domain, and vice versa.
Sure.  Thing is, if you have a chirp, a frequency sweep, and you window it (with overlapping windows) with a suitable size, and do an FFT of that, you need to account for the effect of the windowing function on the computed FFT, to get a representation of the original signal.

Even then, that FFT is a frequency-domain representation of the time-domain signal during that time window.  Because the frequency changes continuously in the time domain, the frequency domain magnitude spike is always spread out a bit.  (It is also spread out a bit due to the windowing et cetera, but the changing frequency exacerbates that.)  If the rate of frequency change is sufficiently low compared to the FFT size, then the effect is small.

One can take the windowed FFT centered at any point in time, even from consecutive samples.  But even then, the FFT represents the frequency domain of the signal within the FFT time window, not at a particular sample.

The question is, what does one want to find out?  FFT is a poor tool if you want to measure e.g. attenuation or phase change of a known signal, but a good tool if you are interested in the frequency spectrum of the measured signal.

Constructing a shaped pulse of a specific frequency yields an analytically known easily calculated frequency spectrum.  The pulse is self-windowing, so that taking the FFT of the measured signal in one chunk, from silence before to silence after, yields directly the frequency spectrum, exactly because we are not interested in any time-dependent variance in it.  The two can be compared one-to-one, without any convolution needed (to undo the effect of a windowing function).  And taking the FFT of the real-world generated signal, and comparing that to the computed Fourier transform, tells how well the signal generation and capture matches the expected; basically gives a simple test/calibration approach, albeit at a single frequency per test pulse.

Mathematically this is rather lightweight, because the Fourier transform can be calculated as needed (it does not need to be precalculated or stored).

Convolution is just one (complex) multiplication sum per FFT bin for each FFT, so it isn't that costly; that's not the issue.  The issue is its practical effect on the FFT.  Consider input time-domain data consisting of 8-bit samples.  The quantization noise creates a noise floor at about 44-48 dB (it is more or less flat, similar to white noise).  If you then apply any kind of convolution to compensate for the effect of the windowing function on the spectrum, it will apply to the quantization noise as well.  Then, if your compensated samples are mapped back to integers, you have a second set of quantization errors, except this time in the frequency domain (as the convolution then does not exactly match the intended one).  It gets quite fuzzy fast, so omitting the entire step can be a big win in the frequency domain accuracy.

(Edited to add the missing crucial sum in the above paragraph.)



Now, it is important to realize that any signal can be reconstructed from its Fourier transform.  This means that if you do an FT over a single, complete chirp, the FT is a complete description of it: it does describe how it progresses through the frequency, and you can reproduce the original time-domain signal using the FT.  (I do believe this is also algebraically known, if the frequency changes during the chirp in an easily described manner, say linearly or exponentially.)

It is only that it is difficult to see or determine how the time-domain signal evolved during the FFT window from the FFT data itself.
(I don't know if it is even possible in any other way except actually reconstructing the time-domain signal, and observing its characteristics.)

Because of this, a single FFT over an entire chirp, compared to the computed (or measured at signal generator), does describe the full frequency-domain response, but it is very difficult to say which extraneous frequencies were caused by which input frequencies, because that information (albeit embedded within the FFT, since it can reconstruct the original signal) is difficult if not impossible to extract directly from the FFT itself.

To recover that, the measured signal is windowed, into many overlapping windows, with window length corresponding to the lowest frequency one is interested in (taking into account the windowing function effect on the resulting FFT), and FFT taken of each window separately.

What I found interesting, and why I posted these few messages, is that generating a different signal –– a modulated constant-frequency pulse ––, there would be no need for either windowing (because the signal itself is naturally "one window"), nor compensating for windowing function effects.  It would reveal details related to only a very narrow slice of the entire possible input frequency domain, but what it would reveal, would be clear and straightforward, standalone description.

Now, I just hope I didn't waste time for anyone who read this, because while I find it interesting, I haven't checked it out in practice; only done "back of the envelope" calculations and estimates to verify it indeed is interesting, and not just a random idea that popped into my mind.  Hopefully, the above explains what and why I found interesting in it.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Marco on November 23, 2022, 09:06:30 pm
So how is this for an actual procedure?

Determine a 2^n block size the generator and 2^(n+1) the digitizer supports. Fill a spectrum with unity magnitude and pseudo-random phases (periodic random noise). Do real-iFFT, scale to fill the generator dynamic range. Generate and digitize, do real-FFT, ignore odd frequencies and subtract pseudo random phases.

The larger digitizer capture range gives some realistic wiggle room for delay, without a true steady state excitation.
Title: Re: Extract precise amplitude and phase from a frequency sweep (VNA from DSO+AWG)
Post by: Nominal Animal on November 24, 2022, 03:11:38 pm
So how is this for an actual procedure?
You'll find out how the device under test shapes white noise.

Thing is, you do not find out what the device does as a response to any particular frequency input signal.

A chirp –– a frequency sweep –– does this in a continuous manner, basically covering all frequencies within the sweep range.  It is more difficult to analyse, because of how the time domain and frequency domain are intertwined, but windowing short durations of the sweep, and doing an FFT on that window, gives a good compromise.

My single-frequency sine-squared-shaped pulse suggestion eliminates the windowing part, but at the cost of only examining a single input frequency, and having to compute quite large FFTs (whole signal at once).



Right now, I'm wondering if visual comparison between algebraic Fourier transforms of sinusoidal signals, and their sampled (discrete) FFTs, might be useful.  I could create a standalone HTML (similar to the FIR filter analysis page (https://www.nominal-animal.net/answers/fir-analysis.html)) that lets one define a signal in algebraic terms –– as a function ––, and have the page calculate both the algebraic Fourier transform, as well as the FFT of the same from the sampled function.  One is the ideal, the other simulates the real-world thing.  Looking at things like quantization (limiting to N-bit precision), sampling jitter, and various sampling delays (in the sub-sample range), might be quite illuminating.

I just don't really want to write an FFT in Javascript (specifically, for real data), and using an existing library would make it non-standalone (unless one includes the library source in the HTML).