Question

I am trying to find the script commands to access the Spectrum Analyzer in the Analog Discovery 2. I would like to specifically access the THD function to read a series of values at different frequencies (using the Wavegen), plot them and save them to a file. I am generally familiar with the use of scripts and have made them work on the scope, wavegen, etc. but cannot seem to find the commands to access the Spectrum Analyzer in Waveforms 2015. I have also used the debug function as well as the plot function so what I need are the specific access routines for the spectrum analyzer and its subfunctions (like THD, SNR, etc.)

Link to post
Share on other sites

Recommended Posts

  • 0
On 8/28/2017 at 3:08 AM, attila said:

Hi @abzza @tomtektest

With WF 3.6.8 you can access Spectrum measurements, like:

print(Spectrum1.Trace1.measureFreq("FF")+" Hz") // fundamental frequency
print(Spectrum1.Trace1.measure("FF")+" dBV") // magnitude
print(Spectrum1.Trace1.measure("THD")+" dBc") // total harmonic distortion

Excellent, thank you!

@tomtektest - I'm working on a Python script to at least do some basic automation of THD analysis across the 20hz-20khz spectrum. It won't be as pretty as the Audio Analyzer Suite, but it will get the job done. Will send it to you when it's ready to go!

Link to post
Share on other sites
  • 0
6 hours ago, abzza said:

Excellent, thank you!

@tomtektest - I'm working on a Python script to at least do some basic automation of THD analysis across the 20hz-20khz spectrum. It won't be as pretty as the Audio Analyzer Suite, but it will get the job done. Will send it to you when it's ready to go!

I'm glad to see someone working on this; please keep us informed of your progress. I have not yet found a list of the new SA commands in 3.6.8, have you found them? I also look forward to seeing your script and would appreciate getting a copy (or access to it through the Digilent Forum) I have experience (commercial with languages like Java Script but have only used Python in small applications to help my granddaughters with some Raspberry Pi projects.

Link to post
Share on other sites
  • 0

Hi @tomtektest, @abzza

With WaveForms Script THD and other measurement logging and plotting can be automated, like this:

i1.thumb.png.fb3fee13e1cbab2218dc54ae72bd0a47.png

function doTHD(){
    var rgTHD = []
    var rgFreq = []
    for(var idx = 1; idx <= 100; idx++){
       Wavegen1.Channel1.Simple.Frequency.value = 1000*idx
       Wavegen1.run() // start AWG
       wait(0.01) // settle time for the external circuit, expressed in seconds
       Spectrum1.Frequency.Stop.value = 20*Wavegen1.Channel1.Simple.Frequency.value // adjust analyzer stop frequency
       Spectrum1.single() // start acquisition
       if(!Spectrum1.wait()){ // wait to finish
          return;
       }
       rgFreq.push(Spectrum1.Trace1.measureFreq("FF"))
       rgTHD.push(Spectrum1.Trace1.measure("THD"))
    }
    Wavegen1.stop()
    print(rgFreq, rgTHD) // print data for copy paste
    // draw in plot1, View / Add plot
    plot1.X.Units.text = "Hz"
    plot1.Y1.Units.text = "dBc"
    plot1.X.data = rgFreq
    plot1.Y1.data = rgTHD
}
doTHD();

 

Edited by attila
Link to post
Share on other sites
  • 0

Hi, i modified attila script slightly so that the thd values are in % and the range is in the usual audio band.

Hope it could be useful for others.

 

function doTHD(){

var rgTHD = []

var rgFreq = []

for(var idx = 20; idx <= 20000; idx+=19980/30){

Wavegen1.Channel1.Simple.Frequency.value = idx

Wavegen1.run() // start AWG

wait(0.01) // settle time for the external circuit, expressed in seconds

Spectrum1.Frequency.Stop.value = 20*Wavegen1.Channel1.Simple.Frequency.value // adjust analyzer stopfrequency

 

Spectrum1.single() // start acquisition

if(!Spectrum1.wait()){ // wait to finish

return;

}

rgFreq.push(Spectrum1.Trace1.measureFreq("FF"))

rgTHD.push(100*Math.pow(10,Spectrum1.Trace1.measure("THD")/20))

}

Wavegen1.stop()

print(rgFreq, rgTHD) // print data for copy paste

// draw in plot1, View / Add plot

plot1.X.Units.text = "Hz"

plot1.Y1.Units.text = "%"

plot1.X.data = rgFreq

plot1.Y1.data = rgTHD

}

doTHD();

Link to post
Share on other sites
  • 0

Thanks @BaffleChallenged - I did much the same, although I incremented the script 1hz at a time for 20000 readings across the spectrum:

for(var idx = 20; idx <= 20000; idx++) // 20hz to 20khz, 1hz increments

