Arithmetic mean filtering method

The arithmetic average filtering method is a commonly used signal filtering method. Its principle is to average a continuous segment of signal values ​​to reduce noise interference in the signal. The specific operation method is as follows:

  1. Select a continuous signal, usually 3-15 data points, called the window size.

  2. Calculate the average of all data points in the window, and use this average as the filtered output value.

  3. Move the window one position backward, that is, move the data points in the window one position to the right, and then repeat step 2 until the filtering is completed.

The arithmetic average filtering method is suitable for situations where the signal changes slowly and the noise is relatively small. It can effectively smooth the signal and reduce the noise. However, for rapidly changing signals or situations where the noise is relatively large, large errors may occur.
Insert image description here

1. C implementation of arithmetic mean filtering method and detailed code explanation

Arithmetic average filtering method is one of the commonly used smoothing filtering methods in the field of signal processing. Its basic idea is to take the average value of a certain number of surrounding sampling points for each sampling point in the signal as the output value of the point. This effectively removes noise and jitter from the signal while retaining the main characteristics of the signal.

The process of implementing the arithmetic average filtering method in C language is as follows:

  1. Define the size of the filter window: that is, take the average of several points on the left and right sides of each sampling point in the signal. Generally, the larger the window size, the better the filtering effect, but the amount of calculation will also increase.

  2. Read signal: Get signal data from external files or sensors.

  3. Filter each sampling point: Taking the current sampling point as the center, take the average value of the sampling points of the left and right window sizes as the output value of that point.

  4. Output filtered signal: Save the filtered signal to an external file or sensor output.

The following is an example of C language code for the arithmetic mean filtering method:

#include <stdio.h>

#define N 100 // 信号长度
#define W 5   // 窗口大小

int main()
{
    
    
    int signal[N]; // 输入信号
    int output[N]; // 输出信号
    int sum;       // 窗口内信号值的总和

    // 读入信号
    for (int i = 0; i < N; i++)
    {
    
    
        scanf("%d", &signal[i]);
    }

    // 对每个采样点进行滤波
    for (int i = 0; i < N; i++)
    {
    
    
        sum = 0;
        for (int j = i - W; j <= i + W; j++)
        {
    
    
            if (j >= 0 && j < N)
            {
    
    
                sum += signal[j];
            }
        }
        output[i] = sum / (2 * W + 1); // 取窗口内信号值的平均值
    }

    // 输出滤波后的信号
    for (int i = 0; i < N; i++)
    {
    
    
        printf("%d ", output[i]);
    }

    return 0;
}

In the above code, the signal length is defined as 100 and the window size is 5. First, the signal is read in, then each sampling point is filtered, and finally the filtered signal is output.

It should be noted that when calculating the sum of signal values ​​within the window, it is necessary to determine whether the window is out of bounds to avoid program crash.

In addition, the above code is only the most basic implementation method, and there is still a lot of room for optimization, such as using sliding windows, processing boundary effects, etc., which can be further optimized according to specific application scenarios.

Insert image description here

2. C++ implementation of arithmetic mean filtering method and detailed code explanation

Arithmetic average filtering is a common digital signal processing technology that reduces random noise in the signal by summing and averaging a certain number of sample values ​​of the signal. In this article, we will introduce how to implement the arithmetic mean filtering method using C++ and explain how the algorithm works and the code details.

Algorithm principle

The basic idea of ​​the arithmetic average filtering method is to sum a set of sample values ​​in the signal and divide the sum by the number of sample values ​​to obtain the average value. If there is random noise in the signal, then the average value should be closer to the true value of the signal, because the random noise is randomly distributed throughout the sampling sequence and the effect on the summation result will be averaged out.

Specifically, the steps of the algorithm are as follows:

  1. Determine the size of the sampling window, that is, how many sample values ​​need to be summed and averaged. The window size can be adjusted according to actual needs and signal characteristics.

  2. Move the window from left to right, summing the sample values ​​within the window each time, and calculating the average.

  3. Use the average value as the output value at the current window position, move the window to the right, and continue with step 2.

Code

The following is a code example using C++ to implement the arithmetic mean filtering method:

#include <iostream>
#include <vector>

using namespace std;

vector<double> movingAverage(const vector<double>& input, int windowSize) {
    vector<double> output(input.size() - windowSize + 1);

    for (int i = 0; i < output.size(); ++i) {
        double sum = 0.0;
        for (int j = i; j < i + windowSize; ++j) {
            sum += input[j];
        }
        output[i] = sum / windowSize;
    }

    return output;
}

int main() {
    vector<double> input = {1, 2, 3, 3, 2, 1};
    int windowSize = 3;
    vector<double> output = movingAverage(input, windowSize);

    cout << "Input: ";
    for (auto x : input) {
        cout << x << " ";
    }
    cout << endl;

    cout << "Output (window size = " << windowSize << "): ";
    for (auto x : output) {
        cout << x << " ";
    }
    cout << endl;

    return 0;
}

In the above code, the movingAverage function accepts two parameters: the input sequenceinput and the size of the sampling windowwindowSize . This function returns a new sequence output, which contains the input sequence processed by the arithmetic mean filtering method.

In themovingAverage function, we first create an output sequence of length to store The output value of the algorithm. Next, we traverse each position in the output sequence, sum the values ​​in the sampling window of the current position, calculate the average, and use it as the output value of the current position. This completes the implementation of the entire algorithm. input.size() - windowSize + 1output

In themain function, we define an input sequenceinput and a sampling window sizewindowSize, and call movingAverage The function processes the input sequence. Finally, we output the original input sequence and the processed output sequence to verify the correctness of the algorithm.

Summarize

Arithmetic average filtering is a simple and effective digital signal processing technology that can help us reduce random noise in signals. In this article, we describe how to implement arithmetic mean filtering using C++ and explain how the algorithm works and the code details. This algorithm can be used to filter real-time or offline data to improve data quality and accuracy.

3. Java implementation of arithmetic mean filtering method and detailed code explanation

The arithmetic mean filtering method is a commonly used filtering method in time series signal processing. Its basic idea is to replace each sample value in the original signal with the average value of a group of samples. This filtering method is simple and easy to implement, but it is not suitable for signals with a lot of noise and interference in the data.

The following is the code and detailed explanation of the arithmetic average filtering method in Java:

public class ArithmeticAverageFilter {
    
    
    private int windowSize; // 滑动窗口大小
    private Queue<Double> queue; // 用队列存储窗口内的数据

    public ArithmeticAverageFilter(int windowSize) {
    
    
        this.windowSize = windowSize;
        this.queue = new LinkedList<>();
    }

    // 添加新的数据到滑动窗口
    public void addData(double data) {
    
    
        queue.add(data);
        if (queue.size() > windowSize) {
    
    
            queue.poll();
        }
    }

    // 计算滑动窗口内数据的平均值
    public double getAverage() {
    
    
        double sum = 0;
        for (double data : queue) {
    
    
            sum += data;
        }
        return sum / queue.size();
    }
}

The above is the implementation code of the arithmetic average filtering method. This class contains two main methods, namely addData and getAverage.

addDataThe method is used to add new data to the sliding window. When the size of the sliding window exceeds the set window size, the data at the head of the queue will be removed from the queue.

getAverageThe method is used to calculate the average of the data within the sliding window. The average can be obtained by traversing the data in the queue, summing it and dividing by the queue length.

Here is a simple example using arithmetic mean filtering:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        double[] data = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int windowSize = 3;

        ArithmeticAverageFilter filter = new ArithmeticAverageFilter(windowSize);

        // 添加数据并输出滤波后的结果
        for (double d : data) {
    
    
            filter.addData(d);
            System.out.println(filter.getAverage());
        }
    }
}

In the above example, an array of length 10 is first defined as the original data, and the window size is set to 3. Then add data to the sliding window in sequence through a loop, and call the getAverage method to calculate the average and output it to the console.

The running results are as follows:

1.0
1.5
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0

It can be seen that the filtered result is much smoother, but there is also a certain degree of signal distortion. Therefore, in practical applications, appropriate filtering methods and parameters need to be selected to achieve a balance between filtering effect and signal quality.
Insert image description here

Guess you like

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