How to perform interpolation using C/C++

Hello guys, I'm new to Arduino and if I apologize if I'm posting this under the wrong section.

Currently, I'm trying to convert my MATLAB written script to Arduino codes manually and in the script, I am using an in-built command called "interp1" to perform interpolation so that I get a fixed number of samples as my output everytime I call this command. However, Arduino doesn't have this in-built command and thus, I have to write the codes (Calculations) manually in terms of C/C++.

I tried to search online but could not find relevant formula/methods used to calculate non-linear interpolation and therefore, I'm posting this now to see if anyone implemented non-linear interpolation using Arduino before and could help me.

Basically in MATLAB, I have this SignalX which consist of some random number of elements (not fixed) and when I use this command to perform interpolation, my SignalX will now consist of 40 elements,

y = interp1(originalxaxis,SignalX,newxaxis);

where
originalxaxis is the number of samples that plots signalX
(The number of samples in signalX is different everytime) INPUT

newaxis is the new number of samples I want to get
(for my case, I want signalX to be plot using 40 samples) OUTPUT

So for example,
SignalX = [6 7 14 30 80 147 155 170 150 11 24 112 81 91 92]; 13 samples
and after interpolation,
SignalX = [6.5493 6.0820 6.0190 6.2813 6.7896 7.8833 10.5123 14.0000 18.3584 24.5474 33.6856 50.8722 72.7026 97.8516 128.7401 147.0000 150.7079 152.9526 156.6432 163.8043 169.4981 168.5142 161.4505 150.0000 100.8972 31.0796 11.2489 14.6683 21.2734 40.9356 86.1473 112.0000 102.1914 85.8438 81.4048 85.7727 90.3963 91.4119 91.8434 92.000] 40 samples

Regardless of how many samples as my input, what I want to achieve is to get 40 samples after performing interpolation (non-linear)

Sorry if my explanation above is poor,
Thank you

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Try looking for the "map" function, it is only integer.

Also this is the link to a float version of map that may help you.

Edit.. Sorry.. They are linear functions.

Tom... :slight_smile:

map() would be linear.

If you want non-linear, then start from the mathematical formula for the interpolation. Try to write that in C.

qsoontat:
Regardless of how many samples as my input, what I want to achieve is to get 40 samples after performing interpolation (non-linear)

You first need to find out how the Matlab function works so that you could work through the calculations with your calculator. For example, if you want to go from 13 to 40 data values does it produce 3 values for every existing value by interpolating between each pair of existing values? Or does it do something very much more complex that takes account of the shape of the whole series?

When you know that it should be straightforward to write the equivalent in C++.

And I can't avoid wondering WHY? All that seems to be happening is the creation of cosmetic values to improve the appearance of something.

...R

Have a look at my MultiMap()

Multimap behaves exactly like - 1-D data interpolation (table lookup) - MATLAB interp1 - MathWorks Benelux

That is linear interpolation between the known datapoints.

Robin2:
And I can't avoid wondering WHY? All that seems to be happening is the creation of cosmetic values to improve the appearance of something.

This is because I'm going to perform correlation with a template signal and that signal is made up of 40 elements/samples. To do correlation, I need both signals to be the same size and thus, my SignalX needs to have 40elements/samples.

I'm also looking into MATLAB but it only show what the command can achieve/do and not how it actually achieve/do

robtillaart:
Have a look at my MultiMap()

Multimap behaves exactly like - 1-D data interpolation (table lookup) - MATLAB interp1 - MathWorks Benelux

That is linear interpolation between the known datapoints.

Thank you, I will look into it for the time being.

There are many different ways to interpolate. To duplicate the MATLAB function, you need to know exactly what it does.

jremington:
There are many different ways to interpolate. To duplicate the MATLAB function, you need to know exactly what it does.

True but the default of interp1() equals multimap() as both do linear interpolation.

The OP used y = interp1(originalxaxis,SignalX,newxaxis); in his post which is the default linear mapping, so multimap should do the job.

The other methods are 'nearest', 'next', 'previous', 'spline','pchip', 'makima', or 'cubic'. Some are pretty easy to implement others need definitely more code / math.

MATLAB can only be approached in behavior as the floats of an Arduino are 32 bit and those from MATLAB are 64bit (internally even more).