Search

CN-122026859-A - Efficient sliding window median filtering method, system and application for microcontroller

CN122026859ACN 122026859 ACN122026859 ACN 122026859ACN-122026859-A

Abstract

The invention discloses a microcontroller-oriented efficient sliding window median filtering method, a system and application, wherein the method comprises the following steps: the system comprises a memory, a microcontroller and an ADC module. The method has extremely high calculation efficiency, and avoids full sequencing when the window slides each time by maintaining the ordered array in an increment mode. Experiments show that on a typical MCU, when the window size is 9, the time consumption of single filtering can be reduced by more than 60% compared with the traditional full insertion ordering, and the time consumption can reach microsecond level. And the excellent pulse noise resistance of the median filtering is maintained, outliers can be effectively removed, and meanwhile, the real abrupt change response of signals is rapid, no excessive smoothness is realized, and the excellent filtering effect is ensured.

Inventors

  • ZHANG XIAOJUN
  • YANG DONGSHENG

Assignees

  • 深圳锐特机电技术有限公司

Dates

Publication Date
20260512
Application Date
20260116

Claims (10)

  1. 1. The efficient sliding window median filtering method for the microcontroller is characterized by comprising the following steps of: S1, in an initialization stage, continuous k initial sampling values are collected and stored in a preset original window buffer area W, the initial sampling values are sequenced and then stored in a preset ordered buffer area S, wherein k is an odd number larger than 1; S2, in a sliding updating stage, when a new sampling value is obtained, incremental updating operation is executed; And S3, in the output stage, directly outputting a median filtering result of the current window according to the element of the k/2 bit in the current ordered buffer area S.
  2. 2. The method of claim 1, wherein the original window buffer W and the ordered buffer S are integer arrays of fixed length that are statically allocated during compilation.
  3. 3. The method for efficient sliding window median filtering for microcontrollers in accordance with claim 2, wherein the original window buffer W is managed using a circular buffer mechanism, and the write index is updated by modulo arithmetic to cover the oldest sample value, avoiding the entire movement of data.
  4. 4. The method for efficient sliding window median filtering for microcontrollers according to claim 1, wherein said performing incremental update operations in step S2 specifically comprises: s21, determining an old sampling value to be removed from the original window buffer area; s22, positioning and removing the old sampling value in the ordered buffer; S23, inserting the new sampling value into the correct position of the ordered buffer area in sequence so as to maintain the monotonicity of the ordered buffer area; And S24, writing the new sampling value into the original window buffer area, and updating the writing index of the original window buffer area.
  5. 5. The efficient sliding window median filtering method for microcontrollers according to claim 1, wherein in steps S22 and S23, the search and insertion strategy is adaptively selected according to the window size k: When k is less than or equal to the threshold 7, the old sampling value is linearly searched and positioned, and the new sampling value is inserted by adopting a linear comparison and shift mode; when k > threshold 7, locating the old sampling value by adopting binary search, and inserting the new sampling value by adopting a memory block moving function.
  6. 6. The method of claim 1, wherein the method is invoked in an interrupt service routine of an analog-to-digital converter ADC, and the sampled value is an integer quantized value of a voltage or current signal collected by the ADC.
  7. 7. The microcontroller-oriented efficient sliding window median filtering method according to claim 6, wherein the method is configured to process a plurality of independent signal channels in parallel, each channel independently maintaining a set of the raw window buffer W and the ordered buffer S.
  8. 8. A high efficiency sliding window median filtering system for a microcontroller, applied to the method of any one of claims 1-7, the system comprising: A memory for storing executable instructions and a static array as an original window buffer W and an ordered buffer S; And a microcontroller for executing the executable instructions to perform the operations of the initialization phase, the sliding update phase and the output phase.
  9. 9. The microcontroller-oriented high-efficiency sliding window median filtering system of claim 8, further comprising an analog-to-digital converter ADC module coupled to the processor for collecting voltage or current analog signals and converting them to integer sample values for supply to the processor.
  10. 10. Use of the efficient sliding window median filtering method for microcontrollers according to any one of claims 1 to 7 in denoising embedded system sensor signals, wherein said use comprises: the method is used for filtering the voltage signal or the current signal acquired by the analog-to-digital converter ADC in real time so as to inhibit impulse noise; the embedded system comprises battery power supply equipment, an industrial sensor node or intelligent metering equipment; The application specifically comprises any one of the following scenes: the battery electric quantity monitoring, namely filtering the voltage sample of the lithium battery to eliminate jump caused by contact shake; filtering MOSFET switching noise in FOC control, and improving the stability of a current loop; the intelligent ammeter is used for carrying out anti-pulse processing on the voltage/current signal before power calculation; And the reliability of analog signal transmission is improved in an industrial 4-20mA transmitter.

