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.
//taken from https://stackoverflow.com/questions/846221/logarithmic-slider
// and modified to suit my needs
function logslider(position, maxp) {
// position will be between 0 and 100
var minp = 0;
//var maxp = 50;
// The result should be between 100 an 10000000
var minv = Math.log(20);
var maxv = Math.log(20000);
// calculate adjustment factor
var scale = (maxv-minv) / (maxp-minp);
return Math.exp(minv + scale*(position-minp));
}
function doTHD(){
var freq
var slices =30
var rgTHD1 = []
var rgTHD2 = []
var rgFreq = []
for(var idx = 0; idx <= slices; idx++){
freq = logslider(idx, slices)
Wavegen1.Channel1.Simple.Frequency.value = freq
//Wavegen1.run() // start AWG
Wavegen1.Channel2.Simple.Frequency.value = freq
Wavegen1.run() // start AWG
wait(0.01) // settle time for the external circuit, expressed in seconds
Spectrum1.Frequency.Start.value = freq // adjust analyzer start frequencyTool
Spectrum1.Frequency.Stop.value = 10*freq // adjust analyzer stop frequencyTool
if(Spectrum1.Trace1.Config.Count.enable==true)
{
print ("averaging is enable");
var avg = 0
var avg2 = 0
for (i=0;i<Spectrum1.Trace1.Config.Count.value;i++)
{
Spectrum1.single() // start acquisition
if(!Spectrum1.wait()){ // wait to finish
return;
}
avg += Spectrum1.Trace1.measure("THD")
avg2 += Spectrum1.Trace2.measure("THD")
}
avg /= Spectrum1.Trace1.Config.Count.value
avg2 /= Spectrum1.Trace1.Config.Count.value
rgTHD1.push(100*Math.pow(10,avg/20))
rgTHD2.push(100*Math.pow(10,avg2/20))
}
else
{
Spectrum1.single() // start acquisition
if(!Spectrum1.wait()){ // wait to finish
return;
}
rgTHD1.push(100*Math.pow(10,Spectrum1.Trace1.measure("THD")/20))
//rgFreq.push(Spectrum1.Trace2.measureFreq("FF"))
rgTHD2.push(100*Math.pow(10,Spectrum1.Trace2.measure("THD")/20))
}
rgFreq.push(Spectrum1.Trace1.measureFreq("FF"))
}
Wavegen1.stop()
print(rgFreq, rgTHD1, rgTHD2) // print data for copy paste
var file = File("~/Desktop/THDvsFreq.csv")
file.appendLine("Freq,THDN CH1, THDN ch2")
for (i=0;i<= slices;i++)
{file.appendLine(rgFreq+","+rgTHD1+","+rgTHD2)} // draw in plot1, View / Add plot plot1.X.Units.text = "Hz" plot1.Y1.Units.text = "%" plot1.Y2.Units.text = "%" plot1.X.data = rgFreq plot1.Y1.data = rgTHD1 plot1.Y2.data = rgTHD2 plot1.update()
} doTHD();
thd1watt.bmp
THD_vs_Power.dwf3script
THD_vs_Freq_%.dwf3script