weighted recursive average filtering method

The weighted recursive average filtering method is a digital filtering method. Its principle is to weight the current sample value and the previous sample values ​​to obtain the filtering result.

Specifically, assuming that the current sample value is x(n) and the previous k sample values ​​are x(n-1), x(n-2),...,x(n-k), then the calculation of the weighted recursive average filtering method The formula is:

y(n) = (1-a)x(n) + a*y(n-1)

Among them, y(n) is the filtered result, and a is the filter coefficient, usually ranging from 0.8 to 0.99. According to the calculation formula, it can be seen that the filtering result is the weighted average of the current sample value and the previous filtering result.

Since the weighted recursive average filtering method considers the weighted average of multiple sample values, it has a better suppression effect on periodic noise. At the same time, since only the previous filtering result needs to be saved, the storage space is small, which is suitable for use in resource-limited environments such as embedded systems.

Insert image description here

1. C implementation of weighted recursive average filtering method and detailed code explanation

Weighted Recursive Averaging Filter is a filtering method commonly used in digital signal processing. It smoothes a continuous signal. This method can effectively eliminate noise and undesirable fluctuations in the signal, making the signal more accurate and stable.

Implementation principle

The weighted recursive average filtering method uses a fixed-length sliding window (N data). Each time new data is input, the data in the window is weighted and averaged according to a certain weight, and the average value is output. The next time new data is entered, the window moves one position to the right, the new data is added to the end of the window, and the front end of the window is discarded. In this way, the signal is continuously smoothed to achieve noise reduction.

The specific calculation formula of the weighted recursive average filtering method is as follows:

Y k = ( 1 − α ) Y k − 1 + α X k Y_k = (1-\alpha)Y_{k-1}+\alpha X_kANDk=(1α)Yk1+αXk

In that, X k X_k Xk is the currently input data, Y k Y_k ANDk is the average value of the output, Y k − 1 Y_{k-1} ANDk1 is the last average value, α \alpha α is the smoothing coefficient, 0 < α < 1 0<\alpha<1 0<a<1. Actually, α \alpha α and the length of the sliding window N N N can be converted into each other, that is, α = 2 / ( N + 1 ) \alpha=2/(N+1) a>a=2/(N+1)

Code

The following is an example code that implements the weighted recursive average filtering method in C language:

#define N 10  // 窗口长度

float weighted_recursive_averaging_filter(float x)
{
    
    
    static float sum = 0;  // 窗口内数据的和
    static float buf[N];   // 窗口数据缓存
    static int index = 0;  // 窗口当前位置

    // 新数据加入
    sum -= buf[index];    // 减去最早的数据
    sum += x;             // 加入新的数据
    buf[index] = x;       // 存储新的数据

    // 窗口移动
    index++;
    if (index >= N) {
    
    
        index = 0;
    }

    // 加权平均
    return sum * 2 / (N + 1);
}

In the above code, we use a static variable to save the sum of the data in the window and the current position. Each time new data is entered, the oldest data is subtracted from the sum, then the new data is added to the sum and stored in the window's cache. Afterwards, the window is moved one position to the right, and if the end of the window has reached the end of the array, the position is reset to 0. Finally, the results are output by calculating the weighted average of the data within the window.

Instructions

When using the weighted recursive average filtering method, the length of the window and the smoothing coefficient need to be determined. Generally speaking, the longer the window length, the better the smoothing effect, but the slower the response speed will be; the larger the smoothing coefficient, the better the smoothing effect, but it will lead to a larger translation amount.

Here is an example using weighted recursive average filtering:

#include <stdio.h>

float weighted_recursive_averaging_filter(float x);

int main()
{
    
    
    float data[] = {
    
    10, 11, 12, 13, 14, 15, 14, 18, 16, 17};
    int len = sizeof(data) / sizeof(float);

    printf("Original data: ");
    for (int i = 0; i < len; i++) {
    
    
        printf("%.1f ", data[i]);
    }
    printf("\n");

    printf("Filtered data: ");
    for (int i = 0; i < len; i++) {
    
    
        printf("%.1f ", weighted_recursive_averaging_filter(data[i]));
    }
    printf("\n");

    return 0;
}

In the above example, we input a raw set of data and then smoothed them. It can be seen that the output results are more stable and accurate than the original data.

Summarize

The weighted recursive average filtering method is a practical digital signal processing method that can effectively eliminate noise and undesirable fluctuations in the signal, making the signal more accurate and stable. In practical applications, the window length and smoothing coefficient can be adjusted as needed to adapt to different input signals.

Insert image description here

2. C++ implementation of weighted recursive average filtering method and detailed code explanation