Description

Efficient sliding window median filtering method, system and application for microcontroller Technical Field The invention relates to the technical field of embedded systems and sensor signal processing, in particular to a microcontroller-oriented efficient sliding window median filtering method, a system and application. Background In embedded systems, analog signals such as voltage and current are usually collected by an ADC (analog-to-digital converter), and are susceptible to electromagnetic interference, contact jitter, or power supply fluctuations, and generate instantaneous outliers (i.e., impulse noise). The traditional linear filtering (such as average filtering) has poor noise suppression effect, and the median filtering can effectively remove outliers due to the nonlinear characteristic of the traditional linear filtering, and meanwhile, the signal mutation characteristics (such as switching action and load switching) are well reserved, so that the traditional linear filtering is widely applied to the fields of power monitoring, motor control, battery management and the like. However, implementing median filtering on a typical micro MCU (e.g., ARMCortex-M0/M3, ESP8266, STM8, etc.), RAM <32KB, no floating point unit, dominant frequency <100 MHz) faces serious challenges: The computational resource is limited, if full-quantity sequencing (such as insertion sequencing and bubbling sequencing) is adopted for each window sliding, the time complexity is O (k 2), when the window size is k=9-21, the single filtering time can be up to several microseconds to tens of microseconds, and the CPU load is obviously increased; Memory is severely limited in that dynamic data structures (e.g., heap, balanced binary tree) or Standard Template Library (STL) cannot be used and runtime memory allocation needs to be avoided; the real-time requirement is high, the filtering is often completed in an ADC Interrupt Service Routine (ISR), and the determination of the execution time and low delay are required to be ensured; The power consumption is sensitive, and frequent data movement and branch jump can increase the energy consumption and shorten the service life of the battery. The existing embedded solution mostly adopts simple full-order or sacrifices filtering performance to change mean filtering, and is difficult to consider real-time performance, robustness and resource efficiency. Disclosure of Invention Technical problem to be solved Aiming at the defects of the prior art, the invention provides a high-efficiency median filtering method with low memory, low delay and deterministic execution, which is suitable for real-time denoising of integer sampling signals such as voltage, current and the like. Technical proposal In order to achieve the purpose, the invention provides the technical scheme that the efficient sliding window median filtering method for the microcontroller comprises the following steps of: S1, in an initialization stage, continuous k initial sampling values are collected and stored in a preset original window buffer area W, and the initial sampling values are sequenced and then stored in a preset ordered buffer area S, wherein k is an odd number larger than 1; S2, in a sliding updating stage, when a new sampling value is obtained, incremental updating operation is executed; S3, in the output stage, according to the element of the 'k/2' bit in the current ordered buffer S, directly outputting the median filtering result of the current window. Further, the original window buffer W and the ordered buffer S are integer arrays of fixed length that are statically allocated during compilation. Furthermore, the original window buffer area W is managed by adopting a circular buffer mechanism, and the written index is updated by modulo operation so as to cover the oldest sampling value and avoid the whole movement of data. Further, the performing the incremental update operation in step S2 specifically includes: s21, determining an old sampling value to be removed from an original window buffer area; s22, positioning and removing old sampling values in an ordered buffer; S23, inserting new sampling values into the correct positions of the ordered buffer areas in sequence so as to maintain the monotonicity of the ordered buffer areas; and S24, writing the new sampling value into the original window buffer area, and updating the writing index of the original window buffer area. Further, in steps S22 and S23, the search and insert strategy is adaptively selected according to the window size k: When k is less than or equal to the threshold 7, the old sampling value is linearly searched and positioned, and the new sampling value is inserted by adopting a linear comparison and shift mode; when k > threshold 7, locating the old sampling value by adopting binary search, and inserting the new sampling value by adopting a memory block moving function. Further, the method is invoked in an interrupt service routine of t