I'm working on a WaveForms script which would be able to generate chart with the frequency profile of a liquid.
For this, I need to use a Wavegen and a Scope: first I need to set the generator to a certain frequency and then I'd like to check the power of that signal on the scope. Between the probes of the generator and the scope there will be the liquid I would like to test.
I created a basic script to step from 1 Hz to 150 Hz, check that signal with the scope, repeat this loop 5 times, and calculate the min, max and average values at each frequency.
Although the script works, I have a few questions:
* I couldn't find any documentation about the Scope.Channelx.fftmagnitude and fftfrequency arrays. Where should I look for them? Are there any other properties related to the FFT configuration?
* Is there a way to set the number of BINs and samples of the scope's FFT?
* Can I somehow use the Goertzel algorithm instead of FFT? (The Görtzel algorithm is designed to tell us the power of a signal at one predefined frequency. https://en.wikipedia.org/wiki/Goertzel_algorithm) I would need this to get the power for the exact Hz value I define, and not the closest one I have from the FFT like I do it now.
* Is there a way to change the color/style of the charts?
Here is the script itself, but bear in mind that it is just the very first version
const hzmax =150;// the maximum frequency to measureconst csamples =5;// the number of samples to measure for each frequencyconst cdatapoints =150;// chart X-axis resolution (currently: 1 datapoint / 1 Hz)var datapoints =newArray(4);// datapoint array
datapoints[0]=newArray(cdatapoints);// max
datapoints[1]=newArray(cdatapoints);// avg
datapoints[2]=newArray(cdatapoints);// min
datapoints[3]=newArray(cdatapoints * csamples);// raw data// clear the raw data arrayfor(var j =0; j < cdatapoints * csamples; j++){
datapoints[3][j]=0;}// generate the freqeuncies to measurevar frequencies =newArray(cdatapoints);for(var j =0; j < cdatapoints; j++){
frequencies[j]= j+1;}{// configure plot
plot1.X.Units.text ="Hz";
plot1.X.Offset.value =-hzmax/2
plot1.X.Range.value = hzmax;
plot1.Y1.AutoScale.checked =false;
plot1.Y2.AutoScale.checked =false;
plot1.Y3.AutoScale.checked =false;
plot1.Y4.AutoScale.checked =false;const vmax =1.00;const vmin =0.98;var offset =-(vmin +(vmax - vmin)/2);var range =(vmax - vmin);
plot1.Y1.Offset.value = offset;
plot1.Y2.Offset.value = offset;
plot1.Y3.Offset.value = offset;
plot1.Y4.Offset.value = offset;
plot1.Y1.Range.value = range;
plot1.Y2.Range.value = range;
plot1.Y3.Range.value = range;
plot1.Y4.Range.value = range;}{// configure wavegenWavegen1.Channel1.Mode.text ="Simple";Wavegen1.Channel1.Simple.Type.text ="Sine";}{// configure scopeScope1.BufferSize.value =1;Scope1.Buffer.value =1;Scope1.Time.Mode.text ="Shift";Scope1.Channel1.checked =true;Scope1.Channel2.checked =false;}Wavegen1.run();for(var n=0; n<csamples; n++){Scope1.Time.Samples.value =1024;for(var i=0; i<hzmax; i++){// change the frequency to the next onevar frequency = frequencies[i];Wavegen1.Channel1.Simple.Frequency.text = frequency;Scope1.Time.Rate.value = frequency *256;
print("Frequency: ",frequency," Hz | Running @ ",Scope1.Time.Rate.value," Hz | ",Scope1.Time.Samples.value," samples");// take a single sample Scope1.single();Scope1.wait();// check out the FFT valuesvar rgmag =Scope1.Channel1.fftmagnitude;var rghz =Scope1.Channel1.fftfrequency;var c = rgmag.length;var minHzDistance = frequency;// check all the FFT values, and get the one closest to our inspected frequencyfor(var j =0; j < c; j++){var hz = rghz[j]var hzDistance = frequency - hz;if(hzDistance <0){
hzDistance =-hzDistance;}if(hzDistance <= minHzDistance){
minHzDistance = hzDistance;
datapoints[3][i*csamples + n]= rgmag[j];}}// calculate the min, max, avg valuesvar rgmin =1000.0;var rgavg =0.0;var rgmax =0.0;var validsamples =0;for(var j =0; j < csamples; j++){
currentsample = datapoints[3][i*csamples + j];if(currentsample !=0){if(currentsample < rgmin){
rgmin = currentsample;}if(currentsample > rgmax){
rgmax = currentsample;}
rgavg += currentsample;
validsamples++;}}
rgavg /= validsamples;
print("Min:",rgmin," V | Avg:",rgavg," V | Max:",rgmax," V | error: +/-",minHzDistance," Hz");
print();// save the calculated data into their arrays
datapoints[0][i]= rgmax;
datapoints[1][i]= rgavg;
datapoints[2][i]= rgmin;// check if the error if too highif(minHzDistance >0.5){Scope1.Time.Samples.value *=2;
i--;}// plot the most recent values on the chart
plot1.Y1.data = datapoints[0];// max
plot1.Y2.data = datapoints[1];// avg
plot1.Y3.data = datapoints[2];// min}}
Question
Andras
Hi,
I'm working on a WaveForms script which would be able to generate chart with the frequency profile of a liquid.
For this, I need to use a Wavegen and a Scope: first I need to set the generator to a certain frequency and then I'd like to check the power of that signal on the scope. Between the probes of the generator and the scope there will be the liquid I would like to test.
I created a basic script to step from 1 Hz to 150 Hz, check that signal with the scope, repeat this loop 5 times, and calculate the min, max and average values at each frequency.
Although the script works, I have a few questions:
* I couldn't find any documentation about the Scope.Channelx.fftmagnitude and fftfrequency arrays. Where should I look for them? Are there any other properties related to the FFT configuration?
* Is there a way to set the number of BINs and samples of the scope's FFT?
* Can I somehow use the Goertzel algorithm instead of FFT? (The Görtzel algorithm is designed to tell us the power of a signal at one predefined frequency. https://en.wikipedia.org/wiki/Goertzel_algorithm) I would need this to get the power for the exact Hz value I define, and not the closest one I have from the FFT like I do it now.
* Is there a way to change the color/style of the charts?
Here is the script itself, but bear in mind that it is just the very first version
Link to comment
Share on other sites
5 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.