matlab using a moving average filter, a resampling and Hampel signal smoothing filter

Original link: http://tecdat.cn/?p=6977

 

This example shows how to use the moving average filter and resampling to isolate the influence of temperature readings hour time period components, and eliminate unwanted line noise from the open-circuit voltage measurements. The example also shows how to use filters to remove large Hampel outliers.

motivation

Smoothing is how we discovered important patterns in the data, while ignoring unimportant things (ie, noise). We use this filter to perform smooth. The goal is to smooth changes in the value generated slowly, so it's easier to see trends in our data.

Sometimes, when you check the input data, you may want to smooth the data to see trends signal. In our example, we set the temperature in Celsius Logan Airport every hour, throughout the month of January 2011.


 

Please note that we can visually see the impact on the time of day, temperature readings. If you are only interested in daily temperature changes monthly, the volatility per hour will cause noise, which makes it difficult to distinguish changes daily. In order to eliminate the effects of time, we now want to smooth our data by using a moving average filter.

Moving average filter

In its simplest form, the length N of the moving average filter taking an average of the waveform of each N successive samples.

In order to apply a moving average filter for each data point, we construct the filter coefficients so that the weight of each point is equal weight, and the average total contribution to 1/24. This gives the average temperature per 24 hours.



 

Filter delay

Please note that the output of the filter will be delayed about two hours. This is because we have delayed moving average filter.

Any symmetrical filter of length N will have a delay (N-1) / 2 samples. 

fDelay =(length(coeff24hMA)-1)/ 2;
 


 

The average extraction differences

 We can also use a moving average filter to better estimate what time of day affect the overall temperature. For this purpose, the smoothed data is first subtracted from the measured value of the temperature per hour. Then, the difference data broken down into a few days, and get the average of all 31 days of the month.

deltaTempC = reshape(deltaTempC,24,31)。';


 

Extracting peak envelope

有时我们也希望对每天温度信号的高低变化有一个平滑变化的估计。为此,我们可以使用该envelope功能连接在24小时内检测到的极高和低点。在这个例子中,我们确保每个极高和极低之间至少有16个小时。我们还可以通过取两个极端之间的平均值来了解高点和低点的趋势。


 

加权移动平均滤波器

其他类型的移动平均滤波器不会对每个样本进行相同的加权

另一个常见的过滤器遵循二项式扩展。这种类型的滤波器近似于大的n值的正常曲线。它可用于滤除小n的高频噪声。要找到二项式滤波器的系数,请与其自身进行卷积,然后以规定的次数迭代地对输出进行卷积。在此示例中,使用五次总迭代。

binomialCoeff = conv(h,h);
fDelay =(length(binomialCoeff)-1)/ 2;
 


 

另一种类似于高斯扩展滤波器的滤波器是指数移动平均滤波器。这种类型的加权移动平均滤波器易于构造,并且不需要大的窗口 。

您可以通过0到1之间的alpha参数调整指数加权移动平均滤波器。较高的alpha值将具有较少的平滑。

exponentialMA = filter(alpha,[1 alpha-1],tempC);
plot(days,tempC,... 
     days-fDelay / 24,binomialMA,...
 
 


 

 


 

Savitzky-Golay过滤器

 为了更紧密地跟踪信号,您可以使用加权移动平均滤波器,该滤波器尝试在最小二乘意义上拟合指定数量的样本上的指定顺序的多项式。

为方便起见,您可以使用该功能sgolayfilt实现Savitzky-Golay平滑滤波器。 

cubicMA = sgolayfilt(tempC,3,7);
quarticMA = sgolayfilt(tempC,4,7);
quinticMA = sgolayfilt(tempC,5,9);


 

重采样

 我们在存在60 Hz交流电源线噪声干扰的情况下对模拟仪器输入端的开环电压进行采样。我们以1 kHz的采样率对电压进行采样。

fs = 1000;
t =(0:numel(openLoopVoltage)-1)/ fs;
 

 

 

让我们尝试使用移动平均滤波器消除线路噪声的影响。

如果构造一个均匀加权的移动平均滤波器,它将删除任何相对于滤波器持续时间而言是周期性的分量。

当以1000Hz采样时,在60Hz的完整周期中大约有1000/60 = 16.667个样本。让我们尝试“围捕”并使用17点过滤器。这将使我们在1000 Hz / 17 = 58.82 Hz的基频下进行最大滤波。

 


 

请注意,虽然电压显着平滑,但仍然包含60 Hz的小纹波。

如果我们重新采样信号,我们可以显着减少纹波,这样我们就可以通过移动平均滤波器捕获60 Hz信号的完整周期。

如果我们以17 * 60 Hz = 1020 Hz重新采样信号,我们可以使用我们的17点移动平均滤波器来消除60 Hz线路噪声。

 
 


 

中位数过滤器

移动平均线,加权移动平均线和Savitzky-Golay滤波器可以平滑它们过滤的所有数据。然而,这可能并不总是想要的。例如,如果我们的数据来自时钟信号并且具有我们不希望平滑的锐边,该怎么办?到目前为止讨论的过滤器效果不佳:

 


 

移动平均线和Savitzky-Golay滤波器分别在时钟信号边缘附近欠校正和过校正。

保留边缘但仍然平滑水平的一种简单方法是使用中值滤波器:

 


 

通过Hampel过滤器去除异常值

许多过滤器对异常值很敏感。与中值滤波器密切相关的滤波器是Hampel滤波器。此滤波器有助于从信号中删除异常值,而不会过度平滑数据。

 


 

由于我们引入的每个尖峰只有一个样本的持续时间,我们可以使用仅三个元素的中值滤波器来消除尖峰。

 


 

滤波器移除了尖峰,但它也删除了原始信号的大量数据点。Hampel滤波器的工作方式类似于中值滤波器,但它只取代了相当于远离局部中值的几个标准偏差的值。

 


 

仅从原始信号中移除异常值。

 

 如果您有任何疑问,请在下面发表评论。 

  

大数据部落 -中国专业的第三方数据服务提供商,提供定制化的一站式数据挖掘和统计分析咨询服务

统计分析和数据挖掘咨询服务:y0.cn/teradat(咨询服务请联系官网客服

Click here to send me a messageQQ:3025393450

 

​QQ交流群:186388004 

【服务场景】  

科研项目; 公司项目外包;线上线下一对一培训;数据爬虫采集;学术研究;报告撰写;市场调查。

【大数据部落】提供定制化的一站式数据挖掘和统计分析咨询

欢迎选修我们的R语言数据分析挖掘必知必会课程!

 

  

大数据部落 -中国专业的第三方数据服务提供商,提供定制化的一站式数据挖掘和统计分析咨询服务

统计分析和数据挖掘咨询服务:y0.cn/teradat(咨询服务请联系官网客服

Click here to send me a messageQQ:3025393450

 

​QQ交流群:186388004 

【服务场景】  

科研项目; 公司项目外包;线上线下一对一培训;数据爬虫采集;学术研究;报告撰写;市场调查。

【大数据部落】提供定制化的一站式数据挖掘和统计分析咨询

欢迎选修我们的R语言数据分析挖掘必知必会课程!

 

Guess you like

Origin www.cnblogs.com/tecdat/p/11546671.html