Matlab重复x轴以进行插值

3

我需要插值风向。数据是以每1000个[英尺]为单位给出的。例如:

%winddata input in feet en degrees
x=0:1000:10000;
grad=[340 350 360 1 10 20 30 35 34 36 38]; 

插值效果很好,我使用interp1函数(请参见下面的代码)。然而,从360度到1度的步骤是一个问题。我希望Matlab可以直接从360度向右(顺时针)插值到1度,而不是逆时针从360-359-358-...3-2-1递减。这在插值风向时没有意义。

我该如何命令Matlab每隔360度重复x轴和y轴数值?

clear all;clc;
h=2000;

%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 1 10 20 30 35 34 36 38];    

%conversion to SI:
x=0.3048*x;
u=0:1:max(x);

yinterp1 = interp1(x,degrees,u,'linear');

figure(3)
plot(degrees,x,'bo',yinterp1,u,'-r')
xlabel('wind direction [degrees]') 
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'

wind direction interpolation, the 360->1 degrees jump is the problem


1
我认为最简单的解决方案是将某个前向阈值以下的所有值加上360。但我认为只有当风向不旋转360度时,这种方法才有效。 - TroyHaskin
1个回答

2
问题在于Matlab虽然非常聪明,但它并不知道你是在处理度数,所以它认为360 = 0。因此,我认为你的问题不在于找到重复绘制每360度的方法,而是你正在输入到interp1 函数中的数据有问题,目前你告诉它在点(0, 950)(360, 750)之间有一条直线。请注意保留HTML标签。
最简单但最丑陋的方法是将360添加到较小的值,使得你的度数向量变成如下形式:
degrees = [340 350 360 361 370 380 390 395 394 396 398];

然后从 degreesyinterp1 向量中减去 360:

clear all;clc;
h=2000;

%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 361 370 380 390 395 394 396 398];    

%conversion to SI:
x=0.3048*x;
u=0:1:max(x);

yinterp1 = interp1(x,degrees,u,'linear');

figure(3)
plot(degrees-360,x,'bo',yinterp1-360,u,'-r')
xlabel('wind direction [degrees]') 
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'
xlim([-180 180]);

这种方法的明显问题是它不能适用于所有情况,但如果你只需要一次性解决问题,那么它会很有效。为了更通用的解决方案,你可以手动输入一个点,在该点以下的值将被增加360:
clear all;clc;
h=2000;

% --------------------- Manual cutoff for rectification -------------------
limitDegrees = 180;
% -------------------------------------------------------------------------
%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 1 10 20 30 35 34 36 38];   

%conversion to SI:
x=0.3048*x;
u=0:1:max(x);


indecesTooSmall = find(degrees <= limitDegrees);
oneVec = zeros(size(degrees));

oneVec(indecesTooSmall) = 1;

vecToAdd = 360*ones(size(degrees));
vecToAdd = vecToAdd .* oneVec;

newDegrees = degrees + vecToAdd;

yinterp1 = interp1(x,newDegrees,u,'linear');

figure(3)
plot(newDegrees-360,x,'bo',yinterp1-360,u,'-r')
xlabel('wind direction [degrees]') 
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'
xlim([-180 180]);

以上两种解决方案均可得到以下结果:

Correct plot

编辑:更简单的解决方案,只需使用rad2deg(unwrap(deg2rad(degrees))),或尝试寻找适用于度数的解包方法。


1
你应该将unwrap作为你的主要答案,这是在这种情况下使用的专用函数。 - Robert Seifert
没错,但是由于他之前的回答和方法,我(作为提问者)更好地理解了真正的问题 :) - Jurriën

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接