使用STM32-ARM_MATH库使用线性插值算法

1、目的

      在ADC采集数据的时候,我们采集回来的数据都是单个单个的数据。但是有时又我们需要将数据拟合成一条曲线,计算曲线内部的值。例如采集数据为x1,x2,但是我们需要求得值在x1,x2之间的一个值,我们设该值为x。那么我们应该如何去求解?

2、硬件及软件

STM32F103ZET6、keil、matlab

3、思路

通过使用arm_math库,下面的这个函数,实现函数构建。该函数说明地址:Linear Interpolation (keil.com)

其中s源数据设置为sin曲线的数据。通过采集特定的点,构成sin函数,然后通过插值函数计算里面的值,并将该值与sin的值进行比较。

4、具体原理说明

5、实验过程

在实验过程中,参考该文件历程进行编写,并对历程进行一部修改。结果如下

/** \example arm_linear_interp_example_f32.c
  */

#include "arm_math.h"
#include "math_helper.h"

#define TEST_LENGTH_SAMPLES     10
#define XSPACING               (0.005f)

/* ----------------------------------------------------------------------
* Test input data for F32 SIN function
* Generated by the MATLAB rand() function
* randn('state', 0)
* xi = (((1/4.18318581819710)* randn(blockSize, 1) * 2* pi));
* 相当于x值
* --------------------------------------------------------------------*/
float32_t testInputSin_f32[TEST_LENGTH_SAMPLES] =
{
    0.649716504673081170,  2.501723745497831200,
    0.188250329003310100,  0.432092748487532540,
    1.722010988459680800,  1.788766476323060600,
    1.786136060975809500,  0.056525543169408797,
    5.491596272728153760,  4.262309671126153390
};

/*------------------------------------------------------------------------------
*  Method 1: Test out Buffer Calculated from Cubic Interpolation
*------------------------------------------------------------------------------*/
float32_t testOutput[TEST_LENGTH_SAMPLES];

/*------------------------------------------------------------------------------
*  Method 2: Test out buffer Calculated from Linear Interpolation
*------------------------------------------------------------------------------*/
float32_t testLinIntOutput[TEST_LENGTH_SAMPLES];

/*------------------------------------------------------------------------------
*  External table used for linear interpolation
*------------------------------------------------------------------------------*/
extern float arm_linear_interep_table[1256];

/* ----------------------------------------------------------------------------
* Calculation of Sine values from Cubic Interpolation and Linear interpolation
* ---------------------------------------------------------------------------- */
int32_t linear(void)
{
  uint32_t i;

  arm_linear_interp_instance_f32 S = {1256, 0, XSPACING, &arm_linear_interep_table[0]};

  /*------------------------------------------------------------------------------
  *  Method 1: Test out Calculated from Cubic Interpolation
  *------------------------------------------------------------------------------*/
  for(i=0; i< TEST_LENGTH_SAMPLES; i++)
  {
    testOutput[i] = arm_sin_f32(testInputSin_f32[i]);
  }

  /*------------------------------------------------------------------------------
  *  Method 2: Test out Calculated from Cubic Interpolation and Linear interpolation
  *------------------------------------------------------------------------------*/

  for(i=0; i< TEST_LENGTH_SAMPLES; i++)
  {
      testLinIntOutput[i] = arm_linear_interp_f32(&S, testInputSin_f32[i]);
  }

  while (1);   /* main function does not return */
}

 /** \endlink */

接下来是对数据源的修改。

/* ---------------------------------------------------------------------- 
* Table generated from following MATLAB Command
* x = 0: 0.005 : (2*pi - 0.00005);
* y = sin(x);
* where pi value is 3.141592653589793
* --------------------------------------------------------------------*/

