English below …

1. Giới thiệu

Trong các hệ thống đo lường và điều khiển tự động, việc sử dụng vi điều khiển hoặc PLC để đọc tín hiệu analog từ các loại cảm biến như cảm biến áp suất, nhiệt độ, khoảng cách, dòng điện… là rất phổ biến. Tuy nhiên, quá trình thu thập và xử lý tín hiệu analog không tránh khỏi vấn đề nhiễu (noise), xuất hiện từ nhiều nguyên nhân khác nhau.

Nguồn nhiễu có thể đến từ:

  • Nhiễu điện từ (EMI) do biến tần, động cơ, relay hoạt động gần khu vực đo.
  • Sai số ADC (Analog-to-Digital Converter) của chính vi điều khiển hoặc PLC, độ phân giải hạn chế.
  • Chênh lệch điện áp giữa cảm biến và mạch đọc, gây ra nhiễu nền (offset noise).
  • Biến đổi môi trường như nhiệt độ, độ ẩm, hoặc nhiễu cơ học (rung động, va chạm).

Tất cả những yếu tố này đều làm cho tín hiệu analog dao động không ổn định, dẫn đến sai số trong đo đạc và điều khiển. Để khắc phục, nhiều phương pháp lọc tín hiệu được áp dụng, trong đó phổ biến nhất là Bộ Lọc Trung Bình Trượt (Moving Average Filter). Bộ lọc này giúp làm mượt tín hiệu, giảm ảnh hưởng của nhiễu ngẫu nhiên, nhưng đồng thời cũng tạo ra độ trễ trong phản hồi tín hiệu đầu ra.

Bài viết này sẽ đi sâu phân tích ảnh hưởng của số mẫu trung bình đến khả năng lọc nhiễu và độ trễ tín hiệu, từ đó giúp lựa chọn cấu hình tối ưu cho từng ứng dụng cụ thể.

2. Phương Pháp Lọc Trung Bình

2.1. Lọc trung bình khối (Block Averaging)

  • Thu thập N mẫu liên tiếp, tính trung bình, lưu kết quả.
  • Sau khi lưu, loại bỏ toàn bộ N mẫu cũ và tiếp tục với nhóm N mẫu mới.

Ưu điểm:

  • Giá trị trung bình độc lập từng đợt, giúp giảm nhiễu hiệu quả.

Nhược điểm:

  • Cập nhật kết quả chậm, vì chỉ tính trung bình sau mỗi nhóm N mẫu.
  • Khi có thay đổi tín hiệu đột ngột, có thể gây hiệu ứng “giật”.

2.2. Lọc trung bình trượt (Moving Average)

  • Duy trì N mẫu gần nhất trong bộ nhớ.
  • Mỗi khi có 1 mẫu mới, loại bỏ 1 giá trị cũ nhất trong N mẫu
  • Tính trung bình lại sau mỗi lần cập nhật tín hiệu mới.

Ưu điểm:

  • Tín hiệu mượt mà, cập nhật liên tục ( theo vòng quét của chương trình )
  • Phản hồi nhanh hơn so với lọc trung bình khối.

Nhược điểm:

  • Cần bộ nhớ để lưu trữ N mẫu gần nhất.
  • Mỗi chu kỳ cần tính toán lại giá trị trung bình, độ trễ cao hơn Lọc Trung Bình Khối.

Mỗi phương pháp lọc đều có những ưu điểm và nhược điểm riêng, tùy vào từng ứng dụng cụ thể chúng ta sẽ chọn phương pháp lọc phù hợp.

3. Ảnh Hưởng của Bộ Lọc Trung Bình tới Độ Trễ của Chương Trình

Khi bạn dùng bộ lọc trung bình trượt (Moving Average), độ trễ xảy ra vì mỗi giá trị đầu ra là trung bình của N giá trị trước đó. Điều này làm tín hiệu trở nên mượt hơn nhưng phản ứng chậm hơn với thay đổi đột ngột.

  • Nếu số Mẫu nhỏ → tín hiệu cập nhật nhanh nhưng lọc nhiễu kém.
  • Nếu số Mẫu lớn → tín hiệu mượt hơn nhưng có độ trễ cao.

3.1 Tính Lọc Trung Bình Trượt (Moving Average)


Ví dụ: So sánh độ trễ của cảm biến có sai số 5 đơn vị analog ( 5 bước A/D ) với 10 mẫu và 25 mẫu.

Lọc trung bình trượt giúp giảm nhiễu ngẫu nhiên theo công thức:

equation

Trong đó:

  • equation là độ lệch chuẩn nhiễu đầu vào .
  • N là số mẫu.

NĐộ lệch chuẩn sau lọc equation
10equation
25equation

→ Lọc 25 mẫu giúp giảm nhiễu hiệu quả hơn 10 mẫu (~37% ít nhiễu hơn).

3.2 So Sánh Ảnh Hưởng Của Số Mẫu Trung Bình Đến Độ Trễ

Khi tăng số mẫu N, tín hiệu đầu ra sẽ mượt hơn nhưng có độ trễ cao hơn. Độ trễ trung bình có thể ước lượng bằng công thức:

equation

Với ( PLC lấy mẫu mỗi 1ms), ta có:

