你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【安富莱——DSP教程】第21章 InterpolationFunctions的使用

[复制链接]
baiyongbin2009 发布时间:2015-4-2 10:48
特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接& ]; E# c9 y( w/ A  z2 E$ R
第21章 InterpolationFunctions的使用
8 G2 X0 h+ w/ `5 L* m0 k5 r7 G
    本期教程主要讲解一维数据的线性插值和二维数据的双线性插值。
    21.1 线性插补 Linear Interpolation
    21.2 双线性插补 Bilinear Interpolation
    21.3 总结

3 H: {2 Y! `4 s* \; S! ~% [% Z" x3 b21.1 线性插值 Linear Interpolation1 Z$ ^) l$ m% A5 P$ B2 X* O' [$ Z: o
21.1.1 arm_linear_interp_f32
公式描述:
21.1.png
    直线插补曲线可以通过线性多项式进行拟合。线性内插的工作原理有效地绘制两个相邻样本之间的直线和沿该线再返回相应的插补点。
    如上图所示,确定了输入参数x在样本数据中的位置后,就可以绘制这两个样本值之间的直线。然后返回X值对应的Y值。返回方法就是下面的直线公式:
      y = y0 + (x - x0) * ((y1 - y0)/(x1-x0))
    其中x0和x1是输入值x最近的两个数据,y0和y1是输出值y最近的两个值。
函数定义如下:
    static __INLINE float32_t arm_linear_interp_f32(
    arm_linear_interp_instance_f32 * S,
    float32_t x)
参数定义:
        [in,out] *S  is an instance of the floating-point Linear Interpolation structure
        [in]   x   input sample to process
    return y  processed output sample.
注意事项:
    1. 结构arm_linear_interp_instance_f32的定义如下(在文件arm_math.h文件):
    typedef struct
   {
          uint32_t nValues;           /**< nValues */
          float32_t x1;               /**< x1 */
          float32_t xSpacing;         /**< xSpacing */
          float32_t *pYData;          /**< pointer to the table of Y values */
     } arm_linear_interp_instance_f32;
    2. 如果输入参数x在输入范围之下返回第一个样本值,如果相爱输入范围之上,返回最后一个样本值。

/ L6 ?% [; v5 ]" G2 t/ U# ^
21.1.2 arm_linear_interp_q31
函数定义如下:
    static __INLINE q31_t arm_linear_interp_q31(
        q31_t * pYData,
        q31_t x,
        uint32_t nValues)
参数定义:
        [in] *pYData  pointer to Q31 Linear Interpolation table
        [in] x         input sample to process
        [in] nValues  number of table values
        return y      processed output sample.
注意事项:
    1. 结构arm_linear_interp_instance_q31的定义如下(在文件arm_math.h文件):
      typedef struct
      {
         uint16_t numRows;   /**< number of rows in the data table. */
         uint16_t numCols;   /**< number of columns in the data table. */
         q31_t *pData;       /**< points to the data table. */
  } arm_bilinear_interp_instance_q31;
    2. 如果输入参数x在输入范围之下返回第一个样本值,如果相爱输入范围之上,返回最后一个样本值。
    3. 输入参数x的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。
- _: W  O0 X3 L# P' B
21.1.3 arm_linear_interp_q15
函数定义如下:
    static __INLINE q15_t arm_linear_interp_q15(
        q15_t * pYData,
        q31_t x,
        uint32_t nValues)
参数定义:
        [in] *pYData  pointer to Q15 Linear Interpolation table
        [in] x         input sample to process
        [in] nValues  number of table values
        return y      processed output sample.
注意事项:
    1. 结构arm_linear_interp_instance_q15的定义如下(在文件arm_math.h文件):
      typedef struct
     {
            uint16_t numRows;   /**< number of rows in the data table. */
            uint16_t numCols;   /**< number of columns in the data table. */
            q15_t *pData;       /**< points to the data table. */
        } arm_bilinear_interp_instance_q15;
     2. 如果输入参数x在输入范围之下返回第一个样本值,如果相爱输入范围之上,返回最后一个样本值。
     3. 输入参数x的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。
6 {. \/ |! E" U
21.1.4 arm_linear_interp_q7
函数定义如下:
      static __INLINE q7_t arm_linear_interp_q7(
        q7_t * pYData,
        q31_t x,
        uint32_t nValues)
参数定义:
        [in] *pYData  pointer to Q7 Linear Interpolation table
        [in] x         input sample to process
        [in] nValues  number of table values
        return y      processed output sample.
注意事项:
    1. 结构arm_linear_interp_instance_q7的定义如下(在文件arm_math.h文件):
       typedef struct
     {
          uint16_t numRows;   /**< number of rows in the data table. */
          uint16_t numCols;   /**< number of columns in the data table. */
          q7_t *pData;                /**< points to the data table. */
        } arm_bilinear_interp_instance_q7;
    2. 如果输入参数x在输入范围之下返回第一个样本值,如果相爱输入范围之上,返回最后一个样本值。
    3. 输入参数x的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。
9 [2 w' [7 ^3 ~  i2 a/ l
21.1.5 实例讲解
实验目的:
   1. 学习InterpolationFunctions中线性插补的实现
实验内容:
    1. 按下按键K1, 串口打印函数DSP_MatLinearInterpolation的输出结果
实验现象:
    通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
21.2.png
程序设计:
  1. /* ----------------------------------------------------------------------                              (1)
    & z! o2 N: u6 x% e# p
  2. * Test input data for F32 SIN function
    7 G+ g4 b' {! {% L2 ]  j
  3. * Generated by the MATLAB rand() function
    ; ?' p5 }4 C% K8 d6 H
  4. * randn('state', 0)( @& R6 m& |2 b( g. \' P4 `( z
  5. * xi = (((1/4.18318581819710)* randn(blockSize, 1) * 2* pi));
    5 w  |: i; B# [- ]0 n- H# Y
  6. * --------------------------------------------------------------------*/8 S* B$ I& _  r* q1 D1 u
  7. float32_t testInputSin_f32[TEST_LENGTH_SAMPLES] =
    6 {# e  C1 {* U) C' d& E$ j, P, I
  8. {
    4 l, m% w) ~& W  S9 x+ Q4 x
  9.    -0.649716504673081170, -2.501723745497831200,
    ) b9 Z; j8 v. |0 f2 A
  10.     0.188250329003310100,  0.432092748487532540,
    3 ~* z) o6 m9 O% O. t1 K
  11.    -1.722010988459680800,  1.788766476323060600,
    * o" D. R  Q( A2 C
  12.     1.786136060975809500, -0.056525543169408797,
    % M5 j4 I) n* a) J& Y, K) V
  13.     0.491596272728153760,  0.2623096711261533900 d4 L. c& M  X- c5 J
  14. };  U! v" i4 c% \1 }# s

  15. + c2 S% |/ K4 N/ Q1 \
  16. /*------------------------------------------------------------------------------( [& [! ]& F! b4 ~
  17. *  Reference out of SIN F32 function for Block Size = 10
    ) R+ e5 }2 n# Y
  18. *  Calculated from sin(testInputSin_f32)
    8 q% F. y0 @3 P0 b9 q0 L
  19. *------------------------------------------------------------------------------*/, @. e9 F, H6 b
  20. float32_t testRefSinOutput32_f32[TEST_LENGTH_SAMPLES] =: R8 e: }$ p9 c: N
  21. {
    , y$ d- T* G, }6 j! G6 f
  22.    -0.604960695383043530, -0.597090287967934840,# }6 |) f  m& y( H, A
  23.     0.187140422442966500,  0.418772124875992690,, K  ^2 v' n, r% {# K
  24.    -0.988588831792106880,  0.976338412038794010,
    5 Y. \, f4 C# A$ J1 G+ G
  25.     0.976903856413481100, -0.056495446835214236,. o2 `, _) |. Z0 ^
  26.     0.472033731854734240,  0.259311907228582830
    + V! _: E) g3 N& w, A: \
  27. };
    7 Y" ^% i; e. _' X0 c
  28. " W3 A6 b! H$ M
  29. /*------------------------------------------------------------------------------
    * r' Y. D9 K; k
  30. *  Method 1: Test out Buffer Calculated from Cubic Interpolation                                      (2)" F4 H5 q! M% M  ?  f
  31. *------------------------------------------------------------------------------*/
    ; r2 d2 X0 G( b7 n# i
  32. float32_t testOutput[TEST_LENGTH_SAMPLES];
    / H" P* M. A& H
  33. % f, a0 A& M6 c' L
  34. /*------------------------------------------------------------------------------
    & [2 ~) o/ ^& O( `# B
  35. *  Method 2: Test out buffer Calculated from Linear Interpolation
    + _3 S4 _2 I* x
  36. *------------------------------------------------------------------------------*/( F- A# E! P; a! u) ]$ t
  37. float32_t testLinIntOutput[TEST_LENGTH_SAMPLES];
    0 ~8 f3 M' w6 f- H/ ?
  38. 7 R. T# ~, I( X/ L5 {' [
  39. /*------------------------------------------------------------------------------
    8 K: v8 a/ ?' r0 {- i) C
  40. *  External table used for linear interpolation5 b, R4 O2 [1 |
  41. *------------------------------------------------------------------------------*/! [9 L" g5 K7 z* h; [% R) Q
  42. extern float arm_linear_interep_table[188495];
    ( V) i& E! S. \# w
  43. ) G- @% A. j( Q5 a# r
  44. /* ----------------------------------------------------------------------- o' [/ L' ]: E) [  }; f5 j. L3 `
  45. * Global Variables for caluclating SNR's for Method1 & Method 2
    5 n: P+ J, A/ U1 d+ o: f
  46. * ------------------------------------------------------------------- */
    0 s$ Y* {" G  B$ G2 J& Y! V
  47. float32_t snr1;% `* r$ p  X- m9 m
  48. float32_t snr2;+ q( Q2 ]) q" h5 m4 z
  49. /*
    / d" _) Q5 J" j; L
  50. *********************************************************************************************************
    ( B% z0 h# D4 L& O. X1 W- g
  51. *        函 数 名: DSP_MatLinearInterpolation2 |: l& s0 n1 {2 |+ X
  52. *        功能说明: 线性插值( b1 X2 {" [: y' I
  53. *        形    参:无2 y% F  y( z- B/ B) s0 a
  54. *        返 回 值: 无
    0 f) q8 @5 S: t( d8 K, C  c
  55. *********************************************************************************************************9 l; D9 k# D% n: `; @) p. U8 Q( ^
  56. */% B0 m. T& H* `; d  G/ E  m
  57. static void DSP_MatLinearInterpolation(void)
      `+ T6 B# K9 V" u6 b( j" F5 }
  58. {/ n3 z0 e$ U7 R6 |# c
  59. uint32_t i;
    , M8 v* x3 T! V9 E- T  C

  60. 9 z; d9 L! c  a' S& A4 ^8 Y
  61. arm_linear_interp_instance_f32 S = {188495, -3.141592653589793238, XSPACING, : V8 y  c& z9 L. h; Y2 q9 z, b
  62. &arm_linear_interep_table[0]};4 V3 A* V, h4 v% D% f7 s

  63. + k8 a0 U8 l/ u& T- k" l
  64. /*------------------------------------------------------------------------------, |5 }" q) G5 `* a/ ]' V* O- F
  65. *  Method 1: Test out Calculated from Cubic Interpolation
    + O4 e( a% [% U1 W
  66. *------------------------------------------------------------------------------*/0 t( ^) k! G; f) s3 k( c3 S! j
  67. for(i=0; i< TEST_LENGTH_SAMPLES; i++)- V' z7 B$ T( C7 U$ \% n
  68. {
    & e9 E! \! h' O1 a6 T1 T
  69. testOutput[i] = arm_sin_f32(testInputSin_f32[i]);
    2 i# ~6 L! D: s+ U2 K
  70. }/ Q. w- l2 y( }0 q- y) [0 m
  71. 8 V' q$ F, f5 o) e: y/ w
  72. /*------------------------------------------------------------------------------
      r+ A6 ]- R; i( x
  73. *  Method 2: Test out Calculated from Cubic Interpolation and Linear interpolation* i( t% ~. q1 U
  74. *------------------------------------------------------------------------------*/( E3 E/ D  O7 G, `2 M) L) U0 M

  75. 2 \- M% u( r: `
  76. for(i=0; i< TEST_LENGTH_SAMPLES; i++)
    . F% Z3 @% m$ j6 n, ]0 t
  77. {
    4 f& Y- L3 a7 C; B# ]8 H$ q2 D
  78. testLinIntOutput[i] = arm_linear_interp_f32(&S, testInputSin_f32[i]);
    ( ?5 X& r3 Y% I
  79. }( R6 K8 F/ V/ O( U7 X

  80. ) e) w$ ?/ D& X2 d4 r+ f
  81. /*------------------------------------------------------------------------------
    ; O! s$ G8 p- @% v
  82. *            SNR calculation for method 1
    " I" l; t$ `8 ~
  83. *------------------------------------------------------------------------------*/3 [7 U/ g- j1 F+ z" d0 E" A' X
  84. snr1 = arm_snr_f32(testRefSinOutput32_f32, testOutput, 2);                                         (3)( x( O3 w# j4 @1 H$ ~  N7 Y! W) Y
  85. printf("Cubic Interpolation snr1 = %f\r\n", snr1);
    & M/ a5 K$ N+ O) X- s8 ^( k

  86. 9 k0 l# C8 W- w  o! N' Q
  87. /*------------------------------------------------------------------------------
    ! t- N1 S. D- x  v1 m: p; W7 V
  88. *            SNR calculation for method 2
    & {% C: U( f; B4 [$ B1 `
  89. *------------------------------------------------------------------------------*/
    4 n3 q5 I+ w4 {
  90. snr2 = arm_snr_f32(testRefSinOutput32_f32, testLinIntOutput, 2);
    5 A, C4 A7 w) A& Q4 Z6 Q
  91. printf("Linear Interpolation snr2 = %f\r\n", snr2);/ K2 y1 a2 i( C. Z
  92. , l7 Q5 K- E' Y" [
  93. /*------------------------------------------------------------------------------' N4 `1 V- ~& ?  o
  94. *            Initialise status depending on SNR calculations
    ) E  C0 Z5 M1 D# H0 v  [& K- S
  95. *------------------------------------------------------------------------------*/
    ' o; Y0 f8 `( u0 M6 _, E
  96. if( snr2 > snr1)
    # T6 V1 E$ k: S# L- V, j/ m" z
  97. {
    $ K( ]4 q5 d8 m( ~
  98. printf("ARM_MATH_SUCCESS\r\n");
    / A' O2 l- a7 A5 l
  99. }
    $ D6 U) L  y0 I  W5 W
  100. else
    & w" s5 }2 h8 N& y+ ]8 ~" L+ I
  101. {, `3 r# g2 f/ ^  }
  102. printf("ARM_MATH_TEST_FAILURE\r\n");
    ; p% |5 l, ?# f+ }( ]1 Q
  103. }
    1 D  i. Z  f) w8 l1 O# J7 t# C
  104. }
复制代码
1. 使用这个程序前需要通过Matlab获取如下的插值样本:
    x = -pi: 0.00005 : (2*pi - 0.00005);
    y = sin(x);
    这个插值样本已经放在在了如下文件中
21.3.png
2. 这个函数通过信噪比来对比了前面讲的三次插补和线性插补。最终结果是线性插补的结果要优于三次插补。
3. 关于信噪比的计算,我们会在下章做详细的讲解。
3 h: {+ {& O& ?) v: L" `( a* W3 d
赞 收藏 评论4 发布时间:2015-4-2 10:48

举报

4个回答
baiyongbin2009 回答时间:2015-4-2 10:57:52
21.2 双线性插值 Bilinear Interpolation( j( f( g4 p, K" E. D& X
21.2.1 arm_bilinear_interp_f32
公式描述:
    双线性插值要麻烦些,不过同线性插值的原理是一样的,公式如下:
    其中由x,y来指定插补点,那么做如下定义
         XF = floor(x)
         YF = floor(y)
    插补值的输出结果计算如下:
    f(x, y) = f(XF, YF) * (1-(x-XF)) * (1-(y-YF))
                   + f(XF+1, YF) * (x-XF)*(1-(y-YF))
                   + f(XF, YF+1) * (1-(x-XF))*(y-YF)
                   + f(XF+1, YF+1) * (x-XF)*(y-YF)
    (x,y)数据是由整数和小数两部分组成,整数部分用来获取所在的插补样值的位置,小数部分用于实际数据的处理。如果(x, y)数值在插补样值的范围之外,那么返回0。
函数定义如下:
      static __INLINE float32_t arm_bilinear_interp_f32(
      const arm_bilinear_interp_instance_f32 * S,
      float32_t X,
      float32_t Y)
参数定义:
        [in,out] *S  points to an instance of the interpolation structure.
        [in] X       interpolation coordinate.
        [in] Y       interpolation coordinate.
        return out interpolated value.
注意事项:
    1. 结构arm_bilinear_interp_instance_f32的定义如下(在文件arm_math.h文件):
        typedef struct
      {
       uint16_t numRows;   /**< number of rows in the data table. */
       uint16_t numCols;    /**< number of columns in the data table. */
       float32_t *pData;     /**< points to the data table. */
       } arm_bilinear_interp_instance_f32;
    2. 如果(x, y)数值在插补样值的范围之外,那么返回0。
' `. `) T# b2 i6 t
21.2.2 arm_bilinear_interp_q31
函数定义如下:
     static __INLINE q31_t arm_bilinear_interp_q31(
      arm_bilinear_interp_instance_q31 * S,
      q31_t X,
      q31_t Y)
参数定义:
         [in,out] *S points to an instance of the interpolation structure.
         [in] X interpolation coordinate in 12.20 format.
         [in] Y interpolation coordinate in 12.20 format.
         return out interpolated value.
注意事项:
    1. 结构arm_bilinear_interp_instance_q31的定义如下(在文件arm_math.h文件):
      typedef struct
      {
            uint16_t numRows;   /**< number of rows in the data table. */
            uint16_t numCols;   /**< number of columns in the data table. */
            q31_t *pData;       /**< points to the data table. */
        } arm_bilinear_interp_instance_q31;
    2. 如果(x, y)数值在插补样值的范围之外,那么返回0。
    3. 输入参数x和y的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。

1 q8 j, W. p+ \8 h$ n/ _
21.2.3 arm_bilinear_interp_q15
函数定义如下:
      static __INLINE q15_t arm_bilinear_interp_q15(
      arm_bilinear_interp_instance_q15 * S,
      q31_t X,
      q31_t Y)
参数定义:
         [in,out] *S points to an instance of the interpolation structure.
         [in] X interpolation coordinate in 12.20 format.
         [in] Y interpolation coordinate in 12.20 format.
         return out interpolated value.
注意事项:
    1. 结构arm_bilinear_interp_instance_q15的定义如下(在文件arm_math.h文件):
      typedef struct
      {
        uint16_t numRows;   /**< number of rows in the data table. */
        uint16_t numCols;   /**< number of columns in the data table. */
         q15_t *pData;       /**< points to the data table. */
  } arm_bilinear_interp_instance_q15;
    2. 输入参数x和y的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。
    3. 如果(x, y)数值在插补样值的范围之外,那么返回0。
+ v2 _4 Q4 y! p" {  c
21.2.4 arm_linear_interp_q7
函数定义如下:
      static __INLINE q7_t arm_bilinear_interp_q7(
       arm_bilinear_interp_instance_q7 * S,
       q31_t X,
       q31_t Y)
参数定义:
          [in,out] *S points to an instance of the interpolation structure.
          [in] X interpolation coordinate in 12.20 format.
          [in] Y interpolation coordinate in 12.20 format.
          return out interpolated value.
注意事项:
    1. 结构arm_bilinear_interp_instance_q7的定义如下(在文件arm_math.h文件):
      typedef struct
      {
            uint16_t numRows;   /**< number of rows in the data table. */
            uint16_t numCols;   /**< number of columns in the data table. */
            q7_t *pData;                /**< points to the data table. */
        } arm_bilinear_interp_instance_q7;
    2. 输入参数x的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。
    3. 如果(x, y)数值在插补样值的范围之外,那么返回0。
    4. 输入参数x和y的数据格式是12.20。这样32位数据的12位整数部分用于样本数据的检测,所以最大值就是2的12次方。后20位用于小数部分。
21.3  总结
    本期教程就跟大家讲这么多,有兴趣的可以深入研究下算法的具体实现。

# y, G3 r: k( x( _' f
stary666 回答时间:2015-4-2 11:00:04
抢个沙发,学习一下
zhenlinuaa 回答时间:2015-4-2 19:58:54
技术贴,支持
kqh1120 回答时间:2015-4-2 21:24:01
谢谢分享 smile.gif
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版