特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接
w' {" z- l @4 f6 k0 D# J5 [) C第21章 InterpolationFunctions的使用
" B6 \; a Z7 c3 w 本期教程主要讲解一维数据的线性插值和二维数据的双线性插值。 21.1 线性插补 Linear Interpolation 21.2 双线性插补 Bilinear Interpolation 21.3 总结 ( `$ H6 m: f& c1 M. K6 a
21.1 线性插值 Linear Interpolation. g+ }2 |% L$ u# g+ X; u, L! K2 S
21.1.1 arm_linear_interp_f32公式描述: 直线插补曲线可以通过线性多项式进行拟合。线性内插的工作原理有效地绘制两个相邻样本之间的直线和沿该线再返回相应的插补点。 如上图所示,确定了输入参数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在输入范围之下返回第一个样本值,如果相爱输入范围之上,返回最后一个样本值。 # Y2 J; \1 W8 T" Q: y+ F
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位用于小数部分。 9 _/ c3 R: A- w- @; s" P; {( w
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位用于小数部分。 : x+ Q7 T4 V0 y7 G" t- S1 b, p( S, ~
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位用于小数部分。 3 |; l; O. N0 b, b
21.1.5 实例讲解实验目的: 1. 学习InterpolationFunctions中线性插补的实现 实验内容: 1. 按下按键K1, 串口打印函数DSP_MatLinearInterpolation的输出结果 实验现象: 通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下: 程序设计: - /* ---------------------------------------------------------------------- (1)7 i; R) b0 w/ O* y1 }
- * Test input data for F32 SIN function: g9 O6 W( Q1 d1 R
- * Generated by the MATLAB rand() function9 c7 ^/ H0 x1 g/ |6 Z3 l8 d2 c. T
- * randn('state', 0)
% G7 C" A5 W0 n - * xi = (((1/4.18318581819710)* randn(blockSize, 1) * 2* pi));
n: G% E* a- j$ ^ - * --------------------------------------------------------------------*/
: X( L# X. {: C - float32_t testInputSin_f32[TEST_LENGTH_SAMPLES] =" N: u* u9 b2 M$ \+ |# w2 u$ Y
- {
- ^ e! G0 X/ c) q5 E- w - -0.649716504673081170, -2.501723745497831200,1 Q- ~ u4 W/ n# N4 F
- 0.188250329003310100, 0.432092748487532540,
3 z( p2 p+ H$ T ]/ ]% @- e - -1.722010988459680800, 1.788766476323060600,
* }& H: t* @# i5 ?" K$ E - 1.786136060975809500, -0.056525543169408797,
. o. ~- X; D* u+ Y - 0.491596272728153760, 0.262309671126153390
6 Z) Z3 [8 B' l& h - };2 O$ X) S: h8 g6 K3 c
-
: r- O1 l# z8 _4 c: B6 b - /*------------------------------------------------------------------------------' B2 N( z8 c- E: Y
- * Reference out of SIN F32 function for Block Size = 10
+ m' B- T9 X; T7 @! f6 I" C$ P - * Calculated from sin(testInputSin_f32)+ b* b @4 y U( ?7 u& ?) x
- *------------------------------------------------------------------------------*/2 {( |- x, O& ?2 D. n( W, L
- float32_t testRefSinOutput32_f32[TEST_LENGTH_SAMPLES] =
' z( f* C/ v8 K( Z7 D$ D! j - {, G, L# g, O0 I
- -0.604960695383043530, -0.597090287967934840,) H2 v2 M' U \' `$ |& |
- 0.187140422442966500, 0.418772124875992690,6 w- a5 g, Q1 n# e( ?7 {
- -0.988588831792106880, 0.976338412038794010,& E2 Q. _$ `4 w
- 0.976903856413481100, -0.056495446835214236,7 S( G# v9 A) k1 |) H
- 0.472033731854734240, 0.259311907228582830
9 j" }' T+ y3 p( ?; c8 Y+ u9 S4 y7 c - };
1 w$ ]3 `; p A% O' M& W4 A - % S% n2 u: R3 n2 m! S$ X: I
- /*------------------------------------------------------------------------------$ Z4 r3 e( j P. S3 D N4 d0 m0 {
- * Method 1: Test out Buffer Calculated from Cubic Interpolation (2)
, |) e3 N- F6 Z - *------------------------------------------------------------------------------*/
8 R+ ]) m4 T: @! y- _& P - float32_t testOutput[TEST_LENGTH_SAMPLES];+ s" w2 w" y! [8 y9 Y& g0 G
- $ I1 A$ J3 X+ }' S5 T& \- `6 E
- /*------------------------------------------------------------------------------: J$ ]1 A, A$ q: q% k
- * Method 2: Test out buffer Calculated from Linear Interpolation; u; t+ ~& ^$ M. s" R$ ?( }' @
- *------------------------------------------------------------------------------*/
' |1 B. I8 ]) y) q - float32_t testLinIntOutput[TEST_LENGTH_SAMPLES];9 E/ c$ a: B- o3 A& q( `% E! r0 \
- 0 i# k z( i9 }' ~5 p P# F
- /*------------------------------------------------------------------------------
7 h! i+ ]1 v, \ - * External table used for linear interpolation6 ~+ d% `' e7 g- p4 @( X
- *------------------------------------------------------------------------------*/
; a( S2 z6 x+ c D4 t8 y# c - extern float arm_linear_interep_table[188495];
6 }# W9 O6 h6 I* l$ b' e) S - : Q$ ~6 c/ S" H: d! z2 g2 I
- /* ----------------------------------------------------------------------
* @4 o4 D9 N3 w$ A, s" U* v - * Global Variables for caluclating SNR's for Method1 & Method 2/ R$ ]( H7 y0 h0 ?9 t
- * ------------------------------------------------------------------- */" Y7 K3 [8 m4 P/ X( A
- float32_t snr1; u6 d* j' i4 w& r0 _
- float32_t snr2;
7 R. z6 Z0 y0 g. [! L - /*- l4 c6 Q! e6 l4 {, E
- *********************************************************************************************************
# ], O7 T& x6 b: h8 g - * 函 数 名: DSP_MatLinearInterpolation9 r* o- c) S- Y: _4 ], d" D
- * 功能说明: 线性插值& G: y E) r5 s; R
- * 形 参:无
- S! G2 l( O2 v& w0 _; t8 N+ Z - * 返 回 值: 无; n3 {% H. R b
- *********************************************************************************************************
! V8 G/ J# I* S, T - */2 a6 z2 e# a# }7 p- ]+ h8 a: l
- static void DSP_MatLinearInterpolation(void)# I" G1 t* A8 @1 B5 ^0 h! n& k. k
- {8 \7 x2 o: x2 R; y+ _$ ? M* D
- uint32_t i;. Q% ^! u; z" s# w2 y b& c% U
-
_. k8 F( W) Z$ A1 u8 } ^ - arm_linear_interp_instance_f32 S = {188495, -3.141592653589793238, XSPACING,
; T) h3 S3 |8 ~ - &arm_linear_interep_table[0]};# w- w2 ~3 S! g; \
-
6 k K. c9 O! [4 l( c - /*------------------------------------------------------------------------------
( W. L, ~. M! F - * Method 1: Test out Calculated from Cubic Interpolation
3 o7 j* q; t: l - *------------------------------------------------------------------------------*/
$ {/ U. {; N0 i2 O7 |2 f- b - for(i=0; i< TEST_LENGTH_SAMPLES; i++)
! v1 v( `* t$ S" R5 `# R - {, }7 I9 j! J I' f7 ^
- testOutput[i] = arm_sin_f32(testInputSin_f32[i]);
2 M+ e* {8 v% b - }
7 q5 L8 G, i5 ]' ?. t2 u+ A -
; Q% a, Z$ F/ F3 L0 Y# s3 v - /*------------------------------------------------------------------------------0 U8 j8 ^# l9 ]
- * Method 2: Test out Calculated from Cubic Interpolation and Linear interpolation
* V3 k# m( l ]4 j - *------------------------------------------------------------------------------*/; T9 N( g& C7 s
- * {3 N$ X5 n; b1 C( c' W+ Y
- for(i=0; i< TEST_LENGTH_SAMPLES; i++)
; j- Z1 ^6 T. u1 ?) g2 y$ t+ P - {
7 @' k6 H/ o$ \& r+ Y9 B4 f+ } - testLinIntOutput[i] = arm_linear_interp_f32(&S, testInputSin_f32[i]);. b, B5 F, A7 l5 A5 D- G/ U4 i
- }6 g) U5 L9 _. G1 K/ G, H
-
: p+ [, @: b( L3 O+ K - /*------------------------------------------------------------------------------
4 t/ K v: h* ^9 N# P/ b3 @7 U p4 n - * SNR calculation for method 1
$ F" x! d: S# v - *------------------------------------------------------------------------------*/3 r$ l$ Q- H9 \" s ~' m# j i
- snr1 = arm_snr_f32(testRefSinOutput32_f32, testOutput, 2); (3)+ L8 I' A/ A! p l7 X
- printf("Cubic Interpolation snr1 = %f\r\n", snr1);
" ~! L# H+ S8 @7 Z) r- n& B - ! A, }! i0 V R5 @4 X6 _+ h; L
- /*------------------------------------------------------------------------------
/ s, z; k$ ~3 w# f" q5 V' B% I' t* @ - * SNR calculation for method 2
: L/ e# }# C4 {8 k - *------------------------------------------------------------------------------*/
8 z& Q8 B+ v# T3 i0 d$ J; e9 w, A - snr2 = arm_snr_f32(testRefSinOutput32_f32, testLinIntOutput, 2);) N+ `0 f9 X7 [: k0 g: Z# q+ M
- printf("Linear Interpolation snr2 = %f\r\n", snr2);; d8 L, k ?! y5 b+ ^6 A/ {) z
- # L: o# |% U: Z) s5 G5 v4 c
- /*------------------------------------------------------------------------------2 ~& g s; y( F. g8 F
- * Initialise status depending on SNR calculations9 r1 G. \$ B% E8 T1 A: ?& X% q$ Q: q
- *------------------------------------------------------------------------------*/
+ a# a* | e, W& @: P- T - if( snr2 > snr1), T, J, _5 N$ ]
- {
) D* g' y+ i8 Z' D - printf("ARM_MATH_SUCCESS\r\n");
1 P, W3 K' S( g6 k# R - }
/ H! u; e' M) F+ q - else
: v5 q: ], ^/ s5 E- r9 } - {" l! \/ @ k7 m. O) X- x1 m
- printf("ARM_MATH_TEST_FAILURE\r\n");
% C9 r$ `: x3 J5 x# ]4 G - }- B4 t6 C& ~, A
- }
复制代码1. 使用这个程序前需要通过Matlab获取如下的插值样本: x = -pi: 0.00005 : (2*pi - 0.00005); y = sin(x); 这个插值样本已经放在在了如下文件中 2. 这个函数通过信噪比来对比了前面讲的三次插补和线性插补。最终结果是线性插补的结果要优于三次插补。 3. 关于信噪比的计算,我们会在下章做详细的讲解。
7 l" w8 G1 _8 m- R. U |
21.2.1 arm_bilinear_interp_f32
21.2.2 arm_bilinear_interp_q31
21.2.3 arm_bilinear_interp_q15
21.2.4 arm_linear_interp_q7