Jump to content
  • 0

Math function in "Wavegen_Custom"


jfdo

Question

Hi All,

I would like to know whether I can include if, else conditional statements in the Waveform generator (in Analog Discovery) in "Custom"  and math tab.

If I select Custom to generate a waveform, in the script editor, Can I use the variable "X" defined in the Math tab  (in the "X from  0 to  1.5" fields) ?

I just want to generate waveform with  "if", "else" conditional statements.

Example: I wish to have the Sin wave when the X (X goes from 0 to 1.5) variable is between 0.5 and 1 and zero output  outside the specified range.

if ( X < 0.5){

Y = 0;}

else if (0.5 <= X <= 1 ){

Y = 0.5*sin(62*PI*X); }

else { Y = 0;}

Please see the attached.

Any suggestions!

I have scripts written in Python. How I can import Python Scripts to generate custom waveforms in Waveform generator?.

Thank you in adavance.

Fernando

 

 

 

Script_Wavegen.jpg

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

Hi @jfdo

You can use the following conditional statement:
(condition)? (true) : (false)
(X<0.5||X>1) ? 0 : 0.5*sin(62*PI*X)

image.png.827d54e1fdba91d5495516db43b985e4.png

 

The custom buffer size is limited, by default 4k up to 16k in Analog Discovery. With the above example a sine period will be generated using 88 samples, 4096/31/1.5 increasing the THD.
To have better quality signal you should use Wait-Run options, like:
image.png.1cf324a1b8ce129c6ae121b021917166.png

Link to comment
Share on other sites

57 minutes ago, attila said:

Hi Attila,

Hi Attila,

Many Thanks for the quick reply. 

What is THD stand for?

Could you please direct me to the place where I can find the syntax  to write math functions as scripts, as you have done in the example?

Also can I import Python files to generate waveforms?

Thank you.

Best Regards

Fernando

57 minutes ago, attila said:

 

Hi @jfdo

You can use the following conditional statement:
(condition)? (true) : (false)
(X<0.5||X>1) ? 0 : 0.5*sin(62*PI*X)

image.png.827d54e1fdba91d5495516db43b985e4.png

 

The custom buffer size is limited, by default 4k up to 16k in Analog Discovery. With the above example a sine period will be generated using 88 samples, 4096/31/1.5 increasing the THD.
To have better quality signal you should use Wait-Run options, like:
image.png.1cf324a1b8ce129c6ae121b021917166.png

 

Link to comment
Share on other sites

The Script and other coding fields in the WF application use JavaScript, based on the ECMAScript, ECMA-262 standard.

Under the Insert group you can find the frequently used operations, functions, constants and special variables for the context, like X in custom waveform.
image.png.da28c0e0f6ddd60df9b410aeb0dc3179.png 

 

Python is not supported in the application.
Eventually you could call Python from the application Script interface to save a pattern to a file, then read it and apply it for Wavegen custom pattern...

 

In case the signal is composed of little amount of samples you will start to see the steps, distorting the sine.

image.thumb.png.78da24acf18df946a1a89f988d9dcc8d.png

image.thumb.png.4173f4a26b738c4098928ab577c90ecf.png

Link to comment
Share on other sites

3 hours ago, attila said:

Hi,

By the way, what are the syntax for

if, else if, else conditions?

thanks

 

3 hours ago, attila said:

 

The Script and other coding fields in the WF application use JavaScript, based on the ECMAScript, ECMA-262 standard.

Under the Insert group you can find the frequently used operations, functions, constants and special variables for the context, like X in custom waveform.
image.png.da28c0e0f6ddd60df9b410aeb0dc3179.png 

 

Python is not supported in the application.
Eventually you could call Python from the application Script interface to save a pattern to a file, then read it and apply it for Wavegen custom pattern...

 

In case the signal is composed of little amount of samples you will start to see the steps, distorting the sine.

image.thumb.png.78da24acf18df946a1a89f988d9dcc8d.png

image.thumb.png.4173f4a26b738c4098928ab577c90ecf.png

 

Link to comment
Share on other sites

6 hours ago, attila said:

Hi @jfdo

The conditional statement can be cascaded:
condition1 ? true : (condition2 ? true : false )
(X<0.5||X>1) ? 0 : 0.5*sin(62*PI*X)
X<0.5? 0 : X>1 ? 0 0.5*sin(62*PI*X) 

Thank you.

Cheers

Fernando

Link to comment
Share on other sites

11 hours ago, jfdo said:

Thank you.

Cheers

Fernando

Hi,

I tried the following but it didn't output anything. Perhaps the syntax is not right!

Any suggestions?

( X < 0.25)? 0:

(0.25 <= X <= 0.5 )? 0.5*sin(2*30*PI*X):

( 0.5 < X)? ( 0.5*sin(2*10*PI*X)+0.5*sin(2*10*PI*X) )

Link to comment
Share on other sites

 

6 hours ago, jfdo said:

Hi,

I tried the following but it didn't output anything. Perhaps the syntax is not right!

Any suggestions?

( X < 0.25)? 0:

(0.25 <= X <= 0.5 )? 0.5*sin(2*30*PI*X):

( 0.5 < X)? ( 0.5*sin(2*10*PI*X)+0.5*sin(2*10*PI*X) )

It is missing the last false statement, you have: cond1? true : cond2? true : cond3 ? true : false
I'm not sure if you even need it... You may wanted: 
X<0.25? 0 : X<=0.5? 0.5*sin(2*30*PI*X): 0.5*sin(2*10*PI*X)+0.5*sin(2*10*PI*X)

image.png.835f6c1e94f5c89ec92e7b956d5b72fb.png

 

For better signal quality you should use AM and FM like this, which is synthesized on the device.

image.thumb.png.5f021fde7ee59f72b5382f5cc83b5d57.png

image.png.62f5b760bfe21468d486c9de7a081388.png

Link to comment
Share on other sites

On 7/3/2019 at 3:43 AM, attila said:

It is missing the last false statement, you have: cond1? true : cond2? true : cond3 ? true : false
I'm not sure if you even need it... You may wanted: 
X<0.25? 0 : X<=0.5? 0.5*sin(2*30*PI*X): 0.5*sin(2*10*PI*X)+0.5*sin(2*10*PI*X)

image.png.835f6c1e94f5c89ec92e7b956d5b72fb.png

 

For better signal quality you should use AM and FM like this, which is synthesized on the device.

image.thumb.png.5f021fde7ee59f72b5382f5cc83b5d57.png

image.png.62f5b760bfe21468d486c9de7a081388.png

Many Thanks Attila for the clarifications.

That was helpful.

Cheers

Fernando

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...