The weighted recursive average filtering method is a digital signal processing method used to smooth noise signals to reduce the impact of noise. It adds weighting processing on the basis of the recursive average filtering method, making the filtering effect more stable. The following is the code and detailed explanation of the weighted recursive average filtering method in C++.

  1. Principle of weighted recursive average filtering method

The weighted recursive average filtering method adds weighting processing on the basis of the recursive average filtering method. Its principle is to perform weighted average processing on the current data, and then use the processing results as the predicted value of the next data. The specific process is as follows:

  • Initialization: Set the weight coefficient w and the initial value y(0).
  • Weighted average processing: Calculate the new predicted value y(k) based on the current data and the previous predicted value y(k-1). The weighted average calculation formula is: y(k) = w * x(k) + (1-w) * y(k-1), where x(k) is the current data, w is the weight coefficient, y(k -1) is the previous predicted value.
  • Iterative processing: Repeat the above steps until all data has been processed.
  1. C++ implementation of weighted recursive average filtering method code

The following is the code for implementing the weighted recursive average filtering method in C++:

#include <iostream>
#include <vector>

using namespace std;

vector<double> weighted_recursive_average_filter(vector<double> data, double w, double y0) {
    vector<double> result(data.size());
    double y = y0;
    for (int i = 0; i < data.size(); i++) {
        y = w * data[i] + (1 - w) * y;
        result[i] = y;
    }
    return result;
}

int main() {
    // 测试数据
    vector<double> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double w = 0.5;
    double y0 = 0;

    // 调用函数
    vector<double> result = weighted_recursive_average_filter(data, w, y0);

    // 输出结果
    for (double item : result) {
        cout << item << " ";
    }
    cout << endl;

    return 0;
}
  1. Detailed code explanation
  • Function weighted_recursive_average_filter: implements the weighted recursive average filtering method.
  • Parameter data: data to be processed.
  • Parameter w: weight coefficient, the value range is between 0 and 1.
  • Parameter y0: initial value, that is, the initial value of the predicted value.
  • Return value: Return the processed data.

In the function, a vector type variable result is first defined to store the processed data. Then a variable y is defined to store the predicted value. Then use a for loop to traverse the data to be processed. In the loop, the new predicted value y is calculated according to the weighted average formula and stored in result. Finally, result is returned as the processing result.

In the main function, the test data data, weight coefficient w and initial prediction value y0 are defined. Then call the weighted_recursive_average_filter function for processing and store the processing results in result. Finally, use a for loop to output the processing results.

  1. Summarize

The weighted recursive average filtering method is an effective digital signal processing method that can smooth noise signals and reduce the impact of noise. By using C++ to implement the code of the weighted recursive average filtering method, we can have a deeper understanding of the implementation principle of this method, so as to better apply it to practical problems.

Insert image description here

3. Java implementation of weighted recursive average filtering method and detailed code explanation

The weighted recursive average filtering method is a weighted-based average filtering method that can effectively remove noise in the signal and retain the main characteristics of the signal. The following is the code and detailed explanation of the weighted recursive average filtering method in Java.

Code:

public class WeightedRecursionAveragingFilter {
    
    
    private double lastResult = 0;
    private double lastData = 0;
    private double weight = 0.5;

    public double filter(double data) {
    
    
        double result = weight * data + (1 - weight) * lastResult;
        lastResult = result;
        lastData = data;
        return result;
    }

    public void setWeight(double weight) {
    
    
        this.weight = weight;
    }

    public double getLastData() {
    
    
        return lastData;
    }
}

Detailed explanation:

First, a classWeightedRecursionAveragingFilter is defined, which contains three member variables: lastResult, lastData and < /span> represents the weighting coefficient. represents the last input data, represents the result of the last filtering, weight. lastResultlastDataweight

Among them, thefilter method is the core method of filtering. It receives the current data and returns the filtered result. The specific implementation is as follows:

double result = weight * data + (1 - weight) * lastResult;
lastResult = result;
lastData = data;
return result;

The formula of the weighted recursive average filtering method is used here to calculate the filtered result. Save this time's results to lastResult for next time use.

The weighting coefficientweight can be set through the setWeight method, and the getLastData method can obtain the last input data.

Instructions:

WeightedRecursionAveragingFilter filter = new WeightedRecursionAveragingFilter();
filter.setWeight(0.6);
double result = filter.filter(data);

Create an WeightedRecursionAveragingFilter object, set the weighting coefficient through the setWeight method, then call the filter method to filter, and return the filter The final resultresult.

Finally, it should be noted that the weighted recursive average filtering method is a lag filter, which may cause a large lag for rapidly changing signals, so it needs to be selected according to actual needs.
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_47225948/article/details/133126449