clipped average filtering method

The limiting average filtering method is a digital signal processing technology used to filter out the impact of noise on the signal. The principle is as follows:

  1. Set a limit range to limit signal values ​​outside the range.

  2. The signal values ​​within the limit range are averaged to obtain an average value.

  3. Use the average value as the output value of this signal point.

  4. The signal is continuously processed, and a certain number of signal points are processed in each processing cycle to obtain the average value within a period of time.

  5. Repeat the above process to finally obtain the filtered signal.

The advantage of the limiting average filtering method is that it is simple and easy to implement, and it can effectively remove instantaneous noise interference. But the disadvantage is that it cannot effectively remove constant noise that exists for a long time. At the same time, setting appropriate limit ranges and average numbers also requires certain experience and experimental verification.
Insert image description here

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

The limiting average filtering method is a commonly used digital signal filtering method used to remove the influence of noise signals. This method achieves smoothing of the signal by taking the absolute value of the signal and limiting its range, and then calculating the moving average.

The following is a piece of code in C language that implements the limiting average filtering method:

#include <stdio.h>

#define N 10   // 信号长度
#define M 3    // 平均滤波器长度
#define MAX 20 // 最大幅值限制

int main() {
    
    
    int x[N] = {
    
    4, 6, 10, 3, 5, 3, 10, 18, 12, 6}; // 原始信号
    int y[N] = {
    
    0}; // 滤波后的信号
    int sum = 0; // 平均滤波器累加和
    int i, j; // 计数器

    // 限幅平均滤波
    for(i = 0; i < N; i++) {
    
    
        // 绝对值限制
        if(x[i] > MAX) {
    
    
            x[i] = MAX;
        } else if(x[i] < -MAX) {
    
    
            x[i] = -MAX;
        }
        // 移动平均
        sum += x[i];
        if(i >= M) {
    
    
            sum -= x[i - M];
            y[i - (M - 1) / 2] = sum / M;
        }
    }

    // 输出滤波结果
    printf("Original Signal\tFiltered Signal\n");
    for(i = 0; i < N; i++) {
    
    
        printf("%d\t\t%d\n", x[i], y[i]);
    }

    return 0;
}

In the above code, the length of the original signalx is N, and the length of the average filter is M. Variablesum records the cumulative sum of the averaging filter. By looping through each signal point, the absolute value of the signal is taken and its range is limited, and then the moving average is calculated and the result is stored in the filtered signal y. Finally, the original signal and the filtered signal are output.

The advantage of the limiting average filtering method is that it is simple to understand, easy to implement, and can better remove the influence of noise signals. However, the disadvantage of this method is that some noise signals with extremely large peak values ​​and relatively short time periods are easily misjudged as valid signals and cannot be removed. Therefore, in practical applications, it is necessary to select an appropriate filtering method based on the specific situation.

Insert image description here

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

The limiting average filtering method is a commonly used digital filtering method that can effectively reduce the impact of noise on the signal. The basic idea is to use average filtering to smooth the signal, and at the same time introduce a limiter to limit the filtered signal within a certain range to reduce the impact of noise.

The following is the code and detailed explanation of the limiting average filtering method in C++:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// 限幅平均滤波函数
double limit_average_filter(vector<double>& data, int N, double A)
{
    double sum = 0.0;   // 定义变量记录部分数据的累加和
    int count = 0;      // 定义变量记录参与累加的数据个数
    double result = 0.0;    // 定义变量记录滤波结果

    for(int i=0; i<N; i++)
    {
        if(abs(data[i]-result) <= A)    // 若当前数据与上次滤波结果之差小于阈值 A,则将其加入部分数据中
        {
            sum += data[i];
            count++;
        }

        if(count != 0)  // 若部分数据非空,则计算平均值,并存储为滤波结果
        {
            result = sum/count;
        }
    }

    return result;
}

int main()
{
    // 测试数据
    vector<double> data = {3.0, 4.0, 2.0, 3.5, 4.5, 5.0, 6.0, 1.0, 2.0, 9.0};
    // 滤波窗口大小
    int N = 5;
    // 限制阈值
    double A = 1.0;
    // 调用限幅平均滤波函数
    double result = limit_average_filter(data, N, A);
    // 输出滤波结果
    cout << "滤波结果为:" << result << endl;

    return 0;
}

In the code, the vector type in the STL library is used to store the input data. In the main function, specify the filter window size and limit threshold, and call the limiting average filter function to filter the data. In the function, the input data is first traversed. When the difference between the current data and the last filtering result is less than the threshold A, it is added to the partial data, and then the average value of the partial data is calculated and stored as the filtering result.

It should be noted that in actual applications, the filter window size and limit threshold need to be adjusted according to specific conditions to achieve better filtering effects.

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

The limiting average filtering method is a filtering method for removing noise, which can effectively suppress the distortion of filtering results caused by a single outlier. The following is the code and detailed explanation of the limiting average filtering method in Java.

The implementation of the limiting average filtering method mainly includes the following steps:

  1. Define the variables and constants you need to use. where n is the filter window size and M is the maximum deviation allowed.
int n = 5;  // 滤波器窗口大小
int M = 10; // 允许的最大偏差值
  1. Define the filter data structure, including the current value, historical value and the sum of the data within the window.
class Filter {
    int current;     // 当前值
    int[] history;   // 历史值
    int sum;         // 窗口内数据的总和
    
    public Filter(int n) {
        current = 0;
        history = new int[n];
        sum = 0;
    }
}
  1. Define the function that implements the clipping average filtering method. This function needs to receive the data that needs to be filtered and the filter data structure.
int limitAverageFilter(int x, Filter filter) {
    // 更新当前值和历史值
    filter.current = x;
    for (int i = filter.history.length - 1; i > 0; i--) {
        filter.history[i] = filter.history[i - 1];
    }
    filter.history[0] = x;
    
    // 更新窗口内数据的总和
    filter.sum += x - filter.history[n - 1];
    
    // 计算平均值和标准差
    double average = filter.sum / (double) n;
    double stdDev = 0;
    for (int i = 0; i < n; i++) {
        stdDev += Math.pow(filter.history[i] - average, 2);
    }
    stdDev = Math.sqrt(stdDev / n);
    
    // 如果标准差小于等于允许的最大偏差值 M,则返回平均值,
    // 否则返回当前值。
    if (stdDev <= M) {
        return (int) average;
    } else {
        return filter.current;
    }
}
  1. Filter using the function just defined. In this example, we simulate a set of random data with noise and perform a clipping average filter on it.
public static void main(String[] args) {
    // 模拟一组带有噪声的随机数据
    int[] data = new int[100];
    Random random = new Random();
    for (int i = 0; i < data.length; i++) {
        data[i] = random.nextInt(100);
    }
    
    // 对数据进行限幅平均滤波
    Filter filter = new Filter(n);
    for (int i = 0; i < data.length; i++) {
        int filtered = limitAverageFilter(data[i], filter);
        System.out.println("Data[" + i + "] = " + data[i] + ", Filtered[" + i + "] = " + filtered);
    }
}

The above is the entire code and detailed explanation of the limiting average filtering method in Java.
Insert image description here

Guess you like

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