Labrador icon indicating copy to clipboard operation
Labrador copied to clipboard

Frequency response calculation

Open mmehari opened this issue 10 months ago • 14 comments

This PR will bring frequency response calculation to espotek's oscilloscope functionality. Interface wise, it will be located under the oscilloscope tab, next to Frequency Spectrum.

freqResp_interface

Operation wise, a frequency response calculation requires

  1. input source to excite the circuit-under-test (CUT) with sinusoidal waveform of varying frequencies, and
  2. two output measurements for capturing the excitation and response wave-forms

To this end, Signal Gen CH1 will be used for 1 and Oscilloscope CH1/CH2 will be used for 2.

Here is an example circuit-under-test (CUT) for measuring the frequency response of an RC circuit.

freqResp_CUT

Frequency response calculation steps

  1. generate a sinusoidal frequency tone on Signal Gen CH1
  2. capture 1 cycle of input and output wave-forms
  3. using least squares regression, fit a sinusoidal to captured wave-forms
  4. calculate gain(dB) and phase response (degree)
  5. plot frequency response (gain or phase)
  6. update next frequency tone on Signal Gen CH1 and start all over again

freqResp_output

Note: Frequency response calculation has been tested on Linux and RPI5 platforms.

mmehari avatar Jan 31 '25 23:01 mmehari

This is awesome! Great to see these enhancements!

MatthewSteeves avatar Feb 01 '25 14:02 MatthewSteeves

Thanks for your great contribution!

Signal Gen CH1 will be used for #1 and Oscilloscope CH1/CH2 will be used for #2.

Did you notice GH is using # as "indicator" for linking to issues? Guess it was meant to write:

Signal Gen CH1 will be used for 1. and Oscilloscope CH1/CH2 will be used for 2. Did I get this right?

mi-hol avatar Feb 10 '25 09:02 mi-hol

That is correct, and I noticed it after making the PR.

mmehari avatar Feb 10 '25 09:02 mmehari

Would you mind to edit the text, so it could be directly copied to WIKI page as documentation, after merge of PR?

mi-hol avatar Feb 10 '25 10:02 mi-hol

I have made the change

mmehari avatar Feb 10 '25 11:02 mmehari

@turboencabulator would perhaps reviewing this PR work for you? In that case please assign yourself as "reviewer"

mi-hol avatar Feb 12 '25 11:02 mi-hol

Or possibly @MatthewSteeves. He seems to have interest.

mmehari avatar Feb 12 '25 12:02 mmehari

@mmehari Awesome work! Do you know if there is a maximum frequency that this supports? I built the same circuit in your photo and it only plots the line to about 2.4kHz even though it steps past that. Not sure if I'm doing something wrong or if it's just a limitation somewhere.

turboencabulator avatar Feb 13 '25 05:02 turboencabulator

@mmehari Awesome work! Do you know if there is a maximum frequency that this supports?

Well the maximum frequency supported is related to the range of Signal Gen CH1, which is 62.5KHz.

I built the same circuit in your photo and it only plots the line to about 2.4kHz even though it steps past that. Not sure if I'm doing something wrong or if it's just a limitation somewhere.

It depends on a couple of things

  1. You might have used different RC values. Different Time Constant (τ) results in different frequency response.
  2. Lower input voltage results in lower output voltage, thus will not be able to plot higher frequency contents. Try using higher input voltages (9V) and see if you get past 2.4KHz

Note: it is possible that the frequency response plot jumps steps without plotting them. The frequency response calculation relies on least square fitting to collected samples and if the samples have lower magnitudes, the fitting residual (error) becomes very large and the step gets discarded.

mmehari avatar Feb 13 '25 21:02 mmehari

@mmehari @turboencabulator Great conversation, resulting in further improvements! This is collaboration at its best!

Note: it is possible that the frequency response plot jumps steps without plotting them. The frequency response calculation relies on least square fitting to collected samples and if the samples have lower magnitudes, the fitting residual (error) becomes very large and the step gets discarded.

Very good explanation for real world issues! Would it be fair to say that newbies (like me) would usually be unaware of the complexities of "what is going on behind the scenes" and consider "the step gets discarded" (I assume without a visual indicator of the fact) as an unexpected behaviour?

