特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接+ |( L7 H' a) P, y' k( X( k
第9章 BasicMathFunctions的使用(二) . |9 n" S* C8 E) A' M
本期教程主要讲基本函数中的相反数,偏移,位移,减法和比例因子。 9.1 相反数(Vector Negate) 9.2 求和(Vector Offset) 9.3 点乘(Vector Shift) 9.4 减法(Vector Sub) 9.5 比例因子(Vector Scale) 9.6 BasicMathFunctions的重要说明 9.7 总结 & m* w0 X, _7 D( ?' {0 h9 y
9.1 相反数(Vector Negate) 这部分函数主要用于求相反数,公式描述如下: pDst[n] = -pSrc[n], 0 <= n < blockSize. 特别注意,这部分函数支持目标指针和源指针指向相同的缓冲区。 9.1.1 arm_negate_f32 这个函数用于求32位浮点数的相反数,源代码分析如下: - /** K! A+ u1 |5 ?
- * @brief Negates the elements of a floating-point vector.
( j; X" X7 s N4 f - * @param[in] *pSrc points to the input vector ' D# P3 M. j( M& [4 O8 [
- * @param[out] *pDst points to the output vector
, O# J$ v" a0 Q: x - * @param[in] blockSize number of samples in the vector 3 b6 ^/ w5 i% s( G! K
- * @return none. ; q# @" j3 s/ L+ J4 D! x
- */
, @ s. A: Z+ P7 ?0 M -
+ z' Y. @6 Q9 Y% I - void arm_negate_f32(
$ e$ f$ Z/ @; B/ L+ @ - float32_t * pSrc," @& J3 C3 N- ^: g3 Q% r# w% m/ Q, _
- float32_t * pDst,, {! O1 P* N& X
- uint32_t blockSize)" N8 N0 S7 d5 n0 `# B0 w
- {
9 F& Z# C" Y1 @* h7 K$ z - uint32_t blkCnt; /* loop counter */
6 Z7 l0 e" F& ?8 l4 Z; t. S- s: S - % d1 y+ D* Z+ }* M0 Y
- ( K8 j, t+ K+ ^4 ~) s
- #ifndef ARM_MATH_CM0_FAMILY
) Q$ ^ z; j& U6 M6 S - ; T, n5 d: d I! q2 P/ }: C6 E9 l ~, K+ g
- /* Run the below code for Cortex-M4 and Cortex-M3 */. ?. z% _4 v$ K9 v1 m) d6 `2 V0 }
- float32_t in1, in2, in3, in4; /* temporary variables */
" }2 C) `7 j2 D B( Q' ]3 r3 J! s - % ^" G' s* d, r5 R" s
- /*loop Unrolling */8 ~6 R# I& Q1 T+ j2 h% m
- blkCnt = blockSize >> 2u;1 o! ~ E, d( Z. l Z- S
-
5 M( T6 [4 o% Q( Z: d# m - /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 9 t, D5 R0 E, a& }' h
- ** a second loop below computes the remaining 1 to 3 samples. */
3 }$ P3 S+ k" E - while(blkCnt > 0u)2 s5 T! Z& r$ y8 @0 q: b B' J
- {3 C3 \# X! b1 `! D2 W2 O
- /* read inputs from source */
C; f3 r7 a9 n0 h+ l8 P - in1 = *pSrc;6 R: b7 R- @1 t1 ?
- in2 = *(pSrc + 1);
4 c4 f$ T* g' ?- ~3 l - in3 = *(pSrc + 2);
5 ~/ h2 p+ R: p7 e0 y - in4 = *(pSrc + 3);
+ `) a5 K$ J ?0 G; o& }$ C" R - # ]! E7 k7 b! A
- /* negate the input */ (1): d& V& S# q& S$ a8 X9 H
- in1 = -in1;
/ q' U% @: `: c8 D% l3 T - in2 = -in2;( v; j O# B9 C3 b2 ]" |4 u
- in3 = -in3;% P3 ~) n' v' P) R/ U$ e
- in4 = -in4;
2 u2 c* b9 `1 f. h- C& I - . [* `7 O* \8 T" F4 |
- /* store the result to destination */( x0 q9 d& `, j: m9 i
- *pDst = in1;
+ T& G* _$ [ K2 |" O - *(pDst + 1) = in2;: l2 c0 O9 @0 o% L
- *(pDst + 2) = in3;' }2 j9 s. p; c! h. ^
- *(pDst + 3) = in4;5 u9 P7 W/ B& u' o! A
- L/ N* L V _7 N' P+ U9 _
- /* update pointers to process next samples */1 @. o$ K5 u1 `# V2 E# o Y, Y/ T
- pSrc += 4u;9 Y) x% Q* M5 e9 c2 e4 V
- pDst += 4u;$ K7 h9 C0 X) ?* o
-
5 H( f7 v. j- k0 ?# L: I0 W8 k3 E - /* Decrement the loop counter */
/ u$ Q% K/ _5 n: z$ [ - blkCnt--;
O9 }: x& g2 t5 L0 L - }
" }- `8 D! `4 l; i! E - 7 P: g0 Q! w9 ]( \( o9 v/ Y
- /* If the blockSize is not a multiple of 4, compute any remaining output samples here. y u: }6 ` [( m$ I
- ** No loop unrolling is used. */( u4 P1 l6 g( `* c
- blkCnt = blockSize % 0x4u;
$ A8 `: m6 B" Q9 e; e/ v8 s0 O! ^9 q -
; ~1 J' D% W" d3 v2 ^ - #else
: \, T, v! }) L2 S( ^1 i/ Q -
8 @" h- d- R& U5 O - /* Run the below code for Cortex-M0 */- R) N" d. c! j2 V7 _6 a3 P
-
2 D/ v( B- w. e4 @ - /* Initialize blkCnt with number of samples */; d9 U$ D- Q2 n* O) f7 I
- blkCnt = blockSize;) H6 J7 j+ V4 ? f: |
-
9 X: N5 X% z; a3 } - #endif /* #ifndef ARM_MATH_CM0_FAMILY */
# y0 V/ D# z7 I7 P% J V -
# X+ K: f* \4 n- B - while(blkCnt > 0u)
( U; O4 v& b4 t. r2 e - {. z" ^" |; C3 ^: B5 O
- /* C = -A */3 a& v4 L, K( M: S( D1 Z }0 w* o& T
- /* Negate and then store the results in the destination buffer. */! _. R; F7 }6 ^7 ]7 Q/ G! p9 O5 u
- *pDst++ = -*pSrc++;! }3 A9 S* ?9 h" T
-
0 C" y4 |* l, W% K5 z0 u' c - /* Decrement the loop counter */
1 N0 ^; O* s0 c5 c+ c/ W5 D; y5 P - blkCnt--;% T0 N4 x: x, x+ f; W
- }
6 L( C1 ^- [' J2 ]. w5 A) c - }
复制代码1. 浮点数的相反数求解比较简单,直接在相应的变量前加上负号即可。
. o/ U; `/ K' e2 }1 ]9.1.2 arm_negate_q31 这个函数用于求32位定点数的相反数,源代码分析如下: - /** 5 u8 F2 I5 J* U+ n. o
- * @brief Negates the elements of a Q31 vector. 4 Z" u/ @0 p3 v' R0 E( m
- * @param[in] *pSrc points to the input vector
2 u" u( ]- f% [0 ^* |2 y0 n+ a: ] - * @param[out] *pDst points to the output vector 1 [# s; x) b- u) X6 _. D
- * @param[in] blockSize number of samples in the vector
7 K6 b4 G2 w& l: M f3 c - * @return none. , H$ p3 X; m" w1 n& V; v
- * - _: t* j# L' x. p
- * <b>Scaling and Overflow Behavior:</b> (1)
! W2 E) y# K5 }4 E6 r! |1 I, J9 J - * \par
]1 R$ U8 F9 X' z9 x5 f - * The function uses saturating arithmetic.
+ F) v9 g$ z1 g: D - * The Q31 value -1 (0x80000000) will be saturated to the maximum allowable positive value 0x7FFFFFFF. 5 I7 V( I, E# K$ x/ f
- */
: _+ l x" F( t2 \' h% q -
8 g% v# K2 G) q' ]) X- y& J - void arm_negate_q31(
. }( q& }# t0 e7 S8 u; ]1 k - q31_t * pSrc, z' \8 O& u/ S/ T% H' U
- q31_t * pDst,* O" z; e# r) w: h4 {; f
- uint32_t blockSize)2 x. z& A) [2 i! q5 L3 `; k
- {
. W# r6 S2 ^6 a' d7 S - q31_t in; /* Temporary variable */( h- B4 q2 w* e1 O8 z3 D3 R
- uint32_t blkCnt; /* loop counter */
% F4 G: ^6 O1 c; u" t0 u; y2 a -
- o* d" q1 y0 S ]/ n - #ifndef ARM_MATH_CM0_FAMILY# i9 c$ \6 d% I" T0 _. i- L
-
' A" B, t- E1 j$ Y) \" V' z7 w - /* Run the below code for Cortex-M4 and Cortex-M3 */
5 t- S% q+ Y+ d$ } - q31_t in1, in2, in3, in4;
! F$ r$ q) a4 ? - ' @$ g. O# j* @7 ?0 ]# s) v$ t
- /*loop Unrolling */
7 f: W4 w. u2 X( Y1 Q c0 U - blkCnt = blockSize >> 2u;1 |5 R6 f; ?9 @& X$ B
-
8 Z( D V1 K9 |5 ?2 _ - /* First part of the processing with loop unrolling. Compute 4 outputs at a time. ; C- w1 F6 C+ w
- ** a second loop below computes the remaining 1 to 3 samples. */. J+ \2 |! Y/ d- m1 F
- while(blkCnt > 0u); K0 ^( `+ V' j) H
- {
/ X% Q% t5 F B# _ ~ - /* C = -A */, Q3 [" R( S" b: E2 w
- /* Negate and then store the results in the destination buffer. */
& d( N( v- y, S- O) S - in1 = *pSrc++;
: c2 T* h" f" r" z0 B5 I. [. p - in2 = *pSrc++;% E* H: x2 q' Z" i7 _: P! o( p2 H% P
- in3 = *pSrc++;
# T6 ]) z& l# M5 h" @5 @& m9 ? - in4 = *pSrc++;8 f5 a4 i# e; X2 n
- 9 C( H! g3 U/ s3 _
- *pDst++ = __QSUB(0, in1); (2)
# k/ g1 _7 C" H/ Y& y - *pDst++ = __QSUB(0, in2);5 g% m$ g# x& N$ ]( V/ |6 g
- *pDst++ = __QSUB(0, in3);1 r# E4 `4 ^ z
- *pDst++ = __QSUB(0, in4);
4 c! S% `, E s! c -
! x# {9 V. J, x7 v; B- q - /* Decrement the loop counter */6 {" }! r" k4 Z4 a" ]' ]% S; O* b
- blkCnt--;/ q" G5 ]! `; T$ q( s/ y* m
- }
" t" l6 O; w% M+ a; M9 h -
9 y7 v7 F3 u: O4 u# W4 p; H - /* If the blockSize is not a multiple of 4, compute any remaining output samples here. # ^9 G9 ^2 ]! ~5 R2 u
- ** No loop unrolling is used. */
5 B U. d ~; c0 _( Q - blkCnt = blockSize % 0x4u;* v% S2 M+ L+ {, b0 A/ l
-
# J; J; ]# _& c' Q2 k2 h - #else) _- J- A. v; t& S5 O# c* m
- 0 [. Z+ @: x0 g; a6 X
- /* Run the below code for Cortex-M0 *// x# O% v4 D; }; ?# a
-
( b1 h% Z5 ^! K# |% Q) E; |3 p/ d& [ - /* Initialize blkCnt with number of samples */5 e: S- a- G* q: o
- blkCnt = blockSize;% |& q+ N6 H+ m, y J) F) T$ i
-
. ^) r* J+ `6 D) |$ c - #endif /* #ifndef ARM_MATH_CM0_FAMILY */
5 U. H, A3 Q' {; e0 ?, P -
/ U9 f2 P% R7 f" k -
: `1 _ W! s# f4 I7 i( j - while(blkCnt > 0u)
1 l! w( \5 s2 p. @ - {
2 F/ [" X W+ D3 |0 u, B# ? - /* C = -A */, w1 k0 y, E3 D/ g9 d/ w4 r0 z
- /* Negate and then store the result in the destination buffer. */
! O f7 d7 P E% x/ ]0 k. @9 @- c - in = *pSrc++;. T( k: U% A8 Z- Z
- *pDst++ = (in == INT32_MIN) ? INT32_MAX : -in;
. I7 ?8 a( K5 X: b - - s1 Y+ H1 j; i3 ~0 [3 S4 s4 s
- /* Decrement the loop counter */
" u1 i: J. [% [2 _% X, h( H8 A/ l - blkCnt--;# b' @* r. N2 E2 }
- }* Q, C- X! U9 I) N. B
- }
复制代码1. 这个函数使用了饱和运算。 饱和运算数值0x80000000将变成0x7FFFFFFF。 2. 饱和运算__QSUB我们在上一章已经详细讲述了,这就就是实现数值0减去相应的参数变量。
N9 G, O: ?! S9.1.3 arm_negate_q15 这个函数用于求16位定点数的相反数,源代码分析如下: - /**
" g; v/ a" d$ |; D - * @brief Negates the elements of a Q15 vector.
; K6 o: Q: u: G7 D* t4 U - * @param[in] *pSrc points to the input vector
/ t# g7 u% W9 W+ n6 a - * @param[out] *pDst points to the output vector
' Z3 [+ r; r) M! T7 ~ - * @param[in] blockSize number of samples in the vector * X+ @- _( K7 p }, n
- * @return none.
8 k, Z X2 ?6 p - * 2 ?. u& u2 f' X+ B$ I
- * \par Conditions for optimum performance
% c& o6 R$ X6 J: ~ - * Input and output buffers should be aligned by 32-bit
+ x% m, U6 |! D. E3 R0 S - *
K2 q; D. W' i6 G$ m6 ?& l8 U0 ? - * & y) ~+ s2 `) j* Q1 h7 ^
- * <b>Scaling and Overflow Behavior:</b> (1)
0 y. z& }; H* ?2 K3 V' V' k4 w - * \par + P6 b" v- V- u$ m `0 [- c/ D `
- * The function uses saturating arithmetic.
6 u: W0 S3 W8 u - * The Q15 value -1 (0x8000) will be saturated to the maximum allowable positive value 0x7FFF.
; G8 n$ h: D( _( `; u/ p) G/ Q - */, c9 D N! Y% b
- & J" ]3 M) T0 m% U$ N& H/ H: W
- void arm_negate_q15(
( q8 K( U g9 R0 X6 M - q15_t * pSrc,
2 B' u% | I1 Q$ `( G- V$ M - q15_t * pDst,
2 r4 r& D( e# x - uint32_t blockSize)
/ D' U9 |) l: p - {- ^( _1 ^3 E3 O- p$ t
- uint32_t blkCnt; /* loop counter */& c: W: @9 V7 s* l
- q15_t in;- o- w3 {$ d9 t$ ]# M! l; N. ?( N q
-
: s, ?9 ~$ N y3 a6 A8 g8 C+ c - #ifndef ARM_MATH_CM0_FAMILY* s* Y1 L' M; W# A; ^5 P. B
-
# j& x& D+ }7 O4 I X# z$ v- M! k - /* Run the below code for Cortex-M4 and Cortex-M3 */
+ Y4 V) o& K* L* |& | -
6 V: p9 s' l& B' R - q31_t in1, in2; /* Temporary variables */: c5 f( B: V- y1 Y0 D% z. ^
-
% O2 n9 |% N+ y" ?! K -
9 Y& U8 e& u+ O7 Y8 c/ {2 h; O - /*loop Unrolling */: K0 Z4 f( L6 [; D1 S
- blkCnt = blockSize >> 2u;+ [: U4 F9 s2 r: E. h5 p
-
' F1 L! K; J# u _; n: X - /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
) p3 G- a/ d* u- d) B - ** a second loop below computes the remaining 1 to 3 samples. */6 E1 B* M+ Y: ~! g& T4 P# Q( T
- while(blkCnt > 0u)
( Z/ m9 ~' L- n" V; R$ P) X - {
; {" W6 h! r" }5 N- F: z. R - /* C = -A */
. ~- W/ h/ o8 p: @+ g# Z - /* Read two inputs at a time */ (2)
0 h( A' {9 b6 a - in1 = _SIMD32_OFFSET(pSrc);6 r ]' F* @" J9 L2 F- o
- in2 = _SIMD32_OFFSET(pSrc + 2);
2 V! z W# g1 v& Y -
7 r1 T0 Z; A# E" a - /* negate two samples at a time */ (3)
* K* q% t% ?; k4 N0 y1 D/ J9 b, ]% G - in1 = __QSUB16(0, in1);, A+ P, I) ^: D& _! U* T0 C
-
3 ~$ F! i/ v& n$ |: G2 i - /* negate two samples at a time */
* X& p4 }: y5 \& x# f y - in2 = __QSUB16(0, in2);+ ?* P: `2 [# c9 ]& i: b; V
-
7 [1 j- @7 p" _( E' ]6 A - /* store the result to destination 2 samples at a time */ (4)
3 |: K- `& k* g$ T - _SIMD32_OFFSET(pDst) = in1;8 N% R x( G& x, h# n
- /* store the result to destination 2 samples at a time */
E8 L4 i! \! H5 W$ K - _SIMD32_OFFSET(pDst + 2) = in2;) w3 l' m9 E3 j. r/ l) A
-
- {: R) B6 k7 Z3 \4 d& U. t; j - ) r0 w4 E. L, }) K
- /* update pointers to process next samples */0 |) R1 r+ m Y7 y! F$ i4 ]
- pSrc += 4u;4 b( C4 T2 Y( X7 C" T
- pDst += 4u;; _# U# F7 Z2 G& T2 R* k6 c8 K6 y/ N
-
, D- F% z% j+ ~! S% F; R- {! |1 R - /* Decrement the loop counter */+ r6 P4 H Y8 E6 @. U* c" e
- blkCnt--;
% k' V0 J1 O- |( d. f1 ?( D1 j8 g3 [; F - }
! @ W* }( `/ \1 `1 y -
4 d/ y( p9 d8 ~% t - /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
, Q5 i4 Y0 K+ F$ |6 [+ Z7 d - ** No loop unrolling is used. */
9 n8 s7 f$ ]& [, d g2 F - blkCnt = blockSize % 0x4u;
- p. Q. h, E% k% ]' q4 I, ^4 k ? -
) V. G$ L& X8 A8 j2 Q( S1 K - #else
' E9 W+ X, q- q0 p; @: \ Z, s - 1 l5 @. f3 `3 |+ |; z( V
- /* Run the below code for Cortex-M0 */8 F) o+ ?% i6 S% o" l/ P+ D
- : ^) G0 [: _2 A% B0 a, T4 d& p
- /* Initialize blkCnt with number of samples */
2 u9 ? w- R* S3 l q8 | - blkCnt = blockSize;
, m1 ^, e# l2 I+ C9 H5 j( a/ D! o - # R5 _( s# Z y! {
- #endif /* #ifndef ARM_MATH_CM0_FAMILY */0 W" p$ z% k! i3 j( N
- % |% x; f, e/ L
- while(blkCnt > 0u)
4 N3 Q5 }! V; P9 p c - {
2 P. u2 F1 H" A. ~# b5 G9 A - /* C = -A */ j# w$ V+ P0 x9 U$ T# h* S. |
- /* Negate and then store the result in the destination buffer. */
% B$ k# z+ I' }8 o7 I: | - in = *pSrc++;- c8 b1 v% y" g& U+ B7 s! j0 t' l
- *pDst++ = (in == (q15_t) 0x8000) ? 0x7fff : -in;
* S. r5 F, _5 w7 h/ `/ N- ~8 [ -
, Z7 z9 l0 g' B3 p6 I9 D - /* Decrement the loop counter */
: U! r: ]) R/ O$ B/ x - blkCnt--;( K/ E) S% G( p% D% m S
- }
# q4 w! l# j+ @* k+ S! }! Z; E - }
复制代码1. 这个函数使用了饱和运算。 饱和运算数值0x8000将变成0x7FFF。 2. 一次读取两个Q15格式的数据。 3. 由于__QSUB是SIMD指令,这里可以实现一次计算两个Q15数据的相反数。 4. 这里实现一次赋值两个Q15数据。
d( K: | D1 s1 } v6 r+ W3 d9.1.4 arm_negate_q7 这个函数用于求8位定点数的相反数,源代码分析如下: - /**
5 u1 m' p, p6 F - * @brief Negates the elements of a Q7 vector. ( V; C" o; N% E! z& O
- * @param[in] *pSrc points to the input vector
2 w& \7 _7 M" C - * @param[out] *pDst points to the output vector # I W a( u/ N: F- R1 f3 H
- * @param[in] blockSize number of samples in the vector
, s$ M$ V" y$ k$ e4 i) S# Y# N - * @return none.
0 \( E# U9 @; M% s2 P- Y6 ~ - * + e& ^- Y* k- c4 q2 N+ }* T
- * <b>Scaling and Overflow Behavior:</b> (1)
6 V8 j* c" P4 ] - * \par
& v# z6 }$ D* l, V% o% F - * The function uses saturating arithmetic.
! i& q5 z% S5 v" | - * The Q7 value -1 (0x80) will be saturated to the maximum allowable positive value 0x7F.
2 Y3 G$ x) h" q - */
- d) g F# y n+ G" v8 [7 I -
$ @. V8 b; j1 u/ c& R3 F# I1 P0 M - void arm_negate_q7(
# q8 x+ p5 Q1 Z" o) u4 i - q7_t * pSrc,
2 `; y( C: E9 E2 G6 P" m - q7_t * pDst,6 e; F' f W$ Q# h5 b% ~ [8 U
- uint32_t blockSize)4 n7 n' Y( X, r" f
- {# Z0 k) o( b* p$ v2 m
- uint32_t blkCnt; /* loop counter */8 A, u% V, U" @6 f; G
- q7_t in;0 l& T3 P% J5 K$ R. ~+ p* S
-
1 j; c# e- F* Y* D" i1 Q: j - #ifndef ARM_MATH_CM0_FAMILY
1 t! e$ R; b* T! j& { -
# l* Q# e" R9 D# q8 p: G3 | - /* Run the below code for Cortex-M4 and Cortex-M3 */
5 o) n" I; e4 ` - q31_t input; /* Input values1-4 */
7 @$ B( ~8 j3 s9 T - q31_t zero = 0x00000000; (2)
* r( ^: C0 `5 w3 B - 5 X s4 z; t3 ~) ?
-
$ Z0 O o! s, J4 l' q - /*loop Unrolling */
Z" n9 v- d* E% C2 a' U4 `' N - blkCnt = blockSize >> 2u;
6 A9 t) `5 o+ E0 H - , e: A" F6 `5 B1 I
- /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
& h) K" H E5 f - ** a second loop below computes the remaining 1 to 3 samples. */
0 c. p5 c0 R+ V$ v - while(blkCnt > 0u)
! [1 U5 C/ R- ?3 ] - {
- }* ]% l8 R1 o# K+ E% q - /* C = -A */
0 t- J7 e) G! v! y. d0 I9 v j% Z - /* Read four inputs */
! U, o( E9 X0 y0 e" e7 J - input = *__SIMD32(pSrc)++; (3)
+ P8 Z8 T: O* O/ r: A% p- |# [% C -
4 |! \: V- q8 T9 D5 ^6 T" G - /* Store the Negated results in the destination buffer in a single cycle by packing the results */% y o6 k0 t4 T+ d- T! j
- *__SIMD32(pDst)++ = __QSUB8(zero, input); (4)
" d: s8 C3 f( g' i# N) j: z -
" i! j4 Q9 Z: t - /* Decrement the loop counter */
4 `8 \+ I% U4 y! H6 M9 ? - blkCnt--;
! Z0 n7 z# o7 [8 y1 _ - }9 ~/ B/ |' L1 K: V/ \8 {
-
0 k9 u o' P6 C; b, ^ - /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 5 y' W8 [; u- P0 _3 u
- ** No loop unrolling is used. */
- I7 j# S9 a( d1 ~ - blkCnt = blockSize % 0x4u;! I7 G ^. |# N+ {
-
& t. a/ ~8 |" T& m" C2 N& i3 i; i8 b - #else' d: |6 c" J8 K+ j
-
& w' i4 |9 {. m2 Y - /* Run the below code for Cortex-M0 */
' i' d6 p( X _' p% c, J. Z! {( u i - 5 c# }! v. L$ { D
- /* Initialize blkCnt with number of samples */
0 g8 t3 H9 {: s% E. c, T - blkCnt = blockSize;
" W4 z3 }6 W8 a) h9 u( d+ }; ] -
! x% G( t0 I9 b' B1 m& n - #endif /* #ifndef ARM_MATH_CM0_FAMILY */
3 M& |, P% c, R6 b. z2 M -
- d, [. _$ {. Y/ Z/ [ S - while(blkCnt > 0u)
% F) D6 S/ d; y, u' K: W" O0 U. d2 |5 S4 Y - {
6 z: t+ M; {7 S8 V9 J( q2 J - /* C = -A */
' y0 j1 E) c0 t: h; S4 } - /* Negate and then store the results in the destination buffer. */ \
8 I! B5 M+ \0 _+ a9 h - in = *pSrc++;; N; M* I Q: w7 `
- *pDst++ = (in == (q7_t) 0x80) ? 0x7f : -in;
! v+ Q: C7 r. A8 A1 k' R -
; h5 l; h) d' @ - /* Decrement the loop counter */$ o w8 ?+ g/ k6 F! N
- blkCnt--;' `' F. d$ L. J8 M
- }
; d& B/ |$ J+ z - }
复制代码1. 这个函数使用了饱和运算。 饱和运算数值0x80将变成0x7F。 2. 给局部变量赋初值,防止默认初始值不是0,所以从某种意义上来说,给变量赋初值是很有必要的。 3. 一次读取4个Q7格式的数据到input里面。 4. 通过__QSUB8实现一次计算四个Q7格式数据的相反数。
$ h! r2 ]" P7 `4 M- [9.1.5 实例讲解实验目的: 1. 四种类型数据的相反数。 实验内容: 1. 按下K1键, 串口打印输出结果 实验现象: 通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下: 程序设计: - /*' ~5 _2 z/ S9 S; S, z$ U
- *********************************************************************************************************
0 N; q: p/ }7 S: K& g - * 函 数 名: DSP_Negate
: W: o( Q( |# x! S1 C- s/ T - * 功能说明: 求相反数& `" g; q* R& R
- * 形 参:无6 t6 @+ W4 H, u# r& x* g
- * 返 回 值: 无% L9 I* b0 w# C; x' s8 a& e |
- *********************************************************************************************************4 h. J2 b: W7 `2 w. D* X/ b+ Y. j
- */: G! c5 D! D7 W$ W
- static void DSP_Negate(void)
1 ~* U* P1 W! ~% F5 e: X5 q$ w6 g - {0 j/ D. {& `* U, M
- static float32_t pSrc;$ d M+ c1 h$ h
- static float32_t pDst;
& @, M( }8 ~0 W. i" c - static q31_t pSrc1;- x7 n* O3 A; a7 N
- static q31_t pDst1;
- s! b' T9 u2 R6 T4 L5 v" \ - static q15_t pSrc2;
: D2 o% n$ z- T - static q15_t pDst2;, U( C- ]! O N) R, b
- static q7_t pSrc3 = 127; /* 为了说明问题,在这里设置初始值为127,然后查看0x80是否饱和为0x7F */8 T) h& k. o# R! c, V/ N# G
- static q7_t pDst3;
! `' w5 Y& O6 H) V - pSrc -= 1.23f;, a) `/ V+ R8 D1 a( i( s
- arm_negate_f32(&pSrc, &pDst, 1);; z0 a; H: ?( H- J( h
- printf("arm_negate_f32 = %f\r\n", pDst);
. L0 W+ H, H" S ? - / L2 ?4 b2 A. F- C( ?3 d
- pSrc1 -= 1;
\. q% W% W$ P" d% I4 v6 z+ b$ A$ { - arm_negate_q31(&pSrc1, &pDst1, 1);
$ m* f4 e7 P9 c+ O6 R0 N - printf("arm_negate_q31 = %d\r\n", pDst1);% s j/ [9 e9 S8 e
- / C1 I! d4 x1 P' n& R
- pSrc2 -= 1;
5 f6 }" n' a9 G9 h" b! K5 q4 K/ C9 N2 R - arm_negate_q15(&pSrc2, &pDst2, 1);
* O- a. B+ D$ z+ V - printf("arm_negate_q15 = %d\r\n", pDst2);) o. ` _& S4 i' B Z- h" x. ~
-
5 m( I, H A" Q0 a$ `- y% P - pSrc3 += 1;
C! \+ V. {: d9 f - arm_negate_q7(&pSrc3, &pDst3, 1);
. E7 S* w5 K4 B+ ?; d& C - printf("arm_negate_q7 = %d\r\n", pDst3);
3 k5 L% v4 `) q' \% ` - printf("***********************************\r\n");
" d7 ]3 p/ w& G {* W4 k7 b+ z( M - }
复制代码
: m0 a/ c B+ j& y3 w" h4 w* } |
9.2.2 arm_offset_q31
9.2.3 arm_offset_q15
9.2.4 arm_offset_q7
9.2.5 实例讲解
9.3.2 arm_shift_q15
9 x K/ R: |% I
9.3.4 实例讲解
9.4.2 arm_sub_q31
9.4.3 arm_sub_q15
9.4.4 arm_sub_q7
9.4.5 实例讲解
9.5.2 arm_scale_q31
9.5.3 arm_scale_q15
9.5.5 实例讲解
9.7 总结