处理一下异常值

处理一下异常值_第1张图片

clear

clc

close all

% 生成风速时程

time_length = 5 * 60; % 5分钟,单位:秒

sampling_rate =15; % 采样频率,单位:Hz

total_samples = time_length * sampling_rate; % 总采样点数

% 生成随机风速数据

wind_speed = randn(1, total_samples);

wind_speed(100)=6;

wind_speed(300)=6;

wind_speed(500)=6;

wind_speed(700)=6;

hhh=wind_speed;

% 对风速数据进行处理

for i = 1:5

% 计算当前分钟内的索引范围

start_index = (i - 1) * 60 * sampling_rate + 1;

end_index = i * 60 * sampling_rate;

% 获取当前分钟内的风速数据

minute_data = wind_speed(start_index:end_index);

% 计算平均值和标准差

mean_value = mean(minute_data);

std_dev = std(minute_data);

% 计算三西格玛准则的阈值

threshold = 3 * std_dev;

% 将不满足三西格玛准则的值替换为平均值

outliers = abs(minute_data - mean_value) > threshold;

minute_data(outliers) = mean_value;

% 更新原始风速数据

wind_speed(start_index:end_index) = minute_data;

end

figure(1)

time_axis = (0:total_samples-1) / sampling_rate;

plot(time_axis, hhh);

hold on

plot(time_axis, wind_speed);

hold off

xlabel('时间 (秒)');

ylabel('风速');

title('风速时程');

你可能感兴趣的:(matlab)