LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

trend fitting - 'x' data

Hello all.

 

Trying to build a sampling vi with trend fitting.

vi. is attached (using express thing for now, I am worling on another vi using primitives but my knowledge there is not so good...

 

So, 

the issue I am having is that the dynamic data does not contain both 'x' and 'y' data, it only has 'y' data. the 'x' data is just in a header for the spacings.

I have added a loop to inject x values.

 

there must be a better way to do this? some advice would be appreciated.

 

I am also getting an error when I run the vi. - the system of equations cannot be solved because the input matrix is singular.

 

My third problem is that my non-linear coefficients array is only displaying 1 value when it should be displaying all three of the coefficients.

 

any help appreciated.

 
 
 

 

0 Kudos
Message 1 of 7
(1,987 Views)

the issue with the array size was just me being silly, that point is all good.

 

0 Kudos
Message 2 of 7
(1,960 Views)

Unfortunately, it is almost always a mistake for people who (a) don't know LabVIEW, (b) don't want to do some work to learn LabVIEW, and (c) don't have access to a mentor or LabVIEW Guru to whom to turn for advice to use Express VIs, particularly for acquiring and/or analyzing data.

 

That's the first problem.  Do you know the math behind curve-fitting?  Could you (by hand) fit a straight line through the points with X coordinates 1, 2, 3, 4, and 5, Y coordinates 1.5, 1.5, 2.5, 4.5, 5?  How would you solve it using pencil and paper?

 

LabVIEW is designed for Engineering "Experiments".  Fitting a straight line through equally-spaced points by the method of Least Squares (have you heard of this?) is simple enough that you should be able to (a) design a small piece of code that will generate N points that generates a straight line with a given slope and intercept and also incorporate "noise" (in the form of a random number being added to it) and in another loop, come up with the equation of the straight line that best fits these points.

 

Did you notice the two-part engineering approach?  You want to do something (Fit a straight line through some noisy data), you design an instrument to do this, and (here's the part that separates an Engineer from a "technician" you design and test your instrument to be sure it "meets the Design Specifications".

 

End of lecture.

 

Bob Schor

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

Hi Bob,

Thanks for answering... again.

 

I do know the math for the curve fitting and using the method of least squares. I have used excel matrix functions to manually calculate with some dummy data.

I think I need to go back to basics and practise more simple things and finish the core units...I am not sure where to start with writing the simple code to generate N points.

 

I think having a guru to aid the beginning of my labview journey would indeed be very helpful!!

 

Toby

0 Kudos
Message 4 of 7
(1,936 Views)

So here is the fun part. The fitting code is part of LabVIEW.

 

Fitting.png

Tim
GHSP
0 Kudos
Message 5 of 7
(1,929 Views)

Hi Tim,

 

Aha! ok!

I am using that curve fit in my vi, but it is missing the x values... should I be using one of those sub vi.'s? the "non linear curve fitting LM"?

 

Toby

0 Kudos
Message 6 of 7
(1,926 Views)

Describe the data you are trying to analyze.  What do you expect it to look like?  Often, in LabVIEW, the data are obtained from "instruments" that sample some physical quantity at a fixed sampling rate, so the X values all represent absolutely equal time steps, hence can be (in principle) replace by simple "order" numbers (first, second, ... or 1, 2, 3 ... or even 0, 1, 2, ...), and hence can even be eliminated.  We do need to know, in principle, the sampling interval (often called "dt") so we can return to "Real World" units.

 

For example, suppose I sample the voltage from the wall, which should be a 60 Hz sinusoid.  Let's say I take 1000 samples at 1 kHz.  What I expect to see is a sinusoidal waveform going up and down 60 times in the one second I sampled it.  If I knew it was "mainly" a 60 Hz sine wave, I could use least squares to fit a 60 Hz Sine and Cosine wave to the data and from the amplitudes of the Sine and Cosine fits, I could compute the actual amplitude of the Sine Wave (e.g. 120 V) and its "phase" at the time I measured it (did I start measuring near the peak of the waveform, near the trough, as it crossed zero going up, or what?).

 

So you want to know how to generate data.  Well, LabVIEW comes with Data Generators, but it's not a bad idea to know how to "Build It Yourself", especially when you are getting started.  Let's build a "noisy" linear ramp of 100 points that you can use to try various fitting routines.

 

All you really need is rule for "how to get to the next point", and how to handle "noise".  Let's make the rule "The next point is 0.5 larger than the previous point", and "noise" means we add a random quantity between -0.5 and +0.5 to the "ideal" (meaning noise-free) value.

 

So how to generate (an array of) 100 points?  Can you say "For Loop"?

 

The For loop has an index, "i", that "counts" the Loop.  We won't use it here (though we could), as I want to mention another useful thing, the Shift Register (you know about these, right)?  We'll use the Shift Register to hold the current (and next) "Ideal" point.  So put a Shift Register on your For Loop, and wire the initial Data Point (from the left outside of the loop) into it.

 

Our rule says "Output the current Ideal value plus some noise term".  We will use LabVIEW's Random Number Generator, which generates numbers uniformly distributed between 0 and 1.  Do some Magic and transform this to a number between -0.5 and +0.5 (which is what we want for the noiise) and add it to the current Ideal Value.  This is our first data point -- bring it to the right edge of the For Loop, where it creates an Indexing Tunnel (I assume you know the difference in both function and appearance between an Indexing Tunnel (the symbol [] appears inside it) and a "Last Point" tunnel (which is a solid color).  Now take the wire containing the current Ideal Value and do some more Magic to make it the next Ideal Value, which you then wire to the right-inside part of the Shift Register, where it will be ready for the next time through the loop.

 

Run your code.  The output will be your array of linear data + noise.  Connect it to a Waveform Graph (or Chart).  Does it look OK?

 

Now you have "known random data".  Go ahead and fit a straight line through it.  If you need an X array, you know the values of X -- they are simply 0, 1, ..., 99, the index of the For Loop that generated the data!

 

Play around with this simple generator.  Once you are comfortable with it, try generating a noisy sinusoid (try to generate an even number of cycles -- it makes the fitting "nicer").

 

Bob Schor

 

0 Kudos
Message 7 of 7
(1,902 Views)