• 0

How to generate a lot of waveforms using for loop?


Question

clear()

if(!('Wavegen' in this) || !('Scope' in this)) throw "Please open a Scope and a Wavegen instrument";



if (Tool.question("Frequency should be constant?") == true) {

frequencyconst = Tool.getInteger("Frequency constant value is", 0, 0, 10000)

print("Frequnecy is constant and the value is " + frequencyconst )

} else {

frequencyfrom= Tool.getInteger("Frequency range from [Hz]", 0, 0, 100)

frequencyto = Tool.getInteger("Frequency range to [Hz]", 0, 0, 100)

frequencystep= Tool.getInteger("Frequency number of steps", 0, 0, 100)

print("Frequnecy range is from: " + frequencyfrom + " Hz to " + frequencyto + " Hz and the step is " + frequencystep)}



if (Tool.question("Generator voltage should be constant?") == true) {

voltageconst = Tool.getInteger("Generator voltage constant value is", 0, 0, 10000)

print("Generator voltage is constant and the value is " + voltageconst )

} else {

voltagefrom= Tool.getInteger("Generator voltage range from [mV]", 0, 0, 100)

voltageto = Tool.getInteger("Generator voltage range to [mV]", 0, 0, 100)

voltagestep= Tool.getInteger("Generator voltage number of steps", 0, 0, 100)

print("Generator voltage range is from: " + voltagefrom + " mV to " + voltageto + " mV and the step is " + voltagestep)}



Wavegen.Channel1.Mode.text = "Simple";

Wavegen.Channel1.Simple.Frequency.value = frequencyconst;

Wavegen.Channel1.Simple.Amplitude.value = voltageconst;

Wavegen.run();

Scope.run();

 

 

Hello! This is my code to generate sinusoidal waveform. But it works only for constant frequency and voltage.


I need help with for loop or something to generate this waveform but for example after choosing:

Frequnecy range is from: 0 Hz to 10 Hz and the step is 5 

Generator voltage range is from: 5 mV to 10 mV and the step is 2

 

0,2,6,8,10 Hz with 5 mV  and 0,2,6,8,10 Hz with 10 mV. So i want to generate 10 waveforms with an interval of for example 5 sec.

Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Hi @Monika997

var rgAmp = [0.005, 0.01]
var rgFreq = [0,2,6,8,10]

rgAmp.forEach(function(amp){
    Wavegen.Channel1.Simple.Amplitude.value = amp // amplitude voltage is half of Vpk2pk
    Wavegen.Channel1.checked = true // enable, output offset or initial value
    
    rgFreq.forEach(function(freq){
        if(freq==0){
            Wavegen.Channel1.stop()
        }else{
            Wavegen.Channel1.Simple.Frequency.value = freq
            Wavegen.Channel1.run()
        }

        wait(0.1) // you may want to wait a bit for the external test/circuit latency, like 100ms
        Scope.single()
        if(!Scope.wait()) throw "stopped"
    })
})
Wavegen.Channel1.stop()

image.png.37490e31df434f6a9a5899c5a3c076cb.png

Link to post
Share on other sites
  • 0
Posted (edited)
16 minutes ago, attila said:

Hi @Monika997


var rgAmp = [0.005, 0.01]
var rgFreq = [0,2,6,8,10]

rgAmp.forEach(function(amp){
    Wavegen.Channel1.Simple.Amplitude.value = amp // amplitude voltage is half of Vpk2pk
    Wavegen.Channel1.checked = true // enable, output offset or initial value
    
    rgFreq.forEach(function(freq){
        if(freq==0){
            Wavegen.Channel1.stop()
        }else{
            Wavegen.Channel1.Simple.Frequency.value = freq
            Wavegen.Channel1.run()
        }

        wait(0.1) // you may want to wait a bit for the external test/circuit latency, like 100ms
        Scope.single()
        if(!Scope.wait()) throw "stopped"
    })
})
Wavegen.Channel1.stop()

image.png.37490e31df434f6a9a5899c5a3c076cb.png

 

 

Thank you so much @attila

I've got one more question. 

These values are already given in the code - var rgAmp = [0.005, 0.01] var rgFreq = [0,2,6,8,10] but in my program I enter the range and the number of steps in the popup window.  I would like the two-dimensional array to be made up of these values entered in the window.

 

 

