STM32F1 DSP

Started by tha, June 28, 2021, 09:39:56 AM

Previous topic - Next topic

tha

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/
       ยังไงนะ ถ้าเป็น DFT จะการคูณเป็น O(N^2) แต่ถ้าเป็น FFT ก็จะลดรูปเหลือ O(Nlog2N) ซึ่งจะลดลงมามาก
       ลองกูเกิ้ลศึกษากันดูเอานะครับ

tha

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.

tha

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 คือค่าของตัวเลขชี้กำลัง.



ปล. ดูรูปแบบ floating point number ในนี้ก็ได้
      https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/
      วิธีการทำงานลึกๆเราก็รู้เอาไว้นิดหน่อยก็ได้


tha

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.



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 คือ:



โดยที่ 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 ของผลลัพธ์.

tha

ไม่ต้องอ่านทั้งหมดก็ได้ ดูแค่การคุณก่อนจุดทศนิยมกับหลังจุดทศนิยมก็ได้ แล้วจะเข้าใจ fixed point มากขึ้น
https://www.sciencedirect.com/topics/computer-science/fixed-point-number
แต่ถ้าเป็น Q7, Q15, Q31 ค่าจะอยู่ระหว่าง -1.0 ถึง 1.0

tha

1.1.3 Fixed-point vs. floating-point

Table 1 เน้นถึงข้อดีและข้อเสียหลัก ๆ ของ fixed-point vs. floating-point ใน DSP applications.



tha

2 Cortex® DSP instructions

The Cortex®-Mx cores มีคุณลักษณะคำสั่งมากมายที่ส่งผลในการนำไปใช้งานอย่างมีประสิทธิภาพของ DSP algorithms.

2.1 Saturation instructions

คำสั่งการทำให้อิ่มตัว(เต็ม), การบวกและการลบมีให้ใช้สำหรับค่า 8-, 16- and 32-bit, คำสั่งเหล่านี้บางส่วนถูกแสดงรายการใน Table 2.



The SSAT (Signed SATurate) instruction ถูกใข้เพื่อปรับขนาดและทำให้อิ่มตัวของค่าที่มีเครื่องหมายไปยังตำแหน่งบิตใดๆ,  โดยสามารถเลือกกการเลื่อนก่อนการทำให้อิ่มตัว.

ปล. การอิ่มตัว อย่างการบวกค่า 8 บิตแล้วได้ค่า 260 ซึ่งมันเกินค่าของ 8 บิต ก็จะเอาค่าแค่ 256 เท่านั้นใช่ไหม ค่าที่เกินก็ตัดทิ้งไป

tha

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 ทำงานอย่างไร.