Số mẫuĐộ trễ trung bình
104.5ms
2512ms

➡ Nếu dùng 25 mẫu, tín hiệu sẽ có độ trễ 12ms, cao hơn đáng kể so với 4.5ms khi dùng 10 mẫu.

4. Ứng Dụng và Lựa Chọn

Tiêu chíN = 10N = 25
Lọc nhiễuTốtTốt hơn rõ rệt
Độ trễ phản hồi4.5ms12ms
Phù hợp với tín hiệu thay đổi nhanh
Phù hợp với tín hiệu ổn định, nhiễu nhiều

5. Giải Pháp Cân Bằng Giữa Độ Trễ và Độ Mượt

Nếu cần giảm nhiễu tốt nhưng vẫn giữ độ trễ thấp, có thể dùng:

  • EMA (Exponential Moving Average): Lọc mượt hơn với ít độ trễ hơn Moving Average.
  • Bộ lọc Kalman hoặc Low-pass Filter số: Phù hợp với hệ thống có mô hình xác định.

Việc lựa chọn N trong bộ lọc trung bình cần cân nhắc giữa độ mượtđộ trễ của tín hiệu. Nếu yêu cầu phản hồi nhanh, nên chọn . Nếu cần giảm nhiễu tốt hơn, chọn . Trong các trường hợp đặc biệt, nên xem xét các bộ lọc tiên tiến hơn như EMA hoặc Kalman.


1. Introduction

In measurement and automatic control systems, using microcontrollers or PLCs to read analog signals from various sensors—such as pressure, temperature, distance, or current sensors—is very common. However, during the signal acquisition and processing stages, analog signals are often affected by noise caused by various factors.

Common noise sources include:

  • Electromagnetic interference (EMI) from inverters, motors, or relays operating near the measurement area.
  • ADC (Analog-to-Digital Converter) errors of the microcontroller or PLC due to limited resolution.
  • Voltage differences between the sensor and the reading circuit, which can cause offset noise.
  • Environmental changes such as temperature, humidity, or mechanical noise (vibrations, impacts).

All these factors can cause analog signals to fluctuate and become unstable, leading to inaccuracies in measurement and control. To address this, several signal filtering methods are applied, with the most popular being the Moving Average Filter. This filter smooths the signal and reduces the effect of random noise, although it also introduces a delay in the output signal’s response.

This article will analyze the impact of the number of averaged samples on noise reduction and signal delay, helping users select the optimal configuration for specific applications.

2. Averaging Filter Methods

2.1. Block Averaging

  • Collect N consecutive samples, compute the average, and store the result.
  • After storing, discard all N old samples and proceed with a new group of N samples.

Advantages:

  • The average value is independent for each batch, effectively reducing noise.

Disadvantages:

  • Result updates are slow, as the average is only calculated after every N samples.
  • Sudden signal changes may cause a “jerky” effect.

2.2. Moving Average

  • Maintain the most recent N samples in memory.
  • Each time a new sample is acquired, discard the oldest sample.
  • Recalculate the average after each new sample.

Advantages:

  • Smooth, continuously updated signal (in sync with the program scan cycle).
  • Faster response than block averaging.

Disadvantages:

  • Requires memory to store the most recent N samples.
  • Each cycle needs to recalculate the average, causing more delay compared to block averaging.

Each filtering method has its own strengths and weaknesses. The choice depends on the specific application.

3. Impact of Moving Average Filters on Program Delay

When using a Moving Average filter, delay occurs because each output value is the average of the previous N input values. While this smooths the signal, it also makes the response slower to sudden changes.

  • Small N → Fast signal updates but poor noise filtering.
  • Large N → Smoother signal but higher delay.

3.1. Noise Reduction by Moving Average

Example: Compare the delay when filtering a sensor with ±5 analog units error (5 ADC steps), using 10 samples vs. 25 samples.

Moving average reduces random noise based on this formula:

equation

Where:

  • equation ​ is the standard deviation of input noise.
  • N is the number of samples.
NFiltered standard deviation equation
10equation
25equation

→ Filtering with 25 samples reduces noise more effectively than with 10 samples (~37% less noise).

3.2. Comparing Delay Based on Number of Samples

As N increases, the output becomes smoother, but the average delay increases. The approximate average delay can be estimated using:

equation

Assuming the PLC samples every 1ms:

Number of SamplesAverage Delay
10 samples4.5 ms
25 samples12 ms

➡ Using 25 samples introduces a 12 ms delay, significantly higher than the 4.5 ms delay when using 10 samples.

4. Application and Selection

CriteriaN = 10N = 25
Noise filteringGoodSignificantly better
Response delay4.5 ms12 ms
Suitable for rapidly changing signals
Suitable for stable but noisy signals

5. Balancing Delay and Smoothness

If you require good noise filtering but want to minimize delay, consider:

  • EMA (Exponential Moving Average): Smoother signal with less delay compared to the Moving Average.
  • Kalman Filter or Digital Low-pass Filter: Suitable for systems with a defined model.

Choosing the number of samples N in an average filter involves balancing between signal smoothness and response delay. For fast response, select a smaller N. For better noise suppression, select a larger N. For special cases, advanced filters such as EMA or Kalman should be considered.