The script takes about ~10 minutes to complete. Here was the output from a little LM1875 chip kit driving an 8 ohm dummy load. Neat! This was before I added the conversion from dB to % for THD (thanks this website for the explanation!), but it worked out to an average of -62dB, or 0.079% THD. Not too shabby for an eBay kit running in the open air.

Another useful addition I added was to average all the THD values to give you a single value to compare against, as well as formatting the output into something more CSV friendly in case the results need to be saved and plotted elsewhere. I'll upload my script changes as well when I'm back at my laptop.

Test-Setup.jpg

Screen---THD-Graph.jpg

Link to post
Share on other sites
  • 0

here is another version of the script i am working on,

it does both channel

it does scale the frequencies logarithmically

it does the averaging if trace 1 type allows count to be enabled.

it saves to a desktor CSV file for data manipulation in a speadsheet.

 

I made some modifications to the spreadsheet I use in hope it will be more generic and useful to others.

I also wrote some short instructions on how to proceed, but I am afraid it won't be clear for beginners.

I also made a drawing of dummy load and possible attenuator for interconnection with the Analog Discovery.

I

 

 

thd1watt.bmp

 

there were errors in the files, new files will be posted in an other post.

Edited by BaffleChallenged
Link to post
Share on other sites
  • 0

I cannot try this right away, but I appreciate your work and will definitely look more at it in a little while. Keep up the good work and perhaps you could do a video about it at some point and post it on YouTube. Let me know if I can help in any way. I know there are many [email protected] users who are looking for something like this for audio testing. tomtektest

 

Link to post
Share on other sites
  • 0

Hi tomtektest, I do follow your YouTube channel, I personnaly did not do any video. I do posses a digital photo camera and a tripod, but no editing software. (also note that English is not my primary language)

My interest in audio measurement is sparked from my experiment in amplifier design and build. This script is just one piece of the puzzle. I still miss THD vs power, but that will be trivial to build from the THD vs Freq above, just a few changes i presume.

I do follow a certain sequence, mostly due to setup constraints, and collect information in a spread sheet.

This is still messy at present, it is a work in progress.

If I come with something manageable, perhaps I could make the content puplicly available (spreadsheet, scripts, instructions on setting up and performing measurements).

If you review this content and think it makes senses, perhaps you could do a video on it.

Regards,

Link to post
Share on other sites
  • 0

Thanks @attila!

Yeah, I'm using the BNC adapter board and some heavy duty, shielded BNC cables for both the AWG and Scope channels - found much cleaner results overall. Thanks for the heads-up though! Busy putting together a selectable 8ohm/4ohm dummy load that can just plug straight into the Analog Discovery via BNC, which will make testing amplifiers much quicker and easier.

Link to post
Share on other sites
  • 0

There were errors in the files I posted in an earlier post, mostly got confused and used THD+N everywhere.

The math is probably ok, if anyone spot a fault, please let me know.

Also take note that you need to setup your Waveform environment correctly, look at Attila post above to get a visual clue of what you need to enable.

Otherwise there are going to be errors while running the script.

I did not have time to have a shot at measuring IMD, still more to come.

In hope that it is useful, here are some corrected files.

THD_vs_Freq_%.dwf3script

DummyLoad_2.pdf

instructions.txt

DummyLoad.dch

Amplificateur_mesure.ods

THD_vs_Power.dwf3script

Link to post
Share on other sites
  • 0

Hello all,

Thank you so much for scripts you posted ! There were very useful for me !

But I noticed something strange: after running a similar script, I found that the script stops by itself after maybe 5 minutes.

I want it to keep running until I decide to stop it. Is there a restriction on that ?

Another question : is there a way to just run a waveform script by an external software, considering a saved workspace ?

It could be someting like calling the application waveforms with 2 arguments like : "waveforms <workspace> <script>"

This could be very useful !!

Thank you in advance

Regards

Link to post
Share on other sites
  • 0

Hi @4x4

In newer software versions the Network Analyzer has THD measurement. You may not need or need a simpler script.

To load a workspace and run the script:
waveforms example.dwf3work -runscript
To open and run a script:
waveforms -script example.js 
To run the current script:
waveforms -runscript

The previously posted script performs one sweep of 31 steps, see:
for(var idx = 0; idx <= slices; idx++)

If you want me to look at your script, why does it stop, please post or send it in message.

image.png.98997cb58d42b0434e3f55b2d5671b1f.png

Edited by attila
Link to post
Share on other sites
  • 0

Any thoughts on building this functionality into Waveforms?  I know the Audio Suite exists, but us Linux and Mac users are left out.  I had asked the author about releasing the source and he was not open to that at all.

Thanks
Cory

 

Link to post
Share on other sites
  • 0
4 hours ago, attila said:

Hi @clytle374

Which functionality are you referring to?
Almost all the function are available under Windows as well under Linux and MacOS.

Sorry I was referring to the functions of the "Audio Analyzer Suite" or these scripts. 

Cory

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now