import pandas as pd
import matplotlib.pyplot as plt
def smooth_data(data, factor):
smoothed_data = data.rolling(window=factor, min_periods=1).mean()
return smoothed_data
# 데이터 불러오기
file_path = '361_2.xlsx'
data = pd.read_excel(file_path)
# 스무딩(factor 조정 가능)
factor = 5
smoothed_data_1 = smooth_data(data['Data-1'], factor)
smoothed_data_2 = smooth_data(data['Data-2'], factor)
# 데이터 번호 열 추가
data.insert(0, 'No.', range(1, len(data) + 1))
# 스무딩된 데이터를 'sm.xlsx' 파일에 저장
output_file = 'sm_out.xlsx'
with pd.ExcelWriter(output_file) as writer:
data[['No.']].to_excel(writer, index=False)
pd.DataFrame({'Smoothed Data-1 (Factor={})'.format(factor): smoothed_data_1}).to_excel(writer, index=False, startcol=1)
pd.DataFrame({'Smoothed Data-2 (Factor={})'.format(factor): smoothed_data_2}).to_excel(writer, index=False, startcol=2)
# 그래프로 출력
plt.plot(data['No.'], data['Data-1'], label='Data-1 (Original)', color='blue', linestyle='--', linewidth=1)
plt.plot(data['No.'], smoothed_data_1, label=f'Data-1 Smoothed (Factor={factor})', color='blue', linestyle='-', linewidth=1)
plt.plot(data['No.'], data['Data-2'], label='Data-2 (Original)', color='red', linestyle='--', linewidth=1)
plt.plot(data['No.'], smoothed_data_2, label=f'Data-2 Smoothed (Factor={factor})', color='red', linestyle='-', linewidth=1)
plt.xlabel('No.')
plt.ylabel('Value')
plt.title('Original vs Smoothed Data')
plt.legend()
plt.grid(True)
plt.show()