if (Tool.question("Frequency should be constant?") == true) {

frequencyconst = Tool.getInteger("Frequency constant value is", 0, 0, 10000)

} else {

frequencyfrom= Tool.getInteger("Frequency range from [Hz]", 0, 0, 100)

frequencyto = Tool.getInteger("Frequency range to [Hz]", 0, 0, 100)

frequencystep= Tool.getInteger("Frequency number of steps", 0, 0, 100)

 

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

Hi @Monika997

You can use something like this:

function conf(amp, freq){
    Wavegen.Channel1.Simple.Amplitude.value = amp
    if(freq==0){
        Wavegen.Channel1.stop()
    }else{
        Wavegen.Channel1.Simple.Frequency.value = freq
        Wavegen.Channel1.run()
    }

    wait(0.1) // you may want to wait a bit for the external test/circuit latency, like 100ms
    Scope.single()
    if(!Scope.wait()) throw "stopped"
}


if (Tool.question("Frequency should be constant?") == true) {
    frequencyconst = Tool.getInteger("Frequency constant value is", 0, 0, 10000)
    conf(0.005, frequencyconst)
} else {
    frequencyfrom= Tool.getInteger("Frequency range from [Hz]", 0, 0, 100)
    frequencyto = Tool.getInteger("Frequency range to [Hz]", 0, 0, 100)
    frequencystep= Tool.getInteger("Frequency number of steps", 0, 0, 100)

    for(var i = 0; i < frequencystep; i++){
        conf(0.005, frequencyfrom + (frequencyto - frequencyfrom)*i/frequencystep)
    } 
}

 

Link to post
Share on other sites
  • 0
On 1/11/2021 at 11:42 AM, attila said:

Hi @Monika997

You can use something like this:




function conf(amp, freq){
    Wavegen.Channel1.Simple.Amplitude.value = amp
    if(freq==0){
        Wavegen.Channel1.stop()
    }else{
        Wavegen.Channel1.Simple.Frequency.value = freq
        Wavegen.Channel1.run()
    }

    wait(0.1) // you may want to wait a bit for the external test/circuit latency, like 100ms
    Scope.single()
    if(!Scope.wait()) throw "stopped"
}


if (Tool.question("Frequency should be constant?") == true) {
    frequencyconst = Tool.getInteger("Frequency constant value is", 0, 0, 10000)
    conf(0.005, frequencyconst)
} else {
    frequencyfrom= Tool.getInteger("Frequency range from [Hz]", 0, 0, 100)
    frequencyto = Tool.getInteger("Frequency range to [Hz]", 0, 0, 100)
    frequencystep= Tool.getInteger("Frequency number of steps", 0, 0, 100)

    for(var i = 0; i < frequencystep; i++){
        conf(0.005, frequencyfrom + (frequencyto - frequencyfrom)*i/frequencystep)
    } 
}

 

 

 

 

So now my code look like this:
 

clear()
if(!('Wavegen' in this) || !('Scope' in this)) throw "Please open a Scope and a Wavegen instrument";



function conf(amp, freq){
    Wavegen.Channel1.Simple.Amplitude.value = amp
    if(freq==0){
        Wavegen.Channel1.stop()
    }else{
        Wavegen.Channel1.Simple.Frequency.value = freq
        Wavegen.Channel1.run()
    }

    wait(0.1) //100ms
    Scope.single()
    if(!Scope.wait()) throw "stopped"
}







if (Tool.question("Frequency should be constant?") == true) {
    frequencyconst = Tool.getInteger("Frequency constant value is", 0, 0, 10000)
    conf(0.005, frequencyconst)
} else {
    frequencyfrom= Tool.getInteger("Frequency range from [Hz]", 0, 0, 100)
    frequencyto = Tool.getInteger("Frequency range to [Hz]", 0, 0, 100)
    frequencystep= Tool.getInteger("Frequency number of steps", 0, 0, 100)

    for(var i = 0; i < frequencystep; i++){
        conf(0.005, frequencyfrom + (frequencyto - frequencyfrom)*i/frequencystep)
    } 
}




if (Tool.question("Generator voltage should be constant?") == true) {
    voltageconst = Tool.getInteger("Generator voltageconstant value is", 0, 0, 10000)
    conf(0.005, voltageconst)
} else {
    voltagefrom= Tool.getInteger("Generator voltagerange from [mV]", 0, 0, 100)
    voltageto = Tool.getInteger("Generator voltage range to [mV]", 0, 0, 100)
    voltagestep= Tool.getInteger("Generator voltage number of steps", 0, 0, 100)

    for(var i = 0; i < voltagestep; i++){
        conf(0.005, voltagefrom + (voltageto - voltagefrom)*i/voltagestep)
    } 
}



Wavegen.Channel1.Mode.text = "Simple";
Wavegen.Channel1.Simple.Frequency.value = frequencyconst;
Wavegen.Channel1.Simple.Amplitude.value = voltageconst;
Wavegen.run();
Scope.run(); 




Here is my problem: For example, by entering a fixed frequency value, it changes without any problem. However, wanting to change the constant voltage value, it is not the amplitude that changes, but also the frequency.

 

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

Hi @Monika997

If you look carefully your code, each conf function call configures amplitude and frequency, and performs a Scope capture.
Then at the end or your code you are setting frequency/voltageconst and starting Wavegen and Scope.
The *const variables may are not even be initialized if in your questions not constant is selected.

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

Hi @Monika997

If you look carefully your code, each conf function call configures amplitude and frequency, and performs a Scope capture.
Then at the end or your code you are setting frequency/voltageconst and starting Wavegen and Scope.
The *const variables may are not even be initialized if in your questions not constant is selected.

 Unfortunately, I don't really understand how to fix it.

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