Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problem with spline

Subject: problem with spline

From: dwi

Date: 15 Nov, 2012 14:49:28

Message: 1 of 2

I have a signal in the form of sin(x). I find all the local maxima and connect them with cubic spline and do the same for the minima, thus creating two envelopes (much like the huang-hilbert method if anyone is familiar with it).I calculate the mean value as mesitimi=(upenvelope+downenvelope)/2) and subtract it from my original DATA and all this in a loop which control how many maxima and minima are there. But when the time comes that I have only 1 minima or maxima I can't create the spline so the program returns an error. But I still need to find the mean and subtract it so as to get a 'residual'. How can I do this?

x=[-50:2:50];
DATA=sin(x);
localmaxima=imregionalmax(DATA);
plithosmaxima=sum(localmaxima); %number of maxima
for i=1:length(localmaxima)
    if localmaxima(i)==1
        maxima1(i)=DATA(i);
    end
end
for i=1:length(maxima1)
    dataxx(i)=i;
end
for i=1:length(dataxx)
    if (maxima1(i)>0)|(maxima1(i)<0)
        maxima(i)=maxima1(i);
        datax(i)=dataxx(i);
    end
end
maxima=maxima(maxima~=0);
datax=datax(datax~=0);
%cubic spline line
cs=spline(datax,[maxima(1) maxima maxima(end)]);
xx=linspace(min(datax),max(datax),length(DATA));
upenvelope=ppval(cs,xx);
localminima=imregionalmin(DATA); %vector me 1 opou iparxei max kai 0 allou
plithosminima=sum(localminima);
for i=1:length(localminima)
    if localminima(i)==1
        minima1(i)=DATA(i);
    end
end
for i=1:length(minima1)
    dataxxx(i)=i;
end
for i=1:length(dataxxx)
    if (minima1(i)>0)|(minima1(i)<0)
        minima(i)=minima1(i);
        datasx(i)=dataxxx(i);
    end
end
minima=minima(minima~=0);
datasx=datasx(datasx~=0);
%cubic spline line
css=spline(datasx,[minima(1) minima minima(end)]);
xxx=linspace(min(datasx),max(datasx),length(DATA));
downenvelope=ppval(css,xxx);
mesitimi=(upenvelope+downenvelope)/2;
h=DATA'-mesitimi';
hold on;
plot (mesitimi,'g-');
%zero crossings
numbercross=sum(diff(sign(h))~=0);
while (abs(plithosmaxima+plithosminima-numbercross)>1)
if mean(h)~=0
  clear DATA maxima minima etc.
    DATA=h;
   clear h;
...
...
...%the same commands as before the while
end
end

Subject: problem with spline

From: dwi

Date: 15 Nov, 2012 15:23:12

Message: 2 of 2

"dwi" wrote in message <k82vdo$do8$1@newscl01ah.mathworks.com>...
> I have a signal in the form of sin(x). I find all the local maxima and connect them with cubic spline and do the same for the minima, thus creating two envelopes (much like the huang-hilbert method if anyone is familiar with it).I calculate the mean value as mesitimi=(upenvelope+downenvelope)/2) and subtract it from my original DATA and all this in a loop which control how many maxima and minima are there. But when the time comes that I have only 1 minima or maxima I can't create the spline so the program returns an error. But I still need to find the mean and subtract it so as to get a 'residual'. How can I do this?
>
> x=[-50:2:50];
> DATA=sin(x);
> localmaxima=imregionalmax(DATA);
> plithosmaxima=sum(localmaxima); %number of maxima
> for i=1:length(localmaxima)
> if localmaxima(i)==1
> maxima1(i)=DATA(i);
> end
> end
> for i=1:length(maxima1)
> dataxx(i)=i;
> end
> for i=1:length(dataxx)
> if (maxima1(i)>0)|(maxima1(i)<0)
> maxima(i)=maxima1(i);
> datax(i)=dataxx(i);
> end
> end
> maxima=maxima(maxima~=0);
> datax=datax(datax~=0);
> %cubic spline line
> cs=spline(datax,[maxima(1) maxima maxima(end)]);
> xx=linspace(min(datax),max(datax),length(DATA));
> upenvelope=ppval(cs,xx);
> localminima=imregionalmin(DATA); %vector me 1 opou iparxei max kai 0 allou
> plithosminima=sum(localminima);
> for i=1:length(localminima)
> if localminima(i)==1
> minima1(i)=DATA(i);
> end
> end
> for i=1:length(minima1)
> dataxxx(i)=i;
> end
> for i=1:length(dataxxx)
> if (minima1(i)>0)|(minima1(i)<0)
> minima(i)=minima1(i);
> datasx(i)=dataxxx(i);
> end
> end
> minima=minima(minima~=0);
> datasx=datasx(datasx~=0);
> %cubic spline line
> css=spline(datasx,[minima(1) minima minima(end)]);
> xxx=linspace(min(datasx),max(datasx),length(DATA));
> downenvelope=ppval(css,xxx);
> mesitimi=(upenvelope+downenvelope)/2;
> h=DATA'-mesitimi';
> hold on;
> plot (mesitimi,'g-');
> %zero crossings
> numbercross=sum(diff(sign(h))~=0);
> while (abs(plithosmaxima+plithosminima-numbercross)>1)
> if mean(h)~=0
> clear DATA maxima minima etc.
> DATA=h;
> clear h;
> ...
> ...
> ...%the same commands as before the while
> end
> end


I forgot to add that when the while breaks:
c=h;
res=DATA'-c;
Then I treat res as mynew DATA and rerun the program from the beginning until the final h is monotonous

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us