LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Linearize zero Parts in 1D-Array

Solved!
Go to solution

I'm having trouble to find the correct solution. The problem is the following: I have a huge 1D-Array with around 80'000 Values. In this Array are double numbers and zeros, which I want to get rid off. The zeros are either several after another or only once zero followed by numbers again for example 0 0 12 0 14 0 0 0 22 0 12 and so on. 

 

If I find a zero or a row of zeros I want to linearize the zero parts with the existing numbers (12,14,22,12 in this example) and write it into the array so my example would change like this (or very similar at least): 4 8 12 13 14 16 18 20 22 17 12.

 

My idea was  to detect the zero or several, count how much there are (as x), get the Value before the zero-part starts, get the value when its finished (as y) and work with the "linear fit" function to get the values I want the zeros to replace with.

 

Since I'm rather new with LabVIEW this is a really difficult task for me. I already made a For-Loop that runs the Array through with autoindexing searching for zeros. With this method I see the indexes in the array where I have to replace the zeros but I'm really stuck on how to continue from this point.

I would have to split this array somehow or maybe there is a way to work with it?

 

I would be really thankful for some smart ideas or good hints to solve this problem. I already have a version that replaces the zero with the last value before the zeros appeared.

 

0 Kudos
Message 1 of 6
(2,018 Views)

Hi Shurican,

 

you can also use the Ramp function after you found the values before and after those zero(es), it accepts start, end and number of values as inputs…

Best regards,
GerdW


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

It seems you are looking for a linear interpolation. SHould be easy. 

0 Kudos
Message 3 of 6
(1,997 Views)
Solution
Accepted by topic author Shurican

@altenbach wrote:

Should be easy. 


Here's simple code that does it (There might be more complicated, but more efficient versions, but try this first).

 

Intrerpolzero.png

0 Kudos
Message 4 of 6
(1,986 Views)

Depending on where the data comes from, additional tweaks are needed, e.g.

 

  • decide what to do if the array starts or ends in zero
  • if NaN is a possibility, replace those too by interpolation
  • What do do with Inf, -Inf?
  • equal comparisons with DBL are potentially dangerous, because your "zero" could be 1e-100, depending on where it came from. You might decide what "close enough to zero" really means.
  • etc.
0 Kudos
Message 5 of 6
(1,976 Views)

I wanna thank everybody for the fast answers and inputs. I already manipulated the data that there aren't any NaN, Inf, -Inf or values really close to zero. So I'm sorry that I wasn't specific enough.

 

I was exactly looking for a linear interpolation and did not know that this function exists, instead I tried to program it by hand which was quite tricky for me. It works perfectly fine and I was able to implement it in my program. So thanks a lot again

0 Kudos
Message 6 of 6
(1,962 Views)