Thread Subject: Sine wave curve fitting

Subject: Sine wave curve fitting

From: Supreet

Date: 9 Sep, 2009 17:41:20

Message: 1 of 6

Hi all,

I wanted some advice and suggestion with regards to curve fitting to an almost sinusoidal data array.

I am trying to do dynamic analysis of a closed loop position system. In the loop is a micro-controller that samples data (sampling time is 1 ms). It saves data for a couple of cycles and then transmits it to a computer as a text file. I have added a sample data set at the end of this post.

I have used MATLAB to import this text file, plotted it and used the Curve Fitting Toolbox but couldnt get an equation for the sine wave.

Can anyone suggest a better method.

Thanks.

a sample data looks like this:
29
29
29
28
37
44
56
67
79
83
88
96
102
112
112
112
109
102
96
88
80
71
63
55
50
45
41
38
35
33
32
31
30
30
29
38
45
55
66
76
85
93
95
100
104
113
114
108
102
95
87
79
70
62
55
49
45
40
37
34
32
31
30
29
29
29
41
47
55
67
78
86
93
100
111
111
111
110
109

Subject: Sine wave curve fitting

From: jrenfree

Date: 9 Sep, 2009 18:20:08

Message: 2 of 6

On Sep 9, 10:41 am, "Supreet " <supreet....@gmail.com> wrote:
> Hi all,
>
> I wanted some advice and suggestion with regards to curve fitting to an almost sinusoidal data array.
>
> I am trying to do dynamic analysis of a closed loop position system. In the loop is a micro-controller that samples data (sampling time is 1 ms). It saves data for a couple of cycles and then transmits it to a computer as a text file. I have added a sample data set at the end of this post.
>
> I have used MATLAB to import this text file, plotted it and used the Curve Fitting Toolbox but couldnt get an equation for the sine wave.
>
> Can anyone suggest a better method.
>
> Thanks.
>
> a sample data looks like this:
> 29
> 29
> 29
> 28
> 37
> 44
> 56
> 67
> 79
> 83
> 88
> 96
> 102
> 112
> 112
> 112
> 109
> 102
> 96
> 88
> 80
> 71
> 63
> 55
> 50
> 45
> 41
> 38
> 35
> 33
> 32
> 31
> 30
> 30
> 29
> 38
> 45
> 55
> 66
> 76
> 85
> 93
> 95
> 100
> 104
> 113
> 114
> 108
> 102
> 95
> 87
> 79
> 70
> 62
> 55
> 49
> 45
> 40
> 37
> 34
> 32
> 31
> 30
> 29
> 29
> 29
> 41
> 47
> 55
> 67
> 78
> 86
> 93
> 100
> 111
> 111
> 111
> 110
> 109

What Matlab function are you using to try and do the curve fitting?

You could try using nlinfit with a sine wave function along the lines
of A+Bsin(C)

Subject: Sine wave curve fitting

From: eige

Date: 9 Sep, 2009 20:31:20

Message: 3 of 6

Just use an FFT. An FFT of size data gives a least squares fit to the
set of complex exponentials in DFTMTX(length(data));

On Sep 9, 1:41 pm, "Supreet " <supreet....@gmail.com> wrote:
> Hi all,
>
> I wanted some advice and suggestion with regards to curve fitting to an almost sinusoidal data array.
>
> I am trying to do dynamic analysis of a closed loop position system. In the loop is a micro-controller that samples data (sampling time is 1 ms). It saves data for a couple of cycles and then transmits it to a computer as a text file. I have added a sample data set at the end of this post.
>
> I have used MATLAB to import this text file, plotted it and used the Curve Fitting Toolbox but couldnt get an equation for the sine wave.
>
> Can anyone suggest a better method.
>
> Thanks.
>
> a sample data looks like this:
> 29
> 29
> 29
> 28
> 37
> 44
> 56
> 67
> 79
> 83
> 88
> 96
> 102
> 112
> 112
> 112
> 109
> 102
> 96
> 88
> 80
> 71
> 63
> 55
> 50
> 45
> 41
> 38
> 35
> 33
> 32
> 31
> 30
> 30
> 29
> 38
> 45
> 55
> 66
> 76
> 85
> 93
> 95
> 100
> 104
> 113
> 114
> 108
> 102
> 95
> 87
> 79
> 70
> 62
> 55
> 49
> 45
> 40
> 37
> 34
> 32
> 31
> 30
> 29
> 29
> 29
> 41
> 47
> 55
> 67
> 78
> 86
> 93
> 100
> 111
> 111
> 111
> 110
> 109

Subject: Sine wave curve fitting

From: Supreet

Date: 29 Sep, 2009 17:12:03

Message: 4 of 6

eige,

I shall try FFT (i havent used it before). The thing I am interested in is getting an equation in terms of A*sin(wt+ph), where A is its amplitude, w is the frequency and ph is its phase.

thx!