float arm_linear_interep_table[1256] =  {

  0,0.005,0.0099998,0.014999,0.019999,0.024997,0.029996,0.034993,0.039989,0.044985,0.049979,0.054972,
  0.059964,0.064954,0.069943,0.07493,0.079915,0.084898,0.089879,0.094857,0.099833,0.10481,0.10978,0.11475,
	0.11971,0.12467,0.12963,0.13459,0.13954,0.14449,0.14944,0.15438,0.15932,0.16425,0.16918,0.17411,0.17903,
	0.18395,0.18886,0.19377,0.19867,0.20357,0.20846,0.21335,0.21823,0.22311,0.22798,0.23284,0.2377,0.24256,0.2474,
	0.25225,0.25708,0.26191,0.26673,0.27155,0.27636,0.28116,0.28595,0.29074,0.29552,0.30029,0.30506,0.30982,0.31457,
	0.31931,0.32404,0.32877,0.33349,0.3382,0.3429,0.34759,0.35227,0.35695,0.36162,0.36627,0.37092,0.37556,0.38019,
	0.38481,0.38942,0.39402,0.39861,0.40319,0.40776,0.41232,0.41687,0.42141,0.42594,0.43046,0.43497,0.43946,0.44395,
	0.44842,0.45289,0.45734,0.46178,0.46621,0.47063,0.47503,0.47943,0.48381,0.48818,0.49253,0.49688,0.50121,0.50553,
	0.50984,0.51414,0.51842,0.52269,0.52694,0.53119,0.53542,0.53963,0.54383,0.54802,0.5522,0.55636,0.56051,0.56464,
	0.56876,0.57287,0.57696,0.58104,0.5851,0.58914,0.59318,0.5972,0.6012,0.60519,0.60916,0.61312,0.61706,0.62099,
	0.6249,0.62879,0.63267,0.63654,0.64039,0.64422,0.64803,0.65183,0.65562,0.65938,0.66314,0.66687,0.67059,0.67429,
	0.67797,0.68164,0.68529,0.68892,0.69254,0.69614,0.69972,0.70328,0.70683,0.71035,0.71386,0.71736,0.72083,0.72429,
	0.72773,0.73115,0.73455,0.73793,0.7413,0.74464,0.74797,0.75128,0.75457,0.75784,0.7611,0.76433,0.76754,0.77074,
	0.77391,0.77707,0.78021,0.78333,0.78643,0.7895,0.79256,0.7956,0.79862,0.80162,0.8046,0.80756,0.8105,0.81342,
	0.81631,0.81919,0.82205,0.82489,0.8277,0.8305,0.83327,0.83603,0.83876,0.84147,0.84416,0.84683,0.84948,0.85211,
	0.85471,0.8573,0.85986,0.8624,0.86492,0.86742,0.8699,0.87236,0.87479,0.8772,0.87959,0.88196,0.8843,0.88663,
	0.88893,0.89121,0.89346,0.8957,0.89791,0.9001,0.90227,0.90441,0.90653,0.90863,0.91071,0.91276,0.91479,0.9168,
	0.91879,0.92075,0.92269,0.92461,0.9265,0.92837,0.93022,0.93204,0.93384,0.93562,0.93737,0.9391,0.94081,0.94249,
	0.94415,0.94578,0.9474,0.94898,0.95055,0.95209,0.95361,0.9551,0.95657,0.95802,0.95944,0.96084,0.96221,0.96356,
	0.96488,0.96618,0.96746,0.96872,0.96994,0.97115,0.97233,0.97348,0.97462,0.97572,0.97681,0.97786,0.9789,0.97991,
	0.98089,0.98185,0.98279,0.9837,0.98459,0.98545,0.98629,0.9871,0.98789,0.98865,0.98939,0.9901,0.99079,0.99146,
	0.9921,0.99271,0.9933,0.99387,0.99441,0.99492,0.99542,0.99588,0.99632,0.99674,0.99713,0.99749,0.99784,0.99815,
	0.99844,0.99871,0.99895,0.99917,0.99936,0.99953,0.99967,0.99978,0.99988,0.99994,0.99998,1,0.99999,0.99996,0.9999,
	0.99982,0.99971,0.99957,0.99942,0.99923,0.99902,0.99879,0.99853,0.99825,0.99794,0.99761,0.99725,0.99687,0.99646,
	0.99602,0.99557,0.99508,0.99458,0.99404,0.99349,0.9929,0.9923,0.99166,0.99101,0.99033,0.98962,0.98889,0.98813,
	0.98735,0.98655,0.98572,0.98486,0.98399,0.98308,0.98215,0.9812,0.98022,0.97922,0.9782,0.97715,0.97607,0.97497,
	0.97385,0.9727,0.97153,0.97033,0.96911,0.96786,0.96659,0.9653,0.96398,0.96264,0.96128,0.95989,0.95847,0.95703,
	0.95557,0.95409,0.95258,0.95104,0.94949,0.9479,0.9463,0.94467,0.94302,0.94134,0.93965,0.93792,0.93618,0.93441,
	0.93262,0.9308,0.92896,0.9271,0.92521,0.9233,0.92137,0.91942,0.91744,0.91544,0.91341,0.91137,0.9093,0.90721,
	0.90509,0.90295,0.90079,0.89861,0.89641,0.89418,0.89193,0.88966,0.88736,0.88505,0.88271,0.88035,0.87796,0.87556,
	0.87313,0.87068,0.86821,0.86572,0.86321,0.86067,0.85812,0.85554,0.85294,0.85032,0.84768,0.84501,0.84233,0.83963,
	0.8369,0.83415,0.83138,0.82859,0.82578,0.82295,0.8201,0.81723,0.81434,0.81143,0.8085,0.80554,0.80257,0.79958,
	0.79657,0.79353,0.79048,0.78741,0.78432,0.7812,0.77807,0.77492,0.77175,0.76856,0.76535,0.76213,0.75888,0.75562,
	0.75233,0.74903,0.74571,0.74236,0.73901,0.73563,0.73223,0.72882,0.72538,0.72193,0.71846,0.71498,0.71147,0.70795,
	0.70441,0.70085,0.69728,0.69369,0.69007,0.68645,0.6828,0.67914,0.67546,0.67177,0.66806,0.66433,0.66058,0.65682,
	0.65304,0.64925,0.64543,0.64161,0.63776,0.63391,0.63003,0.62614,0.62223,0.61831,0.61437,0.61042,0.60645,0.60247,
	0.59847,0.59446,0.59043,0.58639,0.58233,0.57826,0.57417,0.57007,0.56596,0.56183,0.55768,0.55353,0.54936,0.54517,
	0.54097,0.53676,0.53253,0.5283,0.52404,0.51978,0.5155,0.51121,0.50691,0.50259,0.49826,0.49392,0.48957,0.4852,
	0.48082,0.47643,0.47203,0.46762,0.46319,0.45875,0.45431,0.44985,0.44537,0.44089,0.4364,0.43189,0.42738,0.42285,
	0.41832,0.41377,0.40921,0.40465,0.40007,0.39548,0.39088,0.38628,0.38166,0.37703,0.3724,0.36775,0.3631,0.35844,
	0.35376,0.34908,0.34439,0.3397,0.33499,0.33027,0.32555,0.32082,0.31608,0.31133,0.30657,0.30181,0.29704,0.29226,
	0.28748,0.28269,0.27789,0.27308,0.26827,0.26345,0.25862,0.25379,0.24895,0.2441,0.23925,0.23439,0.22953,0.22466,
	0.21978,0.2149,0.21002,0.20513,0.20023,0.19533,0.19042,0.18551,0.1806,0.17568,0.17075,0.16582,0.16089,0.15595,
	0.15101,0.14607,0.14112,0.13617,0.13121,0.12625,0.12129,0.11633,0.11136,0.10639,0.10142,0.096443,0.091465,
	0.086484,0.081502,0.076518,0.071532,0.066543,0.061554,0.056562,0.05157,0.046576,0.041581,0.036584,0.031587,
	0.02659,0.021591,0.016592,0.011592,0.0065926,0.0015927,-0.0034073,-0.0084072,-0.013407,-0.018406,-0.023405,
	-0.028404,-0.033401,-0.038398,-0.043394,-0.048388,-0.053382,-0.058374,-0.063365,-0.068354,-0.073341,-0.078327
,-0.083311,-0.088292,-0.093272,-0.098249,-0.10322,-0.1082,-0.11316,-0.11813,-0.12309,-0.12805,-0.13301,-0.13797,
-0.14292,-0.14786,-0.15281,-0.15775,-0.16268,-0.16761,-0.17254,-0.17746,-0.18238,-0.18729,-0.1922,-0.19711,
-0.20201,-0.2069,-0.21179,-0.21668,-0.22155,-0.22643,-0.23129,-0.23616,-0.24101,-0.24586,-0.2507,-0.25554,
-0.26037,-0.2652,-0.27001,-0.27482,-0.27963,-0.28443,-0.28922,-0.294,-0.29877,-0.30354,-0.3083,-0.31305,
-0.3178,-0.32254,-0.32726,-0.33199,-0.3367,-0.3414,-0.3461,-0.35078,-0.35546,-0.36013,-0.36479,-0.36944,
-0.37408,-0.37871,-0.38334,-0.38795,-0.39255,-0.39715,-0.40173,-0.40631,-0.41087,-0.41542,-0.41997,-0.4245,
-0.42902,-0.43353,-0.43803,-0.44252,-0.447,-0.45147,-0.45592,-0.46037,-0.4648,-0.46922,-0.47363,-0.47803,
-0.48241,-0.48679,-0.49115,-0.4955,-0.49983,-0.50416,-0.50847,-0.51277,-0.51706,-0.52133,-0.52559,-0.52984,
-0.53407,-0.53829,-0.5425,-0.54669,-0.55087,-0.55504,-0.55919,-0.56333,-0.56745,-0.57156,-0.57566,-0.57974,
-0.5838,-0.58786,-0.59189,-0.59592,-0.59992,-0.60392,-0.6079,-0.61186,-0.61581,-0.61974,-0.62365,-0.62755,
-0.63144,-0.63531,-0.63916,-0.643,-0.64682,-0.65063,-0.65441,-0.65819,-0.66194,-0.66568,-0.6694,-0.67311,
-0.6768,-0.68047,-0.68413,-0.68777,-0.69139,-0.69499,-0.69858,-0.70215,-0.7057,-0.70923,-0.71275,-0.71625,
-0.71973,-0.72319,-0.72663,-0.73006,-0.73347,-0.73686,-0.74023,-0.74358,-0.74691,-0.75023,-0.75352,-0.7568,
-0.76006,-0.7633,-0.76652,-0.76972,-0.77291,-0.77607,-0.77921,-0.78234,-0.78544,-0.78853,-0.79159,-0.79464,
-0.79766,-0.80067,-0.80365,-0.80662,-0.80956,-0.81249,-0.81539,-0.81828,-0.82114,-0.82398,-0.82681,-0.82961,
-0.83239,-0.83515,-0.83789,-0.84061,-0.84331,-0.84598,-0.84864,-0.85127,-0.85389,-0.85648,-0.85905,-0.8616,
-0.86412,-0.86663,-0.86911,-0.87158,-0.87402,-0.87643,-0.87883,-0.88121,-0.88356,-0.88589,-0.8882,-0.89048,
-0.89275,-0.89499,-0.89721,-0.89941,-0.90158,-0.90373,-0.90586,-0.90797,-0.91005,-0.91211,-0.91415,-0.91617,
-0.91816,-0.92013,-0.92207,-0.924,-0.9259,-0.92778,-0.92963,-0.93146,-0.93327,-0.93505,-0.93681,-0.93855,-0.94026,
-0.94196,-0.94362,-0.94527,-0.94689,-0.94848,-0.95005,-0.9516,-0.95313,-0.95463,-0.9561,-0.95756,-0.95899,
-0.96039,-0.96177,-0.96313,-0.96446,-0.96577,-0.96706,-0.96832,-0.96956,-0.97077,-0.97196,-0.97312,-0.97426,
-0.97537,-0.97646,-0.97753,-0.97857,-0.97959,-0.98058,-0.98155,-0.98249,-0.98341,-0.98431,-0.98518,-0.98602,
-0.98684,-0.98764,-0.98841,-0.98916,-0.98988,-0.99058,-0.99125,-0.9919,-0.99252,-0.99312,-0.99369,-0.99424,
-0.99476,-0.99526,-0.99574,-0.99618,-0.99661,-0.99701,-0.99738,-0.99773,-0.99805,-0.99835,-0.99863,-0.99888,
-0.9991,-0.9993,-0.99948,-0.99962,-0.99975,-0.99985,-0.99992,-0.99997,-1,-1,-0.99997,-0.99992,-0.99984,
-0.99974,-0.99962,-0.99947,-0.99929,-0.99909,-0.99887,-0.99862,-0.99834,-0.99804,-0.99772,-0.99736,-0.99699,
-0.99659,-0.99616,-0.99571,-0.99524,-0.99474,-0.99422,-0.99367,-0.99309,-0.99249,-0.99187,-0.99122,-0.99055,
-0.98985,-0.98913,-0.98838,-0.98761,-0.98681,-0.98599,-0.98514,-0.98427,-0.98337,-0.98245,-0.98151,-0.98054,
-0.97954,-0.97853,-0.97748,-0.97642,-0.97532,-0.97421,-0.97307,-0.9719,-0.97071,-0.9695,-0.96826,-0.967,
-0.96572,-0.96441,-0.96307,-0.96171,-0.96033,-0.95892,-0.95749,-0.95604,-0.95456,-0.95306,-0.95153,-0.94998,
-0.94841,-0.94681,-0.94519,-0.94355,-0.94188,-0.94019,-0.93847,-0.93674,-0.93497,-0.93319,-0.93138,-0.92955,
-0.92769,-0.92581,-0.92391,-0.92199,-0.92004,-0.91807,-0.91608,-0.91406,-0.91202,-0.90996,-0.90787,-0.90577,
-0.90364,-0.90148,-0.89931,-0.89711,-0.89489,-0.89265,-0.89038,-0.8881,-0.88579,-0.88345,-0.8811,-0.87873,
-0.87633,-0.87391,-0.87147,-0.869,-0.86652,-0.86401,-0.86148,-0.85893,-0.85636,-0.85377,-0.85116,-0.84852,
-0.84587,-0.84319,-0.84049,-0.83777,-0.83503,-0.83227,-0.82949,-0.82668,-0.82386,-0.82101,-0.81815,-0.81526,
-0.81236,-0.80943,-0.80649,-0.80352,-0.80053,-0.79753,-0.7945,-0.79145,-0.78839,-0.7853,-0.7822,-0.77907,
-0.77593,-0.77276,-0.76958,-0.76638,-0.76316,-0.75992,-0.75666,-0.75338,-0.75008,-0.74677,-0.74343,-0.74008,
-0.73671,-0.73332,-0.72991,-0.72648,-0.72303,-0.71957,-0.71609,-0.71259,-0.70907,-0.70554,-0.70199,-0.69842,
-0.69483,-0.69123,-0.6876,-0.68397,-0.68031,-0.67664,-0.67295,-0.66924,-0.66552,-0.66178,-0.65802,-0.65425,
-0.65046,-0.64665,-0.64283,-0.63899,-0.63514,-0.63127,-0.62738,-0.62348,-0.61956,-0.61563,-0.61168,-0.60772,
-0.60374,-0.59975,-0.59574,-0.59172,-0.58768,-0.58362,-0.57956,-0.57548,-0.57138,-0.56727,-0.56314,-0.559,
-0.55485,-0.55069,-0.54651,-0.54231,-0.5381,-0.53388,-0.52965,-0.5254,-0.52114,-0.51687,-0.51258,-0.50828,
-0.50397,-0.49964,-0.4953,-0.49095,-0.48659,-0.48222,-0.47783,-0.47343,-0.46902,-0.4646,-0.46017,-0.45572,
-0.45127,-0.4468,-0.44232,-0.43783,-0.43333,-0.42882,-0.4243,-0.41976,-0.41522,-0.41067,-0.4061,-0.40153,
-0.39694,-0.39235,-0.38775,-0.38313,-0.37851,-0.37388,-0.36923,-0.36458,-0.35992,-0.35525,-0.35058,-0.34589,
-0.34119,-0.33649,-0.33178,-0.32705,-0.32233,-0.31759,-0.31284,-0.30809,-0.30333,-0.29856,-0.29379,-0.289,
-0.28421,-0.27942,-0.27461,-0.2698,-0.26498,-0.26016,-0.25533,-0.25049,-0.24565,-0.2408,-0.23594,-0.23108,
-0.22621,-0.22134,-0.21646,-0.21157,-0.20668,-0.20179,-0.19689,-0.19199,-0.18708,-0.18216,-0.17724,-0.17232,
-0.16739,-0.16246,-0.15753,-0.15259,-0.14764,-0.1427,-0.13775,-0.13279,-0.12783,-0.12287,-0.11791,-0.11294,
-0.10797,-0.103,-0.098028,-0.093051,-0.088071,-0.083089,-0.078106,-0.07312,-0.068132,-0.063143,-0.058152,
-0.05316,-0.048167,-0.043172,-0.038176,-0.033179,-0.028182,-0.023183,-0.018184,-0.013185,-0.0081852
};

结果图片,其中testlinintoutput是线性插值的结果,而testoutput是使用sin函数计算出来的结果。

声明:本内容为作者独立观点,不代表电子星球立场。未经允许不得转载。授权事宜与稿件投诉,请联系:editor@netbroad.com
本篇所含全部资料,点击此处留下邮箱我会发给你
资料明细:linear.zip
觉得内容不错的朋友,别忘了一键三连哦!
赞 3
收藏 2
关注 9
成为作者 赚取收益
全部留言
0/200
  • 熊紅 2022-11-27 11:51
    老师,能不能发我一下资料,谢谢! m1****@****.com
    回复 2条回复