mi-hol avatar Feb 14 '25 08:02 mi-hol

It is not an unexpected behaviour in the sense of a bug from the calculation, but it is more like a limitation of the applied methodology. In the course of the frequency response calculation, you will encounter frequency values where the response from the circuit-under-test (CUT) is very weak (higher frequencies in RC circuits) and that can't be used to evaluate the frequency response. Thus, you simply discard it and continue on to the next frequency value.

mmehari avatar Feb 14 '25 09:02 mmehari

it is more like a limitation of the applied methodology

Thanks for clarification

mi-hol avatar Feb 14 '25 20:02 mi-hol

Along the way, I found one extra bug. The thing is oscilloscope CH2 UI is disabled in frequency response mode and it was not re-enabled when switching to frequency spectrum mode. The commit https://github.com/espotek-org/Labrador/pull/272/commits/a90deb6b3675baab9ec9ddb46539a912cd851470 will fix this.

mmehari avatar Feb 15 '25 05:02 mmehari

Thanks @mmehari! I think I've figured out why it wasn't plotting. The signal generator clips all of the sine wave peaks at 3.3V, and the valleys start to distort when the frequency goes past ~400Hz. (The falling edge slew rate looks like it's limited when below ~0.9V, probably a hardware limitation?) All this distortion introduced too much error, causing the measurement at that frequency to be discarded. I got the best results by setting the offset to 1V and amplitude to 2.2-2.3V. Results look good out to about 6kHz and then they start to get noisy and unreliable past 10kHz.

I've got a couple of wishlist items, I wouldn't consider any of these to be blockers though:

  • A button to clear the display and restart the measurement. I was clicking the up/down arrows on the min/max/step spinboxes to get it to restart, and it would be nice if there was a dedicated button for it.
  • Speaking of the up/down arrows, it would be nice if they incremented the most-significant digits instead of incrementing/decrementing by 1, like the frequency/amplitude/offset spinboxes.

turboencabulator avatar Feb 15 '25 17:02 turboencabulator

I have addressed both wishlists and let me know if that is what you have asked

mmehari avatar Feb 16 '25 20:02 mmehari

That's exactly what I was looking for. Thanks!

turboencabulator avatar Feb 16 '25 22:02 turboencabulator

That's exactly what I was looking for.

@turboencabulator image

Marking all comments as "resolved" via button image

would complete the review "officially" and open the merge window.

Example of open conversation: image

mi-hol avatar Feb 17 '25 12:02 mi-hol

This is awesome! Great to see these enhancements!

@MatthewSteeves would you perhaps mind to become the beta tester?

mi-hol avatar Feb 17 '25 13:02 mi-hol

First time properly looking through this thread. This is awesome man.

EspoTek avatar Feb 17 '25 23:02 EspoTek

would complete the review "officially" and open the merge window.

I think I clicked them all. (First time using the review feature.) Let me know if I missed any.

turboencabulator avatar Feb 18 '25 01:02 turboencabulator

The frequency response calculation requires both Oscilloscopes (CH1 and CH2) to be present and receive samples. Hence it beneficial to make them non checkable. commit https://github.com/espotek-org/Labrador/pull/272/commits/84541a90dde430e92ac3408eaa25d461aec140b0 does this.

mmehari avatar Feb 18 '25 02:02 mmehari

First time using the review feature

with success!

@mmehari LGTM (from list of code-review acronyms at https://www.codelantis.com/blog/code-review-acronyms-lgtm-nit)

Please go ahead and merge code, then update wiki

mi-hol avatar Feb 18 '25 09:02 mi-hol

This is awesome! Great to see these enhancements!

@MatthewSteeves would you perhaps mind to become the beta tester?

Hey I’ll take a look and provide feedback, though I’m not very qualified to really put the change through it’s paces. My background is IT/programming and electronics of (one of) my (too many) interests! 😄

MatthewSteeves avatar Feb 18 '25 11:02 MatthewSteeves

Wiki is updated. https://github.com/espotek-org/Labrador/wiki/Frequency-Response-Calculation

mmehari avatar Mar 23 '25 07:03 mmehari