eige <jeicke@gmail.com> wrote in message <ed2be6ff-d8dc-4f16-8348-507b8d03f51a@m11g2000yqf.googlegroups.com>...
> Just use an FFT. An FFT of size data gives a least squares fit to the
> set of complex exponentials in DFTMTX(length(data));
>
> On Sep 9, 1:41?pm, "Supreet " <supreet....@gmail.com> wrote:
> > Hi all,
> >
> > I wanted some advice and suggestion with regards to curve fitting to an almost sinusoidal data array.
> >
> > I am trying to do dynamic analysis of a closed loop position system. In the loop is a micro-controller that samples data (sampling time is 1 ms). It saves data for a couple of cycles and then transmits it to a computer as a text file. I have added a sample data set at the end of this post.
> >
> > I have used MATLAB to import this text file, plotted it and used the Curve Fitting Toolbox but couldnt get an equation for the sine wave.
> >
> > Can anyone suggest a better method.
> >
> > Thanks.
> >
> > a sample data looks like this:
> > 29
> > 29
> > 29
> > 28
> > 37
> > 44
> > 56
> > 67
> > 79
> > 83
> > 88
> > 96
> > 102
> > 112
> > 112
> > 112
> > 109
> > 102
> > 96
> > 88
> > 80
> > 71
> > 63
> > 55
> > 50
> > 45
> > 41
> > 38
> > 35
> > 33
> > 32
> > 31
> > 30
> > 30
> > 29
> > 38
> > 45
> > 55
> > 66
> > 76
> > 85
> > 93
> > 95
> > 100
> > 104
> > 113
> > 114
> > 108
> > 102
> > 95
> > 87
> > 79
> > 70
> > 62
> > 55
> > 49
> > 45
> > 40
> > 37
> > 34
> > 32
> > 31
> > 30
> > 29
> > 29
> > 29
> > 41
> > 47
> > 55
> > 67
> > 78
> > 86
> > 93
> > 100
> > 111
> > 111
> > 111
> > 110
> > 109

Subject: Sine wave curve fitting

From: TideMan

Date: 30 Sep, 2009 07:50:39

Message: 5 of 6

On Sep 30, 6:12 am, "Supreet " <supreet....@gmail.com> wrote:
> eige,
>
> I shall try FFT (i havent used it before). The thing I am interested in is getting an equation in terms of  A*sin(wt+ph), where A is its amplitude, w is the frequency and ph is its phase.
>
> thx!
>
> eige <jei...@gmail.com> wrote in message <ed2be6ff-d8dc-4f16-8348-507b8d03f...@m11g2000yqf.googlegroups.com>...
> > Just use an FFT.  An FFT of size data gives a least squares fit to the
> > set of complex exponentials in DFTMTX(length(data));
>
> > On Sep 9, 1:41?pm, "Supreet " <supreet....@gmail.com> wrote:
> > > Hi all,
>
> > > I wanted some advice and suggestion with regards to curve fitting to an almost sinusoidal data array.
>
> > > I am trying to do dynamic analysis of a closed loop position system. In the loop is a micro-controller that samples data (sampling time is 1 ms). It saves data for a couple of cycles and then transmits it to a computer as a text file. I have added a sample data set at the end of this post.
>
> > > I have used MATLAB to import this text file, plotted it and used the Curve Fitting Toolbox but couldnt get an equation for the sine wave.
>
> > > Can anyone suggest a better method.
>
> > > Thanks.
>
> > > a sample data looks like this:
> > > 29
> > > 29
> > > 29
> > > 28
> > > 37
> > > 44
> > > 56
> > > 67
> > > 79
> > > 83
> > > 88
> > > 96
> > > 102
> > > 112
> > > 112
> > > 112
> > > 109
> > > 102
> > > 96
> > > 88
> > > 80
> > > 71
> > > 63
> > > 55
> > > 50
> > > 45
> > > 41
> > > 38
> > > 35
> > > 33
> > > 32
> > > 31
> > > 30
> > > 30
> > > 29
> > > 38
> > > 45
> > > 55
> > > 66
> > > 76
> > > 85
> > > 93
> > > 95
> > > 100
> > > 104
> > > 113
> > > 114
> > > 108
> > > 102
> > > 95
> > > 87
> > > 79
> > > 70
> > > 62
> > > 55
> > > 49
> > > 45
> > > 40
> > > 37
> > > 34
> > > 32
> > > 31
> > > 30
> > > 29
> > > 29
> > > 29
> > > 41
> > > 47
> > > 55
> > > 67
> > > 78
> > > 86
> > > 93
> > > 100
> > > 111
> > > 111
> > > 111
> > > 110
> > > 109

Well, FFT is exactly the wrong way to go about fitting a single sine
wave to your data.
Ignore this bad advice. You will spend a lot of time and get nowhere.
Better to follow jrenfree's advice.
And if you know the frequency a priori, it becomes a linear least
squares problem that can be solved using Matlab's \ operator.

Subject: Sine wave curve fitting

From: Frederic Moisy

Date: 30 Sep, 2009 21:37:01

Message: 6 of 6

You could try the Ezyfit toolbox
http://www.mathworks.com/matlabcentral/fileexchange/10176

Just fit your data with the command :
showfit('f(t) = A*sin(wt+ph); A=2; w=3; ph=1');
(where you have to provide suitable initial guesses for A, w, and ph)

F.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
sine wave curve... Supreet 29 Sep, 2009 13:14:09
sine curve fitting Supreet 9 Sep, 2009 13:44:05
rssFeed for this Thread

Contact us at files@mathworks.com