• 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.

## Recommended Posts

• 0
```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()```

##### Share on other sites
• 0
Posted (edited)
16 minutes ago, attila said:
```
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()```

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
##### Share on other sites
• 0

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)
}
}```

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

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
• 0

##### Share on other sites
• 0

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.

##### Share on other sites
• 0
On 1/13/2021 at 11:17 AM, attila said:

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.

I edited my code so that it would ask all the questions about values first and then set the appropriate frequencies and voltages.

```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
Wavegen.Channel1.checked = true

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"
}

freq_question = Tool.question("Frequency should be constant?")
amp_question = Tool.question("Generator voltage should be constant?")

frequencystep = 1
if (freq_question) {
frequency = Tool.getNumber("Frequency constant value is", 0, 0, 10000)
//conf(voltageconst, frequencyconst)
} else {
frequencyfrom = Tool.getNumber("Frequency range from [Hz]", 0, 0, 100)
frequencyto = Tool.getNumber("Frequency range to [Hz]", 0, 0, 100)
frequencystep= Tool.getNumber("Frequency number of steps", 0, 0, 100)
}

voltagestep = 1
if (amp_question) {
voltage = Tool.getNumber("Generator voltage constant value is", 0, 0, 10000)
//conf(voltageconst, frequencyconst)
} else {
voltagefrom = Tool.getNumber("Generator voltagerange from [mV]", 0, 0, 100)
voltageto = Tool.getNumber("Generator voltage range to [mV]", 0, 0, 100)
voltagestep = Tool.getNumber("Generator voltage number of steps", 0, 0, 100)
}

for(i = 1; i <= frequencystep; i++){
for(j = 1; j <= voltagestep; j++) {
if(typeof voltage !== 'undefined') {
voltage = (voltagefrom + (voltageto - voltagefrom)*j/voltagestep)
}
if(typeof frequency !== 'undefined') {
frequency = (frequencyfrom + (frequencyto - frequencyfrom)*i/frequencystep)
}
conf(voltage, frequency)
}
}```

The frequency (range and consvalue) are set correctly. However, the voltage does not all the time. How should I change my code?

I want the program written above to work according to the principle below. Or: 1. Amp = 0.005 and frequencies 2,4,6,8,10 and then 2. Amp = 0.01 and frequencies 2,4,6,8,10:

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

rgAmp.forEach(function(amp){
Wavegen.Channel1.Simple.Amplitude.value = amp
Wavegen.Channel1.checked = true

rgFreq.forEach(function(freq){
if(freq==0){
Wavegen.Channel1.stop()
}else{
Wavegen.Channel1.Simple.Frequency.value = freq
Wavegen.Channel1.run()
}

wait(0.1)
Scope.single()
if(!Scope.wait()) throw "stopped"
})
})
Wavegen.Channel1.stop()

```

Edited by Monika997
##### Share on other sites
• 0

Is this what you want ?

```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
Wavegen.Channel1.checked = true

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"
}

freq_question = Tool.question("Frequency should be constant?")
amp_question = Tool.question("Generator voltage should be constant?")

frequencystep = 1
if (freq_question) {
frequencyto = frequencyfrom = Tool.getNumber("Frequency constant value is", 0, 0, 10000)
//conf(voltageconst, frequencyconst)
} else {
frequencyfrom = Tool.getNumber("Frequency range from [Hz]", 0, 0, 1000)
frequencyto = Tool.getNumber("Frequency range to [Hz]", 0, 0, 1000)
frequencystep= Tool.getNumber("Frequency number of steps", 0, 0, 100)
}

voltagestep = 1
if (amp_question) {
voltageto = voltagefrom = Tool.getNumber("Generator voltage constant value is [mV]", 0, 0, 10000)
//conf(voltageconst, frequencyconst)
} else {
voltagefrom = Tool.getNumber("Generator voltagerange from [mV]", 0, 0, 1000)
voltageto = Tool.getNumber("Generator voltage range to [mV]", 0, 0, 1000)
voltagestep = Tool.getNumber("Generator voltage number of steps", 0, 0, 100)
}

for(i = 1; i <= frequencystep; i++){
for(j = 1; j <= voltagestep; j++) {
voltage = (voltagefrom + (voltageto - voltagefrom)*j/voltagestep)/1000 // mV to V
frequency = (frequencyfrom + (frequencyto - frequencyfrom)*i/frequencystep)

conf(voltage, frequency)
}
}```

## 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