LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Interpolation in C or C++

Hi,

I 'm currently working with a NI 488.2 PCI-GPIB board.

I need to adapt a measure for one instrument to another.

Imagine I have an oscilloscope connected to my PC, I send a command which order the instrument to answer me differents points(samples) offa the curb.
If I receive those 1000 samples and I need to convert the message with only 800 samples for an old instrument, how can I do that?

Is this operation called Interpolation (mathmatics)?
Is there any kind of function in CVI or other libraries to do so?

Thanks in advance dor your help.

PS : sorry for my bad English.
0 Kudos
Message 1 of 10
(8,140 Views)

As far as I can understand, your situation is as follows:

1. Your instrument returns you a set of data that describe a curve
2. You want the same (or very similar) curve described with fewer points

The first obvious thing is that reducing the number of points will result in losing some detail in the curve and possibly masking some very fast spike your signal can have (and perhaps you would like to maintain...), so you'll have to consider if doing so you are sure not to alter your measure in a radical way.

Supposing the answer is not, what you could do could be to determine an interpolant of original data (using PolyFit or other functions inside Advanced Analisys >> Curve fitting library) and then use the coefficients to generate a new curve with fewer points (use Ramp to generate a new X-axis array and PolyEv1D to generate new data based on new X-axis - this method works for fitting functions that return an array of coefficients that describe the interpolant).

Alternatively you could use the PolyInterp function in a loop (Advanced Analisys >> Interpolation library) appropriately choosing every new x value and the correct subset of data to interpolate to generate the new y value.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 10
(8,138 Views)
Hi,

thank you for your help.

Just some other questions :

The message I received on my GPIB board is only made of amplitude values. So I haven't got any axis values. Will it cause problems for interpolation functions?

I tried a simple example with f(x)= x ² , so I built 2 arrays

x which contains 100 points from 0 to 99
y which contains 100 points from 0 to 9801

I tried getting values from PolyInterp with a new x value  : it works for under certain conditions, for example if i try 2.33 as a new x value, it has a big error value so y value is not correct.

I also tried Rational Interpolation and it works. Will it work in any case?

Maybe, I should use PolyFit to obtain coefficient  and etc... I'll try later.

Thanks in advance.
0 Kudos
Message 3 of 10
(8,121 Views)


NI_Device_user_sb wrote:
The message I received on my GPIB board is only made of amplitude values. So I haven't got any axis values. Will it cause problems for interpolation functions?

The data array is made only of amplitute possibly because delta time is a constant (that is: samples are taken at constant rate). In this case, knowing the dx you can create a x-axis array with Ramp or a simple loop. Interpolation functions like PolyFit will need an actual x array before they can calculate the interpolant.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 10
(8,110 Views)
Hi,

I've just tried something. I join my source code to have your opinion.

If you have any solution to print easily a curb in a graph...in order to make comparisons...



#include <ansi_c.h>
#include <analysis.h>

#define NB_POINTS 100
#define NEW_NB_POINTS 50


void main (void)
{
    int i,
        order = 3;
   
    double x[NB_POINTS],
           y[NB_POINTS],
           dx = 0.01,
           error,
           * Coefficients,
           OutputArray[NB_POINTS],
           Result[NEW_NB_POINTS],
           newx[NEW_NB_POINTS];
          
   
    Coefficients = calloc(order+1,sizeof(double));
   
   
    //Creating initial x values
    for(i=0;i<NB_POINTS;i++) x[i]=i*dx;
   
    //Creating initial y values
    for(i=0;i<NB_POINTS;i++) y[i]=x[i]*x[i];
   
    //Creating a new x array
    Ramp (50, 0.0,(NB_POINTS - 1)*dx, newx);
   
    //Caculation of coefficients
    PolyFit(x,y,NB_POINTS,order,OutputArray,Coefficients, &error);
   
    //Generating new y values based on new x values
    PolyEv1D (newx, NEW_NB_POINTS, Coefficients, order+1, Result);
   
    free(Coefficients);
}


Message Edité par NI_Device_user_sb le 06-05-2008 03:54 AM
0 Kudos
Message 5 of 10
(8,069 Views)
Your code seems good to me: are you getting reasonable results?
You could plot both of your signals (before and after interpolation) on the diagram with PlotWaveForm (or PlotXY plotting against the corresponding simulated x array) so that you can immediately compare algorithm results.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 10
(8,053 Views)
Hi,

it works well for simple type of curbs.

I've tried with a Sine wave and it works (polynomial)

Now, I've to think about kind of curbs which cannot work, like Pulse signals. Result is'nt good cause of approximation on rising and falling edges.

Maybe a Gaussian Fit...I'll see.

Thanks a lot for your help and if you've any idea for method...I would be thankful.
0 Kudos
Message 7 of 10
(8,049 Views)
Hi,

it's me again Smiley Happy

I've tried Spline Cubic interpolation method and it works fine. But if my input signal is made of pulses I have few samples which are not well placed ; Like noise on falling and raising edges.

Maybe I have to filter the output signal??


Someone told me to try the Lanczos algorithm.(cubic method is faster than Lanczos' as I've read)
I've found this very useful link : here

There is a C++ example. It's fine for my final project which is in C++, but I need to test it under CVI, so how to convert it in C language.
I've got some ideas but the example shows normalized signals, will it cause problems for my resulting signal?

Thanks in advance for your help.


Message Edité par NI_Device_user_sb le 06-09-2008 03:26 AM
0 Kudos
Message 8 of 10
(7,993 Views)
Hi,

me again.

I've found another problem.

I tried Cubic Spline Interpolation with an amplitude spectrum signal.

I'm joining 2 capture files :
green signal is the original signal
red and blue signals are interplations results

Do you have any idea if Lanczos algorithm can give better results than Spline cubic?

Is there any other solution to resample a signal ?
Download All
0 Kudos
Message 9 of 10
(7,887 Views)
Hi,

maybe a simple other problem.

What I was doing is a spline interpolation. When doing interpolation, I used the function Ramp()
It generates my new x array based on a linear repartition. The problem is that I can also retrieve data which are based on a logarithmic x axis scale.

So, I think I'm introducing an error.

What do you think about this problem?
Do you see any simple solution to generate the x axis on a log scale?

Thanks in advance for your help.


0 Kudos
Message 10 of 10
(7,533 Views)