Definition der Spline-Interpolation (ähnlich den natürlichen Splines, aber mit einer an den Rändern des Interpolations- bzw. Extrapolationsbereiches verschwindenden zweiten Ableitung) |
def mysplines(xp,yp,xleft,xright,xq):
cm=zeros([4*(len(xp)-1),4*(len(xp)-1)])
for i in range(0,len(xp)-1):
cm[4*i,4*i]=0.0
cm[4*i,4*i+1]=0.0
cm[4*i,4*i+2]=1.0
if i==0:
cm[4*i,4*i+3]=3*xleft
else:
cm[4*i,4*i+3]=3*xp[i]
cm[4*i,4*i-4]=0.0
cm[4*i,4*i-3]=0.0
cm[4*i,4*i-2]=-1.0
cm[4*i,4*i-1]=-3*xp[i]
cm[4*i+1,4*i]=1.0
cm[4*i+1,4*i+1]=xp[i]
cm[4*i+1,4*i+2]=xp[i]**2
cm[4*i+1,4*i+3]=xp[i]**3
cm[4*i+2,4*i]=1.0
cm[4*i+2,4*i+1]=xp[i+1]
cm[4*i+2,4*i+2]=xp[i+1]**2
cm[4*i+2,4*i+3]=xp[i+1]**3
cm[4*i+3,4*i]=0.0
if i==len(xp)-2:
cm[4*i+3,4*i+1]=0.0
cm[4*i+3,4*i+2]=1.0
cm[4*i+3,4*i+3]=3*xright
else:
cm[4*i+3,4*i+1]=1.0
cm[4*i+3,4*i+2]=2*xp[i+1]
cm[4*i+3,4*i+3]=3*xp[i+1]**2
cm[4*i+3,4*i+4]=0.0
cm[4*i+3,4*i+5]=-1.0
cm[4*i+3,4*i+6]=-2*xp[i+1]
cm[4*i+3,4*i+7]=-3*xp[i+1]**2
cs=zeros(4*(len(xp)-1))
for i in range(0,len(xp)-1):
cs[4*i+1]=yp[i]
cs[4*i+2]=yp[i+1]
cc=solve(cm,cs)
yq=zeros(len(xq))
for q in range(0,len(xq)):
i=len(xp)-2
while (i>0) and (xq[q]<xp[i]):
i-=1
yq[q]=cc[4*i+3]*xq[q]**3+cc[4*i+2]*xq[q]**2+cc[4*i+1]*xq[q]+cc[4*i]
return yq
|
% nicht im Command Window
function yq=mysplines(xp,yp,xleft,xright,xq)
cm=zeros(4*(length(xp)-1),4*(length(xp)-1));
for i=1:length(xp)-1;
cm(4*i-3,4*i-3)=0;
cm(4*i-3,4*i-2)=0;
cm(4*i-3,4*i-1)=1;
if i==1
cm(4*i-3,4*i)=3*xleft;
else
cm(4*i-3,4*i)=3*xp(i);
cm(4*i-3,4*i-7)=0;
cm(4*i-3,4*i-6)=0;
cm(4*i-3,4*i-5)=-1;
cm(4*i-3,4*i-4)=-3*xp(i);
end
cm(4*i-2,4*i-3)=1;
cm(4*i-2,4*i-2)=xp(i);
cm(4*i-2,4*i-1)=xp(i)^2;
cm(4*i-2,4*i)=xp(i)^3;
cm(4*i-1,4*i-3)=1;
cm(4*i-1,4*i-2)=xp(i+1);
cm(4*i-1,4*i-1)=xp(i+1)^2;
cm(4*i-1,4*i)=xp(i+1)^3;
cm(4*i,4*i-3)=0;
if i==length(xp)-1;
cm(4*i,4*i-2)=0;
cm(4*i,4*i-1)=1;
cm(4*i,4*i)=3*xright;
else
cm(4*i,4*i-2)=1;
cm(4*i,4*i-1)=2*xp(i+1);
cm(4*i,4*i)=3*xp(i+1)^2;
cm(4*i,4*i+1)=0;
cm(4*i,4*i+2)=-1;
cm(4*i,4*i+3)=-2*xp(i+1);
cm(4*i,4*i+4)=-3*xp(i+1)^2;
end
end
cs=zeros(4*(length(xp)-1));
for i=1:length(xp)-1
cs(4*i-2)=yp(i);
cs(4*i-1)=yp(i+1);
end
cc=linsolve(cm,cs);
yq=zeros(1,length(xq));
for q=1:length(xq)
i=length(xp)-1;
while ((i>1) && (xq(q)<xp(i)))
i=i-1;
end
yq(q)=cc(4*i)*xq(q)^3+cc(4*i-1)*xq(q)^2+cc(4*i-2)*xq(q)+cc(4*i-3);
end
|
Auswahl geeigneter Interpolationen in Abhängigkeit von der Anzahl der zu interpolierenden Extrempunkte |
def interpol(mea,mev,mee):
if len(mea)>2:
#Splines, zweite Ableitung an den Raendern!! null
meq=mysplines(mea,mev,0,N-1,arange(0,N))
else:
if len(mea)==2:
#lineare Inter-/Extrapolation
meq=arange(0,N)*(mev[1]-mev[0])/float(mea[1]-mea[0])+mev[0]-mea[0]*(mev[1]-mev[0])/float(mea[1]-mea[0])
else:
if len(mea)==1:
#Konstante
meq=ones(N)*mev[0]
else:
#Ersatzwert
meq=ones(N)*mee
return meq
|
% nicht im Command Window
function meq=interpol(N,mea,mev,mee)
if length(mea)>2
meq=mysplines(mea,mev,0,N-1,0:N-1);
else
if length(mea)==2
meq=(0:N-1)*(mev(2)-mev(1))/(mea(2)-mea(1))+mev(1)-mea(1)*(mev(2)-mev(1))/(mea(2)-mea(1));
else
if length(mea)==1
meq=ones(1,N)*mev(1);
else
meq=ones(1,N)*mee;
end
end
end
|
EMD, Zerlegung in intrinsische Moden |
xm=copy(x)
modes=zeros([0,N])
for m in range(0,N):
if max(xm)>min(xm):
mmm=zeros(N)
for l in range(0,100): #Anzahl der Siebschritte
xmm=xm-mmm
mxv=array([])
mxa=array([])
for i in range(1,N-1):
if (xmm[i]>xmm[i-1]) and (xmm[i]>xmm[i+1]):
mxv=append(mxv,xmm[i])
mxa=append(mxa,i)
mnv=array([])
mna=array([])
for i in range(1,N-1):
if (xmm[i]<xmm[i-1]) and (xmm[i]<xmm[i+1]):
mnv=append(mnv,xmm[i])
mna=append(mna,i)
mxq=interpol(mxa,mxv,max(xmm))
mnq=interpol(mna,mnv,min(xmm))
mmm+=0.5*(mxq+mnq)
modes=append(modes,xmm.reshape((1,N)),axis=0)
xm=mmm
plot(arange(0,N),modes[0,:],arange(0,N),modes[1,:],arange(0,N),modes[2,:])
show()
|
xm=x;
modes=zeros(0,N);
for m=1:N
if max(xm)>min(xm)
mmm=zeros(1,N);
for l=1:100 % Anzahl der Siebschritte
xmm=xm-mmm;
mxv=[];
mxa=[];
for i=2:N-1
if ((xmm(i)>xmm(i-1)) && (xmm(i)>xmm(i+1)))
mxv=[mxv,xmm(i)];
mxa=[mxa,i];
end
end
mnv=[];
mna=[];
for i=2:N-1
if ((xmm(i)<xmm(i-1)) && (xmm(i)<xmm(i+1)))
mnv=[mnv,xmm(i)];
mna=[mna,i];
end
end
% interpol(N,mxa,mxv,max(xmm))
if length(mxa)>2
cm=zeros(4*(length(mxa)-1),4*(length(mxa)-1));
for i=1:length(mxa)-1;
cm(4*i-3,4*i-3)=0;
cm(4*i-3,4*i-2)=0;
cm(4*i-3,4*i-1)=1;
if i==1
cm(4*i-3,4*i)=3*0;
else
cm(4*i-3,4*i)=3*mxa(i);
cm(4*i-3,4*i-7)=0;
cm(4*i-3,4*i-6)=0;
cm(4*i-3,4*i-5)=-1;
cm(4*i-3,4*i-4)=-3*mxa(i);
end
cm(4*i-2,4*i-3)=1;
cm(4*i-2,4*i-2)=mxa(i);
cm(4*i-2,4*i-1)=mxa(i)^2;
cm(4*i-2,4*i)=mxa(i)^3;
cm(4*i-1,4*i-3)=1;
cm(4*i-1,4*i-2)=mxa(i+1);
cm(4*i-1,4*i-1)=mxa(i+1)^2;
cm(4*i-1,4*i)=mxa(i+1)^3;
cm(4*i,4*i-3)=0;
if i==length(mxa)-1;
cm(4*i,4*i-2)=0;
cm(4*i,4*i-1)=1;
cm(4*i,4*i)=3*(N-1);
else
cm(4*i,4*i-2)=1;
cm(4*i,4*i-1)=2*mxa(i+1);
cm(4*i,4*i)=3*mxa(i+1)^2;
cm(4*i,4*i+1)=0;
cm(4*i,4*i+2)=-1;
cm(4*i,4*i+3)=-2*mxa(i+1);
cm(4*i,4*i+4)=-3*mxa(i+1)^2;
end
end
cs=zeros(4*(length(mxa)-1));
for i=1:length(mxa)-1
cs(4*i-2)=mxv(i);
cs(4*i-1)=mxv(i+1);
end
cc=linsolve(cm,cs);
yq=zeros(1,N);
for q=0:N-1
i=length(mxa)-1;
while ((i>1) && (q<mxa(i)))
i=i-1;
end
mxq(q+1)=cc(4*i)*q^3+cc(4*i-1)*q^2+cc(4*i-2)*q+cc(4*i-3);
end
else
if length(mxa)==2
mxq=(0:N-1)*(mxv(2)-mxv(1))/(mxa(2)-mxa(1))+mxv(1)-mxa(1)*(mxv(2)-mxv(1))/(mxa(2)-mxa(1));
else
if length(mxa)==1
mxq=ones(1,N)*mxv(1);
else
mxq=ones(1,N)*max(xmm);
end
end
end
% interpol(N,mna,mnv,min(xmm))
if length(mna)>2
cm=zeros(4*(length(mna)-1),4*(length(mna)-1));
for i=1:length(mna)-1;
cm(4*i-3,4*i-3)=0;
cm(4*i-3,4*i-2)=0;
cm(4*i-3,4*i-1)=1;
if i==1
cm(4*i-3,4*i)=3*0;
else
cm(4*i-3,4*i)=3*mna(i);
cm(4*i-3,4*i-7)=0;
cm(4*i-3,4*i-6)=0;
cm(4*i-3,4*i-5)=-1;
cm(4*i-3,4*i-4)=-3*mna(i);
end
cm(4*i-2,4*i-3)=1;
cm(4*i-2,4*i-2)=mna(i);
cm(4*i-2,4*i-1)=mna(i)^2;
cm(4*i-2,4*i)=mna(i)^3;
cm(4*i-1,4*i-3)=1;
cm(4*i-1,4*i-2)=mna(i+1);
cm(4*i-1,4*i-1)=mna(i+1)^2;
cm(4*i-1,4*i)=mna(i+1)^3;
cm(4*i,4*i-3)=0;
if i==length(mna)-1;
cm(4*i,4*i-2)=0;
cm(4*i,4*i-1)=1;
cm(4*i,4*i)=3*(N-1);
else
cm(4*i,4*i-2)=1;
cm(4*i,4*i-1)=2*mna(i+1);
cm(4*i,4*i)=3*mna(i+1)^2;
cm(4*i,4*i+1)=0;
cm(4*i,4*i+2)=-1;
cm(4*i,4*i+3)=-2*mna(i+1);
cm(4*i,4*i+4)=-3*mna(i+1)^2;
end
end
cs=zeros(4*(length(mna)-1));
for i=1:length(mna)-1
cs(4*i-2)=mnv(i);
cs(4*i-1)=mnv(i+1);
end
cc=linsolve(cm,cs);
yq=zeros(1,N);
for q=0:N-1
i=length(mna)-1;
while ((i>1) && (q<mna(i)))
i=i-1;
end
mnq(q+1)=cc(4*i)*q^3+cc(4*i-1)*q^2+cc(4*i-2)*q+cc(4*i-3);
end
else
if length(mna)==2
mnq=(0:N-1)*(mnv(2)-mnv(1))/(mna(2)-mna(1))+mnv(1)-mna(1)*(mnv(2)-mnv(1))/(mna(2)-mna(1));
else
if length(mna)==1
mnq=ones(1,N)*mnv(1);
else
mnq=ones(1,N)*min(xmm);
end
end
end
mmm=mmm+0.5*(mxq+mnq);
end
modes=[modes;xmm];
xm=mmm;
end
end
plot(0:N-1,modes(1,:),0:N-1,modes(2,:),0:N-1,modes(3,:))
|