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:
fitting points to a sin function

Subject: fitting points to a sin function

From: Rafael Herrejon

Date: 7 Mar, 2008 09:07:02

Message: 1 of 6

Hello, i need some help trying to fit some points to a wave.

Using a camera, i can see a rotating coin in the distance.
Then coin gets closer to the camera and then gets farther
again. The image being seen is a serie of ellipses and
circles (depending on the rotating angle). fitting an
ellipse to those images, i can get its major axis( or axis
of rotation A) and the minor axis(B). Seen by the
camera, '0'<B<A, (in reality this is impossible because the
coin width<>0 ).
When the camera sees the coin as a circle, the coin's
relative rotation angle is 0deg(or 180deg) and when it is
seen as a 'line' the coins angle would be 90deg.

This is the axis im able to get from the image processing

a=
[7;7;7;7;7;8;7;7;8;9;8;8;9;9;8;9;10;9;9;9;10;9;10;11;11;10;1
0;12;11;10;12;13;11;11;12;13;11;12;14;12;12;12;14;12;12;14;1
4;12;12;14;13;12;13;15;12;11;12;14;12;11;13;13;11;11;12;11;1
1;11;12;11;10;10;11;10;10;10;10;9;9;9;9;8;8;9;9;8;8;8;8;7;8;
8;8;6;7;7;7;6];

b=
[6;7;7;4;6;7;7;3;7;8;6;5;8;8;5;7;9;8;4;9;9;7;6;10;10;6;8;11;
10;6;10;11;9;7;11;11;7;9;12;12;7;11;12;10;7;12;12;9;9;12;12;
7;11;12;12;7;12;12;9;7;11;11;7;8;11;11;5;9;11;10;4;9;10;8;5;
10;10;7;5;9;9;5;6;9;8;4;6;8;7;3;6;8;6;3;6;7;5;3]


But as you can see the data is not so clean, due to
ilumination or other factors(distance to the camera, blur
in the image, etc).
anyways, if i calculate the relative angle as

theta = acos(b/a);

where ydata=theta

ydata=[ 0.5411; 0; 0; 0.9626; 0.5411; 0.5054; 0; 1.1279;
0.5054; 0.4759; 0.7227; 0.8957; 0.4759; 0.4759; 0.8957;
0.6797; 0.4510; 0.4759; 1.1102; 0; 0.4510; 0.6797; 0.9273;
0.4297; 0.4297; 0.9273; 0.6435; 0.4111; 0.4297; 0.9273;
0.5857; 0.5621; 0.6126; 0.8810; 0.4111; 0.5621; 0.8810;
0.7227; 0.5411; 0; 0.9480; 0.4111; 0.5411; 0.5857; 0.9480;
0.5411; 0.5411; 0.7227; 0.7227; 0.5411; 0.3948; 0.9480;
0.5621; 0.6435; 0; 0.8810; 0; 0.5411; 0.7227; 0.8810;
0.5621; 0.5621; 0.8810; 0.7565; 0.4111; 0; 1.0989; 0.6126;
0.4111; 0.4297; 1.1593; 0.4510; 0.4297; 0.6435; 1.0472; 0;
0; 0.6797; 0.9818; 0; 0; 0.8957; 0.7227; 0; 0.4759; 1.0472;
0.7227; 0; 0.5054; 1.1279; 0.7227; 0; 0.7227; 1.0472;
0.5411; 0; 0.7752; 1.0472];
xdata=0:0.0145:1.4065;


and then i was trying to fix a sinusoidal wave to this
values so i can predict the relative rotation angle further
in time.

The data is full of noise, but a simple sinusoidal wave
(A*sin(B*t+C)) can be seen using only the last 18 values of
the data

