Electoday 4.0
ไมโครคอนโทรลเลอร์ => ARM Processors => Topic started by: tha on June 28, 2021, 09:39:56 am
-
AN4841
Application note
Digital signal processing for STM32 microcontrollers using CMSIS
Introduction
application note นี้อธิบาย the development of digital filters สำหรับ analog signals, และการแปลงระหว่าง time และ frequency domains. ตัวอย่างที่กล่าวถึงในเอกสารนี้ ได้แก่ a low-pass และ a high-pass FIR filter, ตลอดจน Fourier fast transforms ด้วย floating และ fixed point ที่ความถี่ที่ต่างกัน.
เฟิร์มแวร์ที่เกี่ยวข้อง (X-CUBE-DSPDEMO) ซึ่งใช้ได้กับ STM32F429xx and STM32F746xx MCUs, สามารถปรับให้เข้ากับ STM32 microcontroller รุ่นใดๆก็ได้.
ปล. ลองโหลดเฟิร์มแวร์ X-CUBE-DSPDEMO มาดูกันก่อน
เรื่อง FFT ลองดูที่นี่ก็ได้ https://slideplayer.in.th/slide/1963070/ (https://slideplayer.in.th/slide/1963070/)
ยังไงนะ ถ้าเป็น DFT จะการคูณเป็น O(N^2) แต่ถ้าเป็น FFT ก็จะลดรูปเหลือ O(Nlog2N) ซึ่งจะลดลงมามาก
ลองกูเกิ้ลศึกษากันดูเอานะครับ
-
Digital Signal Processing (DSP) คือ the mathematical manipulation และ processing of signals. Signals ที่จะถูกประมวลผลมาใน various physical formats ที่รวมถึง audio, video หรือ any analog signal ที่นำพา information, อย่างเช่น the output signal of a microphone.
ทั้ง Cortex®-M4-based STM32F4 Series และ Cortex®-M7-based STM32F7 Series จัดให้มี instructions สำหรับ signal processing, และรองรับ advanced SIMD (Single Instruction Multi Data) และ Single cycle MAC (Multiply and Accumulate) instructions.
การใช้ STM32 MCUs ใน a real-time DSP application ไม่เฉพาะลด cost, แต่ยังลด the overall power consumption อีกด้วย.
เอกสารต่อไปนี้ถูกพิจารณาว่าเป็นตัวอ้างอิง :
• PM0214, “STM32F3 and STM32F4 Series Cortex®-M4 programming manual”, มีให้ใช้ประโยชน์บน www.st.com
• PM0253, “STM32F7 Series Cortex®-M7 programming manual”, มีให้ใช้ประโยชน์บน www.st.com
• CMSIS - Cortex® Microcontroller Software Interface Standard, มีให้ใช้ประโยชน์บน www.arm.com
• Arm® compiler toolchain Compiler reference, มีให้ใช้ประโยชน์บน http://infocenter.arm.com
• “Developing Optimized Signal Processing Software on the Cortex®-M4 Processor”, หนังสือ technical โดย Shyam
Sadasivan, มีให้ใช้ประโยชน์บน www.techonline.com.
-
1 Basic DSP notions
1.1 Data types
DSP operations สามารถใช้อย่างใดอย่างหนึ่ง floating-point หรือ fixed-point formats.
1.1.1 Floating point
Floating point เป็นวิธีหนึ่งที่แสดงจำนวนจริง.
The floating point unit ใน the Cortex®-M4 มีเฉพาะ single precision, ดังที่มันประกอบด้วยฟิลด์เลขชี้กำลัง 8-bit และ a 23-bit fraction(เศษ), สำหรับทั้งหมดของ 32 bits (ดู Figure 1). The floating point unit ใน the Cortex®-M7 รองรับทั้ง single และ double precision, ดังแสดงใน Figure 2.
Value = (-1)s x M x 2^(E-127), หรือ Value = (-1)s x M x 2^(E-1023)
ที่ S คือค่าของ the sign bit, M คือค่าของเลขที่อยู่ทางด้านขวาของจุดทศนิยม, และ E คือค่าของตัวเลขชี้กำลัง.
(https://i.imgur.com/9PKDN0h.png)
ปล. ดูรูปแบบ floating point number ในนี้ก็ได้
https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/ (https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/)
วิธีการทำงานลึกๆเราก็รู้เอาไว้นิดหน่อยก็ได้
-
1.1.2 Fixed point
Fixed point representation แสดงจำนวนที่มีส่วนจำนวนเต็มและส่วนจำนวนเศษ, ใน a 2-complement format. ตัวอย่างเช่น, a 32-bit fixed point representation, แสดงใน Figure 3, จัดสรร(แบ่งส่วน) 24 bits สำหรับ the integer part และ 8 bits สำหรับ the fractional part.
(https://i.imgur.com/z1pP5Rm.png)
Available fixed-point data sizes ใน Cortex®-Mx cores คือ 8-, 16- and 32-bit.
The most common format ถูกใช้สำหรับ DSP operations คือ Q7, Q15 and Q31, ที่มีเฉพาะบิตเศษที่แสดงจำนวนระหว่าง -1.0 และ + 1.0.
การแสดงของ a Q15 number คือ:
(https://i.imgur.com/V2JImsz.png)
โดยที่ bs คือ the sign bit (the 15th bit), และ bn คือตัวเลขสำหรับ bit n.
ย่านของจำนวนที่ถูกรองรับใน a Q15 number ถูกประกอบด้วยระหว่าง -1.0 และ 1.0, ตรงกันกับจำนวนเต็มที่น้อยสุดและมากสุดที่สามารถถูกแสดงเป็น, -32768 และ 32767 ตามลำดับ.
ตัวอย่างเช่น, จำนวน 0.25 จะถูกเข้าระหัสใน Q15 เป็น 0x2000(8192).
เมื่อดำเนินการทำงานบน fixed-point สมการจะเป็นดังนี้ :
c = a <operand> b
โดยที่ a, b and c เป็นจำนวน fixed-point ทั้งหมด, และ <operand> หมายถึงการบวก การลบ การคูณ หรือการหาร สมการนี้ยังคงเป็นจริงสำหรับตัวเลขทศนิยมเช่นกัน.
Note: ต้องใช้ความระมัดระวังเมื่อดำเนินการกับ fixed-point numbers.
ตัวอย่างเช่น, ถ้า c = a x b ที่มี a และ b อยู่ในรูป Q31 format, นี้จะนำมาซึ่งผลลัพธ์ที่ไม่ถูกต้อง เนื่องจาก the compiler จะถือว่าการดำเนินการนี้เป็น an integer operation, เพราะเหตุนี้มันจะสร้าง “muls a, b” และจะเก็บเฉพาะ the least significant 32 bits ของผลลัพธ์.
-
ไม่ต้องอ่านทั้งหมดก็ได้ ดูแค่การคุณก่อนจุดทศนิยมกับหลังจุดทศนิยมก็ได้ แล้วจะเข้าใจ fixed point มากขึ้น
https://www.sciencedirect.com/topics/computer-science/fixed-point-number (https://www.sciencedirect.com/topics/computer-science/fixed-point-number)
แต่ถ้าเป็น Q7, Q15, Q31 ค่าจะอยู่ระหว่าง -1.0 ถึง 1.0
-
1.1.3 Fixed-point vs. floating-point
Table 1 เน้นถึงข้อดีและข้อเสียหลัก ๆ ของ fixed-point vs. floating-point ใน DSP applications.
(https://i.imgur.com/qxlZVx5.png)
-
2 Cortex® DSP instructions
The Cortex®-Mx cores มีคุณลักษณะคำสั่งมากมายที่ส่งผลในการนำไปใช้งานอย่างมีประสิทธิภาพของ DSP algorithms.
2.1 Saturation instructions
คำสั่งการทำให้อิ่มตัว(เต็ม), การบวกและการลบมีให้ใช้สำหรับค่า 8-, 16- and 32-bit, คำสั่งเหล่านี้บางส่วนถูกแสดงรายการใน Table 2.
(https://i.imgur.com/hkWdEin.png)
The SSAT (Signed SATurate) instruction ถูกใข้เพื่อปรับขนาดและทำให้อิ่มตัวของค่าที่มีเครื่องหมายไปยังตำแหน่งบิตใดๆ, โดยสามารถเลือกกการเลื่อนก่อนการทำให้อิ่มตัว.
ปล. การอิ่มตัว อย่างการบวกค่า 8 บิตแล้วได้ค่า 260 ซึ่งมันเกินค่าของ 8 บิต ก็จะเอาค่าแค่ 256 เท่านั้นใช่ไหม ค่าที่เกินก็ตัดทิ้งไป
-
2.2 MAC instructions
Multiply ACcumulate (MAC) instructions ถูกใช้อย่างกว้างขวางใน DSP algorithms, อย่างในกรณีของ the Finite Impulse Response (FIR) และ Infinite Impulse Response (IIR).
การปฏิบัติ multiplication and accumulation ใน single cycle instruction เป็น a key requirement สำหรับการสำเร็จ high performance.
ตัวอย่างต่อไปนี้อธิบายว่า the SMMLA (Signed Most significant word MuLtiply Accumulate) instruction ทำงานอย่างไร.
(https://i.imgur.com/KPlN1yo.png)
-
2.3 SIMD instructions
นอกเหนือจาก MAC instructions ที่ปฏิบัติ a multiplication and an accumulation ใน a single cycle, ยังมี the SIMD (Single Instruction Multiple Data) instructions, ที่กระทำ multiple identical operations ใน a single cycle instruction.
Table 3 แสดงรายการบาง SIMD instructions.
(https://i.imgur.com/bOrM9yP.png)
ตัวอย่างต่อไปนี้อธิบายว่า the __shadd8 instruction ทำงานอย่างไร.
(https://i.imgur.com/8XmNEZl.png)
The __shadd8 การคืนกลับที่แท้จริง:
• การบวกที่แบ่งเท่าๆกันของไบต์แรกจากแต่ละ operand, ในไบต์แรกของค่าคืนกลับ
• การบวกที่แบ่งเท่าๆกันของไบต์ที่สองจากแต่ละ operand, ในไบต์ที่สองของค่าคืนกลับ
• การบวกที่แบ่งเท่าๆกันของไบต์ที่สามจากแต่ละ operand, ในไบต์ที่สามของค่าคืนกลับ
• การบวกที่แบ่งเท่าๆกันของไบต์ที่สี่จากแต่ละ operand, ในไบต์ที่สี่ของค่าคืนกลับ
-
3 Algorithms
3.1 Filters
ตัวกรองดิจิทัลที่พบบ่อยที่สุดคือ:
• FIR (Finite Impulse Response): ถูกใช้, ท่ามกลาง, ใน motor control และ audio equalization
• IIR (Infinite Impulse Response): ถูกใช้ใน smoothing data
The IIR filter สามารถถูกใช้เพื่อจัดให้มีใช้ filters อย่างเช่น Butterworth, Chebyshev, และ Bessel.
3.2 Transforms
A transform เป็น a function ที่แปลง data จาก a domain หนึ่งไปเป็นโดเมนอื่น.
The FFT (Fast Fourier Transform) เป็นตัวอย่างทั่วไป: มันเป็น an efficient algorithm ที่ถูกใช้แปลง a discrete time-domain signal ไปเป็น an equivalent frequency-domain signal โดยอ้างอิง the Discrete Fourier Transform (DFT).
-
4 DSP application development
4.1 CMSIS library
The Arm® Cortex® Microcontroller Software Interface Standard (CMSIS) คือ a vendor-independent hardware abstraction layer สำหรับทุก Cortex® processor based devices. CMSIS ถูกพัฒนาโดย Arm® ร่วมกับ silicon, tools and middleware partners.
แนวคิดเบื้องหลัง CMSIS คือการจัดเตรียมอินเทอร์เฟซซอฟต์แวร์ที่สม่ำเสมอและเรียบง่ายให้กับ the processor สำหรับ interface peripherals, real-time operating systems, และ middleware, การใช้ software ซ้ำง่าย, ลดเคิร์พการเรียนรู้สำหรับ new microcontroller developments และลดเวลาการวางตลาดสำหรับ new devices.
CMSIS library มากับ ST firmware ภายใต้ \Drivers\CMSIS\.
The CMSIS-DSP library ประกอบด้วย :
• Basic mathematical functions พร้อมด้วย vector operations
• Fast mathematical functions, อย่าง sine and cosine
• Complex mathematical functions อย่าง calculating magnitude (มีค่า real + imagin อย่าง (1+j2))
• Filtering functions อย่าง FIR or IIR
• Matrix computing functions
• Transform functions อย่าง FFT
• Controller functions อย่าง PID controller
• Statistical functions(สถิติ) อย่าง calculating minimum or maximum
• Support functions อย่างการแปลงจากรูปแบบหนึ่งไปเป็นรูปแบบอื่น
• Interpolation functions
algorithms ส่วนใหญ่ใช้ floating-point and fixed-point ในรูปแบบต่างๆ ตัวอย่างเช่น ในกรณี FIR, the available Arm® functions คือ :
• arm_fir_init_f32
• arm_fir_f32
• arm_fir_init_q31
• arm_fir_q31
• arm_fir_fast_q31
• arm_fir_init_q15
• arm_fir_q15
• arm_fir_fast_q15
• arm_fir_init_q7
• arm_fir_q7
-
4.2 DSP demonstration overview
เป้าหมายของการสาธิตนี้คือการแสดงการผสานรวมอย่างเต็มรูปแบบด้วย STM32F429 โดยใช้ ADC, DAC, DMA และ timers, และยังเรียกใช้ CMSIS routines, ทั้งหมดด้วยการใช้ graphics, โดยใช้ประโยชน์จาก the 2.4" QVGA TFT LCD ที่รวมอยู่ใน the discovery board.
การสาธิตนี้ยังแสดงให้เห็นว่าการย้ายแอปพลิเคชันจาก an STM32F4 microcontroller ไปยังตัวหนึ่งของ the STM32F7 Series ง่ายเพียงใด.
A graphical user interface ถูกออกแบบโดยใช้ STemWin, เพื่อง่ายต่อการเข้าถึงไปยังคุณลักษณะต่างๆของ the demonstration.