# 数据平滑和离群值检测

### 移动窗口方法

```load windData.mat mins = 1:length(speed); window = 5; meanspeed = movmean(speed,window); plot(mins,speed,mins,meanspeed) axis tight legend("Measured Wind Speed","Average Wind Speed over 5 min Window") xlabel("Time") ylabel("Speed")```

```medianspeed = movmedian(speed,window); plot(mins,speed,mins,medianspeed) axis tight legend("Measured Wind Speed","Median Wind Speed over 5 min Window") xlabel("Time") ylabel("Speed")```

```t = 1:0.2:15; A = sin(2*pi*t) + cos(2*pi*0.5*t); Anoise = A + 0.5*rand(1,length(t)); plot(t,A,t,Anoise) axis tight legend("Original Data","Noisy Data")```

```window = 3; Amean = movmean(Anoise,window); plot(t,A,t,Amean) axis tight legend("Original Data","Moving Mean - Window Size 3")```

```Amean = movmean(Anoise,5); plot(t,A,t,Amean) axis tight legend("Original Data","Moving Mean - Window Size 5")```

### 常见的平滑方法

`smoothdata` 函数提供几种平滑选项，如萨维茨基-戈雷方法，这是一种常用的信号处理平滑技术。默认情况下，`smoothdata` 根据数据为方法选择最佳估计窗口大小。

```[Asgolay,window] = smoothdata(Anoise,"sgolay"); plot(t,A,t,Asgolay) axis tight legend("Original Data","Savitzky-Golay","location","best")```

`window`
```window = 3 ```

```Anoise(36) = 20; Arlowess = smoothdata(Anoise,"rlowess",5); plot(t,Anoise,t,Arlowess) axis tight legend("Noisy Data","Robust Lowess")```

### 检测离群值

```Amedian = smoothdata(Anoise,"movmedian"); plot(t,Anoise,t,Amedian) axis tight legend("Noisy Data","Moving Median")```

```TF = isoutlier(Anoise); ind = find(TF)```
```ind = 36 ```
`Aoutlier = Anoise(ind)`
```Aoutlier = 20 ```

```Afill = filloutliers(Anoise,"next"); plot(t,Anoise,t,Afill,"o-") axis tight legend("Noisy Data with Outlier","Noisy Data with Filled Outlier")```

```Aremove = rmoutliers(Anoise); plot(t,Anoise,t(~TF),Aremove,"o-") axis tight legend("Noisy Data with Outlier","Noisy Data with Outlier Removed")```

### 非均匀数据

```t0 = datetime(2014,1,1,1,1,1); timeminutes = sort(t0 + minutes(1:30)); timehours = t0 + hours(1:48); time = [timeminutes timehours]; Airreg = rand(1,length(time)); plot(time,Airreg) axis tight```

```Adefault = smoothdata(Airreg,"movmean",3); plot(time,Airreg,time,Adefault) axis tight legend("Original Data","Smoothed Data with Default Sample Points")```

MATLAB® 中的许多数据处理函数（包括 `smoothdata``movmean``filloutliers`）允许您提供样本点，从而确保相对于其采样单位和频率处理数据。要消除 `Airreg` 中前半小时数据的高频变化，请将 `SamplePoints` 名称-值参数和 `time` 中的时间戳结合使用。

```Asamplepoints = smoothdata(Airreg,"movmean", ... hours(3),"SamplePoints",time); plot(time,Airreg,time,Asamplepoints) axis tight legend("Original Data","Smoothed Data with Sample Points")```