Main Content

데이터 평활화와 이상값 감지

데이터 평활화는 데이터에서 원치 않는 잡음이나 동작을 제거하는 기법을 나타내는 반면, 이상값 감지는 나머지 데이터와 큰 차이가 있는 데이터 점을 식별합니다.

이동 윈도우 방법

이동 윈도우 방법은 일반적으로 데이터에서 이웃한 점들을 통계적으로 나타내기 위해 데이터를 더 작은 단위로 구분해 한 번에 한 단위씩 처리하는 방법입니다. 이동 평균은 데이터를 따라 윈도우를 이동하면서 각 윈도우 내에 있는 점의 평균을 구하는 일반적인 데이터 평활화 기법입니다. 이 기법은 한 데이터 점과 다음 데이터 점 간의 작은 변동을 제거하는 데 도움이 될 수 있습니다.

예를 들어, 약 3시간 동안 매분 측정된 풍속 값이 있다고 가정하겠습니다. movmean 함수와 윈도우 크기 5분을 사용하여 빠른 속도의 돌풍을 평활화합니다.

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")

Figure contains an axes object. The axes object with xlabel Time, ylabel Speed contains 2 objects of type line. These objects represent Measured Wind Speed, Average Wind Speed over 5 min Window.

마찬가지로, movmedian 함수를 사용하여 슬라이딩 윈도우에서의 풍속 중앙값을 계산할 수 있습니다.

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")

Figure contains an axes object. The axes object with xlabel Time, ylabel Speed contains 2 objects of type line. These objects represent Measured Wind Speed, Median Wind Speed over 5 min Window.

모든 데이터가 이동 윈도우 방법으로 평활화하기 적합한 것은 아닙니다. 예를 들어, 랜덤 잡음이 삽입된 정현파 신호를 생성해 보겠습니다.

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")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original Data, Noisy Data.

윈도우 크기가 3인 이동 평균을 사용하여 잡음이 있는 데이터를 평활화합니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent 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")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original Data, Moving Mean - Window Size 5.

일반적인 평활화 방법

smoothdata 함수는 신호 처리에서 많이 사용되는 평활화 기법인 사비츠키-골레이 방법을 비롯한 여러 가지 평활화 옵션을 제공합니다. 기본적으로 smoothdata는 데이터에 따라 평활화 방법에 가장 적합한 윈도우 크기를 추측하여 선택합니다.

사비츠키-골레이 방법을 사용하여 잡음이 있는 신호 Anoise를 평활화하고, 이 방법에 사용된 윈도우 크기를 출력해 보겠습니다. 이 방법은 movmean에 비해 더 나은 밸리 근삿값을 제공합니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original Data, Savitzky-Golay.

window
window = 3

또 다른 평활화 방법으로, 데이터에 잡음 외에도 이상값이 있는 경우 특히 도움이 되는 로버스트 Lowess 방법이 있습니다. 잡음이 있는 데이터에 이상값을 넣은 다음, 로버스트 Lowess를 사용하여 데이터를 평활화해 보겠습니다. 그러면 이상값이 제거됩니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Noisy Data, Robust Lowess.

이상값 감지하기

데이터에 이상값이 있으면 데이터 처리 결과와 그 밖의 계산된 양이 크게 왜곡될 수 있습니다. 예를 들어, 이상값이 포함된 데이터를 이동 중앙값을 사용하여 평활화하려 할 경우 잘못된 피크 또는 밸리가 나타날 수 있습니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Noisy Data, Moving Median.

isoutlier 함수는 이상값이 감지될 경우 논리값 1을 반환합니다. Anoise에 있는 이상값의 인덱스와 값을 확인해 보겠습니다.

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

filloutliers 함수를 사용하고 채우기 방법을 지정하여 데이터에 있는 이상값을 대체할 수 있습니다. 예를 들어, Anoise에 있는 이상값을 그 바로 오른쪽에 있는 이웃 값으로 채워 보겠습니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Noisy Data with Outlier, Noisy Data with Filled Outlier.

또는 rmoutliers 함수를 사용하여 데이터에서 이상값을 제거할 수 있습니다. 예를 들어, Anoise에 있는 이상값을 제거할 수 있습니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Noisy Data with Outlier, Noisy Data with Outlier Removed.

비균일 간격의 데이터

모든 데이터가 균일한 간격의 점으로 구성되는 것은 아니며, 이는 데이터 처리 방법에 영향을 줄 수 있습니다. Airreg의 데이터에 대한 불규칙한 샘플링 시간을 포함하는 datetime형 벡터를 만들어 보겠습니다. time 벡터는 이틀에 걸쳐 처음 30분 동안은 1분마다, 그다음에는 1시간마다 추출한 샘플을 나타냅니다.

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

Figure contains an axes object. The axes object contains an object of type line.

기본적으로 smoothdata는 균일한 간격의 정수(여기서는 1,2,...,78)에 대해 평활화합니다. 정수 타임스탬프가 Airreg의 점 샘플링과 시점이 잘 맞지 않아 처음 30분간의 데이터는 평활화 후에도 여전히 잡음이 있는 것처럼 보입니다.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original Data, Smoothed Data with Default Sample Points.

smoothdata, movmean, filloutliers를 비롯한 MATLAB®의 여러 데이터 처리 함수를 사용하면, 제공된 샘플 점에 대해 그 샘플링 단위와 샘플링 주파수를 기준으로 데이터가 처리되도록 할 수 있습니다. Airreg의 처음 30분간 데이터의 고주파 변동을 제거하려면 time의 타임스탬프에 SamplePoints 이름-값 인수를 사용하십시오.

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original Data, Smoothed Data with Sample Points.

참고 항목

함수

라이브 편집기 작업

관련 항목