#开发时间：2024/6/12 9:36
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams

# 移动平均法
def moving_average(signal, window_size):
    return np.convolve(signal, np.ones(window_size) / window_size, mode='same')

plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

config = {
    "font.family": 'serif',
    "font.size": 20,
    "mathtext.fontset": 'stix',
    "font.serif": ['Times New Roman'],  # 宋体
    'axes.unicode_minus': False  # 处理负号
}

rcParams.update(config)

# 读取信号
fs = 1000
noisy_signal = pd.read_csv('noisy_signals_time.csv')

# 检查是否包含时间列
if 'time' in noisy_signal.columns:
    time_column = noisy_signal['time'].values.reshape(-1, 1)
    other_columns = noisy_signal.drop(columns='time')
else:
    time_column = None
    other_columns = noisy_signal

# 应用去噪方法
window_size = 5  # 窗口宽度
denoised_signals = pd.DataFrame()

for column in other_columns.columns:
    denoised_signals[column] = moving_average(other_columns[column].values, window_size)

# 如果有时间列，将其添加回去
if time_column is not None:
    denoised_signals.insert(0, 'time', time_column)

# 保存去噪后的信号到CSV文件
denoised_signals.to_csv('denoise_MovingAverage.csv', index=False)

# 绘图
plt.figure(figsize=(12, 10))

# 绘制原始信号和去噪后信号（这里只绘制第一列作为示例）
plt.subplot(3, 1, 1)
plt.plot(noisy_signal.iloc[:, 1], label='Noisy Signal')
plt.legend()

plt.subplot(3, 1, 2)
plt.plot(denoised_signals.iloc[:, 1], label='Moving Average')
plt.legend()

plt.tight_layout()
plt.show()

