[MATLAB Issue 75] #Source code sharing | Irregular data interpolation based on MATLAB to achieve time series data expansion

[MATLAB Issue 75] #Source code sharing | Irregular data interpolation based on MATLAB to achieve time series data expansion

If the time data is sorted with an interval of unit 1, data with an interval of 0.5 can be inserted.

1. Realization effect

1. Regularly spaced data

Insert image description here
Insert image description here

2. Irregularly spaced data

Insert image description here
Insert image description here

2. Main program code

1. Interpolation test effect

%%  清空环境变量
warning off             % 关闭报警信息
close all               % 关闭开启的图窗
clear                   % 清空变量
clc                     % 清空命令行

%%  导入数据(时间序列的单列数据)
y= xlsread('数据集.xlsx');
N=size(y,1);
x=1:1:N;   % x也可以为不规则间隔插值  , 如x=sort(rand(N,1))
d=0.5;%间隔设置  ,根据x变量的间隔设置
p=0.01;%插值密度大于0,p值越小精度越高。

%% 测试插值效果
xt1=(x)';%测试样本 插值扩充后的x轴
xt2=xt1+p;%测试样本  插值偏移位置为 xt1和xt2坐标的中点
yti=cuts(x,y,xt2,xt1);%测试样本 插值结果
error = sqrt(sum((yti -y ).^2) ./ N);
  %绘图 
  figure()
  plot(x,y,'b-x',reshape([xt1 xt2]',[],1),reshape([yti yti]',[],1),'ro',mean([xt1 xt2],2),yti,'ro', 'LineWidth', 2)
  legend('实际值','插值')
xlabel('x')
ylabel('y')
string = {
    
    '测试集插值误差';['RMSE=' num2str(error)]};
title(string)
grid

2. Formal interpolation

%% 正式插值
x1=(0.5:1:N-0.5)';%测试样本 插值扩充后的x轴
x2=x1+p;%测试样本  插值偏移位置为 xt1和xt2坐标的中点
yi=cuts(x,y,x2,x1);%测试样本 插值结果
  %绘图 
  figure()
  plot(x,y,'b-x',reshape([x1 x2]',[],1),reshape([yi yi]',[],1),'ro',mean([x1 x2],2),yi,'ro', 'LineWidth', 2)
  legend('实际值','插值')
xlabel('x')
ylabel('y')
string2 = {
    
    '插值效果'};
title(string2)
grid

3. Irregular data interpolation

Customize the x interval range. For example, there are also 100 samples. The x-axis number is randomly selected from 1:500 as the x-axis, and y remains unchanged. It can also contain decimals, or define it randomly by yourself.

xx = randperm(500); %
x=sort(xx(1:N));   % x也可以为不规则间隔插值

During formal interpolation, x1 is changed to:

x1=(1:10:500)';%测试样本 插值扩充后的x轴

3. Code acquisition

Just reply "Issue 75" to the CSDN backend private message.

Guess you like

Origin blog.csdn.net/qq_29736627/article/details/132898772