xdata=1.16:0.0145:1.4065;
ydata=[ 0; 0.8957; 0.7227; 0; 0.4759; 1.0472; 0.7227; 0;
0.5054; 1.1279; 0.7227; 0; 0.7227; 1.0472; 0.5411; 0;
0.7752; 1.0472];
conttime=xdata(1):0.0005:xdata(size(xdata));
figure
hold on
plot(xdata,ydata,'*');
plot(conttime,abs(1.16*sin((17.5239*pi*conttime)+2.8)),'r-
');
hold off

The parameters A,B and C where found by trial and error and
i havent calculated the error, but i would like to know how
to get the best parameters with a fitting algorithm that
minimizes the error. Measuring the rotation period
acurately its impossible with this noisy data.

What do you recommend me to do?

thank you in advance

Subject: fitting points to a sin function

From: Rafael Herrejon

Date: 1 Apr, 2008 06:22:02

Message: 2 of 6

"Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp>
wrote in message <fqr0jm$273$1@fred.mathworks.com>...
> Hello, i need some help trying to fit some points to a
wave.
>
> Using a camera, i can see a rotating coin in the
distance.
> Then coin gets closer to the camera and then gets farther
> again. The image being seen is a serie of ellipses and
> circles (depending on the rotating angle). fitting an
> ellipse to those images, i can get its major axis( or
axis
> of rotation A) and the minor axis(B). Seen by the
> camera, '0'<B<A, (in reality this is impossible because
the
> coin width<>0 ).
> When the camera sees the coin as a circle, the coin's
> relative rotation angle is 0deg(or 180deg) and when it is
> seen as a 'line' the coins angle would be 90deg.
>
> This is the axis im able to get from the image processing
>
> a=
>
[7;7;7;7;7;8;7;7;8;9;8;8;9;9;8;9;10;9;9;9;10;9;10;11;11;10;1
>
0;12;11;10;12;13;11;11;12;13;11;12;14;12;12;12;14;12;12;14;1
>
4;12;12;14;13;12;13;15;12;11;12;14;12;11;13;13;11;11;12;11;1
>
1;11;12;11;10;10;11;10;10;10;10;9;9;9;9;8;8;9;9;8;8;8;8;7;8;
> 8;8;6;7;7;7;6];
>
> b=
>
[6;7;7;4;6;7;7;3;7;8;6;5;8;8;5;7;9;8;4;9;9;7;6;10;10;6;8;11;
>
10;6;10;11;9;7;11;11;7;9;12;12;7;11;12;10;7;12;12;9;9;12;12;
>
7;11;12;12;7;12;12;9;7;11;11;7;8;11;11;5;9;11;10;4;9;10;8;5;
> 10;10;7;5;9;9;5;6;9;8;4;6;8;7;3;6;8;6;3;6;7;5;3]
>
>
> But as you can see the data is not so clean, due to
> ilumination or other factors(distance to the camera, blur
> in the image, etc).
> anyways, if i calculate the relative angle as
>
> theta = acos(b/a);
>
> where ydata=theta
>
> ydata=[ 0.5411; 0; 0; 0.9626; 0.5411; 0.5054; 0; 1.1279;
> 0.5054; 0.4759; 0.7227; 0.8957; 0.4759; 0.4759; 0.8957;
> 0.6797; 0.4510; 0.4759; 1.1102; 0; 0.4510; 0.6797;
0.9273;
> 0.4297; 0.4297; 0.9273; 0.6435; 0.4111; 0.4297; 0.9273;
> 0.5857; 0.5621; 0.6126; 0.8810; 0.4111; 0.5621; 0.8810;
> 0.7227; 0.5411; 0; 0.9480; 0.4111; 0.5411; 0.5857;
0.9480;
> 0.5411; 0.5411; 0.7227; 0.7227; 0.5411; 0.3948; 0.9480;
> 0.5621; 0.6435; 0; 0.8810; 0; 0.5411; 0.7227; 0.8810;
> 0.5621; 0.5621; 0.8810; 0.7565; 0.4111; 0; 1.0989;
0.6126;
> 0.4111; 0.4297; 1.1593; 0.4510; 0.4297; 0.6435; 1.0472;
0;
> 0; 0.6797; 0.9818; 0; 0; 0.8957; 0.7227; 0; 0.4759;
1.0472;
> 0.7227; 0; 0.5054; 1.1279; 0.7227; 0; 0.7227; 1.0472;
> 0.5411; 0; 0.7752; 1.0472];
> xdata=0:0.0145:1.4065;
>
>
> and then i was trying to fix a sinusoidal wave to this
> values so i can predict the relative rotation angle
further
> in time.
>
> The data is full of noise, but a simple sinusoidal wave
> (A*sin(B*t+C)) can be seen using only the last 18 values
of
> the data
>
> xdata=1.16:0.0145:1.4065;
> ydata=[ 0; 0.8957; 0.7227; 0; 0.4759; 1.0472; 0.7227; 0;
> 0.5054; 1.1279; 0.7227; 0; 0.7227; 1.0472; 0.5411; 0;
> 0.7752; 1.0472];
> conttime=xdata(1):0.0005:xdata(size(xdata));
> figure
> hold on
> plot(xdata,ydata,'*');
> plot(conttime,abs(1.16*sin((17.5239*pi*conttime)+2.8)),'r-
> ');
> hold off
>
> The parameters A,B and C where found by trial and error
and
> i havent calculated the error, but i would like to know
how
> to get the best parameters with a fitting algorithm that
> minimizes the error. Measuring the rotation period
> acurately its impossible with this noisy data.
>
> What do you recommend me to do?
>
> thank you in advance
>
Hello, is anybody out there that could help me? or just
give me a little idea of what should i do? I posted this
almost a month ago and i havent received any suggestion
whatsoever. Please help

Rafael

Subject: fitting points to a sin function

From: Ashish Uthama

Date: 1 Apr, 2008 12:59:27

Message: 3 of 6

some general terms you can search for
curve fitting
regression analysis

maybe even a fourier analysis could give you a rough estimate. Pick the =
f =

with max amplitude, the f might be ~B, the corresponding phase might giv=
e =

you C..and the amplitude might give you A
(am sure its not that simple, you would have to prolly figure out scale =
 =

factors for each.. (based on number of samples/bins etc), but its an ide=
a =

:)

If you have the optimization toolbox, am sure you could use/adapt
some of the minimization problems for this.

This seems like one of those straightforward problems you go 'aagh.. tha=
t =

was it' when someone else points it out.

anyone..?

btw..the line wraps mangles the code and we cant just copy paste. ppl =

usually stop reading when there are lots of numbers :|


On Tue, 01 Apr 2008 02:22:02 -0400, Rafael Herrejon =

<rafael.erasethis@ic.is.tohoku.ac.jp> wrote:

> "Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp>
> wrote in message <fqr0jm$273$1@fred.mathworks.com>...
>> Hello, i need some help trying to fit some points to a
> wave.
>>
>> Using a camera, i can see a rotating coin in the
> distance.
>> Then coin gets closer to the camera and then gets farther
>> again. The image being seen is a serie of ellipses and
>> circles (depending on the rotating angle). fitting an
>> ellipse to those images, i can get its major axis( or
> axis
>> of rotation A) and the minor axis(B). Seen by the
>> camera, '0'<B<A, (in reality this is impossible because
> the
>> coin width<>0 ).
>> When the camera sees the coin as a circle, the coin's
>> relative rotation angle is 0deg(or 180deg) and when it is
>> seen as a 'line' the coins angle would be 90deg.
>>
>> This is the axis im able to get from the image processing
>>
>> a=3D
>>
> [7;7;7;7;7;8;7;7;8;9;8;8;9;9;8;9;10;9;9;9;10;9;10;11;11;10;1
>>
> 0;12;11;10;12;13;11;11;12;13;11;12;14;12;12;12;14;12;12;14;1
>>
> 4;12;12;14;13;12;13;15;12;11;12;14;12;11;13;13;11;11;12;11;1
>>
> 1;11;12;11;10;10;11;10;10;10;10;9;9;9;9;8;8;9;9;8;8;8;8;7;8;
>> 8;8;6;7;7;7;6];
>>
>> b=3D
>>
> [6;7;7;4;6;7;7;3;7;8;6;5;8;8;5;7;9;8;4;9;9;7;6;10;10;6;8;11;
>>
> 10;6;10;11;9;7;11;11;7;9;12;12;7;11;12;10;7;12;12;9;9;12;12;
>>
> 7;11;12;12;7;12;12;9;7;11;11;7;8;11;11;5;9;11;10;4;9;10;8;5;
>> 10;10;7;5;9;9;5;6;9;8;4;6;8;7;3;6;8;6;3;6;7;5;3]
>>
>>
>> But as you can see the data is not so clean, due to
>> ilumination or other factors(distance to the camera, blur
>> in the image, etc).
>> anyways, if i calculate the relative angle as
>>
>> theta =3D acos(b/a);
>>
>> where ydata=3Dtheta
>>
>> ydata=3D[ 0.5411; 0; 0; 0.9626; 0.5411; 0.5054; 0; 1.1279;
>> 0.5054; 0.4759; 0.7227; 0.8957; 0.4759; 0.4759; 0.8957;
>> 0.6797; 0.4510; 0.4759; 1.1102; 0; 0.4510; 0.6797;
> 0.9273;
>> 0.4297; 0.4297; 0.9273; 0.6435; 0.4111; 0.4297; 0.9273;
>> 0.5857; 0.5621; 0.6126; 0.8810; 0.4111; 0.5621; 0.8810;
>> 0.7227; 0.5411; 0; 0.9480; 0.4111; 0.5411; 0.5857;
> 0.9480;
>> 0.5411; 0.5411; 0.7227; 0.7227; 0.5411; 0.3948; 0.9480;
>> 0.5621; 0.6435; 0; 0.8810; 0; 0.5411; 0.7227; 0.8810;
>> 0.5621; 0.5621; 0.8810; 0.7565; 0.4111; 0; 1.0989;
> 0.6126;
>> 0.4111; 0.4297; 1.1593; 0.4510; 0.4297; 0.6435; 1.0472;
> 0;
>> 0; 0.6797; 0.9818; 0; 0; 0.8957; 0.7227; 0; 0.4759;
> 1.0472;
>> 0.7227; 0; 0.5054; 1.1279; 0.7227; 0; 0.7227; 1.0472;
>> 0.5411; 0; 0.7752; 1.0472];
>> xdata=3D0:0.0145:1.4065;
>>
>>
>> and then i was trying to fix a sinusoidal wave to this
>> values so i can predict the relative rotation angle
> further
>> in time.
>>
>> The data is full of noise, but a simple sinusoidal wave
>> (A*sin(B*t+C)) can be seen using only the last 18 values
> of
>> the data
>>
>> xdata=3D1.16:0.0145:1.4065;
>> ydata=3D[ 0; 0.8957; 0.7227; 0; 0.4759; 1.0472; 0.7227; 0;
>> 0.5054; 1.1279; 0.7227; 0; 0.7227; 1.0472; 0.5411; 0;
>> 0.7752; 1.0472];
>> conttime=3Dxdata(1):0.0005:xdata(size(xdata));
>> figure
>> hold on
>> plot(xdata,ydata,'*');
>> plot(conttime,abs(1.16*sin((17.5239*pi*conttime)+2.8)),'r-
>> ');
>> hold off
>>
>> The parameters A,B and C where found by trial and error
> and
>> i havent calculated the error, but i would like to know
> how
>> to get the best parameters with a fitting algorithm that
>> minimizes the error. Measuring the rotation period
>> acurately its impossible with this noisy data.
>>
>> What do you recommend me to do?
>>
>> thank you in advance
>>
> Hello, is anybody out there that could help me? or just
> give me a little idea of what should i do? I posted this
> almost a month ago and i havent received any suggestion
> whatsoever. Please help
>
> Rafael

Subject: fitting points to a sin function

From: John D'Errico

Date: 1 Apr, 2008 14:02:06

Message: 4 of 6

"Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp> wrote in message
<fsskaa$5pl$1@fred.mathworks.com>...

> > The data is full of noise, but a simple sinusoidal wave
> > (A*sin(B*t+C)) can be seen using only the last 18 values
> of
> > the data

(snip)

> Hello, is anybody out there that could help me? or just
> give me a little idea of what should i do? I posted this
> almost a month ago and i havent received any suggestion
> whatsoever. Please help

You asked this question before. I answered
then.

PLOT THE DATA.

Clearly this data is NOT a pure sine wave.

PLOT THE DATA.

Choose a reasonable model for your data.
Choose reasonable starting values. You might
think of using an fft to guess at a reasonable
frequency for the starting values. Now look
at the fft for spikes.

Decide if your coin may not be spinning at
a constant frequency. It might even be
slowing down or wobbling. The fft might
show that behavior.

A sine wave fit of the last 18 points yields
the curve

0.55447 + 0.54702*sin((x - 1.2775)/0.0088558)

But you don't have much data, and this model
does NOT fit the entire curve. There may be
several periodic modes overlaid on top of each
other in this curve, something that would not
surprise me. You can get some information
from an fft.

The fact is, your data is NOT purely sinusoidal,
although it is vaguely periodic.

John

Subject: fitting points to a sin function

From: Rafael Herrejon

Date: 16 Apr, 2008 14:14:03

Message: 5 of 6

I do have the optimization toolbox. As you said, it might
be a straightforward problem but somehow im still not able
to make it work. anyways thank you for the comments

Best Regards
Rafael

Subject: fitting points to a sin function

From: Rafael Herrejon

Date: 16 Apr, 2008 14:59:02

Message: 6 of 6

Hello John

Thank you for your response, as you said, the data i posted
before doesnt seem sinusoidal or even periodic. But still i
would like to learn how to approximate a set of data to a
sinusoidal wave.

Said this, let me start over.

Having a sinusoidal wave given by
y=A*abs(sin((B*pi*time)+C)

some values are "taken" every 0.0145 secs.

xdata=0.0:0.0145:1.4065;
ydata=1.16*abs(sin((xdata*pi*17.5439)+2.7 ));

Having the values of xdata and ydata only, how can i
calculate the coefficients A,B and C?

i tried using

x0 = [0; 0.5411; 2.7] % Starting guess
[x,resnorm] = lsqcurvefit(@fun,x0,xdata,ydata)

where

function F = fun(x,xdata)
F =x(1)*abs(sin((x(2)*pi*xdata)+x(3)));

y is periodic and sinusoidal, but the coefficients obtained
are not correct. I really dont understand what might be
wrong. Could you give me an idea?

Thank you
Rafael

%%Code
xdata=0.0:0.0145:1.4065;
ydata=1.16*abs(sin((xdata*pi*17.5439)+2.7 ));
x0 = [0; 0.5411; 2.7] % Starting guess
[x,resnorm] = lsqcurvefit(@example7f,x0,xdata,ydata)
time=0.0:0.001:1.4065;
y=1.16*abs(sin((time*pi*17.5439)+2.7 ));
curvecalc =x(1)*abs(sin((time*pi*x(2))+ x(3) ));
figure
plot(time,y,'b',xdata,ydata,'g*',time,curvecalc,'r'),
legend('original curve','sampled points','calculated
curve');
%%

Tags for 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