LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Improving speed of Mathscript for calculations in Fourier space

Hi Everyone,

 

Disclaimer: I am a physicist, not a programmer, please try not to cringe at my code....

 

 

I am trying to carry out some functions in Fourier space for very large arrays.

 

The way I have done this is to first use the LabVIEW FFT function, then iterate over each frequency component using two for loops, using Mathscript to do the calculations I need within these loops. The index of each loop is critical to my calculations (as it represents the spatial frequency component).

 

It works, however it is very slow (since I need to loop this entire process several thousand times for different data). I have read on the forums that using Mathscript within a for loop is not ideal, that it is better to use arrays in the Mathscript window. However, I am not familiar with this, and can see problems since I currently work with the indices of each loop.

 

I have attached the appropriate segment of my code (as an image, since I had trouble uploading the actual code). The calculations in the Mathscript window are critical, while the other math window is simply to set kx, ky = 0 in the middle of the array rather than at the corner, while avoiding dividing by zero. The "true" case for k>kmax is not shown, it simply outputs 0+0i to all three tunnels.

 

 

Thanks to anyone who can help!
Steve

 

P.S. for anyone interested, what I am doing is calculating the current and magnetisation at each point in a two-dimensional superconductor by measuring the magnetic field at each point in a film above it.

0 Kudos
Message 1 of 7
(3,477 Views)

Hi bob,

 

It works, however it is very slow

Yes, especially as you use a formula node AND a mathscript node…

 

Do all calculations in pure G! Employ polymorphism: LabVIEW can do most of the calculations on arrays as it does on scalars!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 7
(3,430 Views)

Can you attach the actual code instead of a picture? Also include some typical default data.

 

You are just doing plain calculations, calling the mathscript node one point at a time in the innermost loop.

How big are the 2D arrays?

 

I agree with Gerd.  It looks to me that rewriting this all in plain LabVIEW would be quite trivial and it will be much faster.

 

One minor point: The control for "kmax" belongs outside the loops. There is no need to re-read the control with every interation of the innermost loop. The code only make sense if kmax is constant for the duration of the loops. 

0 Kudos
Message 3 of 7
(3,419 Views)

 

0 Kudos
Message 4 of 7
(3,371 Views)

Try a different browser or zip the files up before attaching.

0 Kudos
Message 5 of 7
(3,365 Views)

I do not know the size of your arrays, but you can get sometimes pretty nice speed improvement, if you do the different FFT calculations using a video card. Few years ago I had to do FFT operations on large data sets (gigabytes of single floats, magnetic Barkhausen-noise data), and I managed to get nice speed using CUDA language and doing FFT in parallel way on the multicore videocard.

LabVIEW has also a toolkit, so you may have a look:

 

NI LabVIEW GPU Analysis Toolkit

0 Kudos
Message 6 of 7
(3,346 Views)

Ok, I am attaching the actual code now.

 

I have included an array of typical data as a defualt value for "field values".

 

I also moved kmax outside of the loops, but was less confident about making any of the other suggested changes...

0 Kudos
Message 7 of 7
(3,329 Views)