English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 26 June 2008, 16:33   #1
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Help with sine regression

Can anybody point me in the right direction as to how to do a sine regression. I would like to write C code for my Amiga to do it. I’ve been searching the internet for several days and all I can come up with is that TI calculators do it and how to estimate it. What I would like is a numerical method that will give an exact answer, as opposed to a convergent method.

One method I did come across was to do a Direct Fourier Transform, which my calculator does, but the numbers that come out don’t mean anything to me. I don’t see how you get the sine constants (amp, phase, freq, offset) for each sine wave in the signal.

Any information will be appreciated.

Thank you.
Ed Cruse is offline  
Old 27 June 2008, 00:01   #2
Photon
Moderator
Photon's Avatar
 
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,577
It's a "sum" of quotients. I made one once, unfortunately it's on a crashed harddisk that takes 22 hours to restore, so I've never gotten round to it (yet).
Photon is offline  
Old 27 June 2008, 15:32   #3
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Quote:
Originally Posted by Photon View Post
It's a "sum" of quotients. I made one once, unfortunately it's on a crashed harddisk that takes 22 hours to restore, so I've never gotten round to it (yet).
Thank you for replying, but I'm still in the dark. Could you please be a little more specific if you can remember. As a test I generated 8 data points for a simple sine wave, did a FFT on my calculator and came up with numbers that don't mean anything to me. The output format is (real part, imaginary part).

0--------->(0,0)
0.707----->(0,-4)
1--------->(0,0)
0.707----->(0,0)
0--------->(0,0)
-0.707---->(0,0)
-1-------->(0,0)
-0.707---->(0,4)

Sorry for the crazy looking table above, the editor removed all the spaces so I used dashes and a right arrow.

Thank you.
Ed Cruse is offline  
Old 27 June 2008, 17:05   #4
eLowar
Citizen of Elthesh
eLowar's Avatar
 
Join Date: Sep 2003
Location: London, UK
Posts: 949
Well, while I'd have to do the math to see where the exact numbers come from, it does make sense. A sine function contains exactly one frequency, which is why you get one spike in fourier space (plus its mirror image with the opposite sign in the imaginary part).

http://mathworld.wolfram.com/FourierTransformSine.html

See? Two delta functions on opposite sides with opposite signs before i.
eLowar is offline  
Old 28 June 2008, 16:50   #5
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Quote:
Originally Posted by eLowar View Post
Well, while I'd have to do the math to see where the exact numbers come from, it does make sense. A sine function contains exactly one frequency, which is why you get one spike in fourier space (plus its mirror image with the opposite sign in the imaginary part).

http://mathworld.wolfram.com/FourierTransformSine.html

See? Two delta functions on opposite sides with opposite signs before i.
So, you're saying that the output of the transform is a spectrum (frequency verses amplitute)? Since I used y=sin(x) to create the data, shouldn't the amplitude by -1 and 1? If the imaginary part is amplitude what's the real part, phase?

I checked out the link, good stuff, but way over my head. What I really need is something that tells me how to take a signal (made up of data points) and derive all the sin functions that make it up, so I can write the code. The math theory is just to much for me. I had calculus but I didn't do so well, even though I understand it I have to use my calculator to do the calculations.

Thank you for the link, I put it into my favorites so I can keep going back and studying, it looks like a good site in general.
Ed Cruse is offline  
Old 28 June 2008, 17:00   #6
eLowar
Citizen of Elthesh
eLowar's Avatar
 
Join Date: Sep 2003
Location: London, UK
Posts: 949
Well, I expect (again without really feeling like looking into it myself, I'm rather busy right now) you probably used integers for x, which basically means degrees, while the sin() function probably expects radians. Otherwise I'd have to (tentatively) agree with your assessment.

Yes, in fourier space you see the amplitude at frequencies, rather than locations. The frequency of each sine wave in the signal corresponds to the distance of the delta spikes from the center, the amplitude should (if I'm not mistaken) correspond to the amplitude in fourier space. Phase is more complicated, I believe (adding some exponential factor). Note that even if these statements are correct they're still just generalities and there may e.g. be constant factors missing. It's been a while since I've properly worked with this and I'm a little rusty.

A proper mathematician could probably be of more help.
eLowar is offline  
Old 29 June 2008, 01:41   #7
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Quote:
Originally Posted by eLowar View Post
Well, I expect (again without really feeling like looking into it myself, I'm rather busy right now) you probably used integers for x, which basically means degrees, while the sin() function probably expects radians. Otherwise I'd have to (tentatively) agree with your assessment.

Yes, in fourier space you see the amplitude at frequencies, rather than locations. The frequency of each sine wave in the signal corresponds to the distance of the delta spikes from the center, the amplitude should (if I'm not mistaken) correspond to the amplitude in fourier space. Phase is more complicated, I believe (adding some exponential factor). Note that even if these statements are correct they're still just generalities and there may e.g. be constant factors missing. It's been a while since I've properly worked with this and I'm a little rusty.

A proper mathematician could probably be of more help.

Thank you for the info, I'm still in the dark but the light is starting to come on.

I used degrees in double float, my calculator is set in degree mode so it should have worked correctly. I did not know that the frequency is a delta from the center, I thought if was delta from the left or top.

Seems like everybody has done this at one time or another but haven't in a long time and don't remember. I even asked DrMath.com but they haven't gotten back and it's been a couple of days, so they probably won't.

Thanks again.
Ed Cruse is offline  
Old 29 June 2008, 01:46   #8
eLowar
Citizen of Elthesh
eLowar's Avatar
 
Join Date: Sep 2003
Location: London, UK
Posts: 949
Well, look at the formula, k0 is the period (and 1/k0 the frequency; although maybe the other way around ), and the delta spikes are shifted by k0, left and right from the origin (in the imaginary part). I'm unsure about the rest. I might think more about it next week if there's still a need, I should have a little more time then.
eLowar is offline  
Old 29 June 2008, 02:10   #9
Photon
Moderator
Photon's Avatar
 
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,577
Ah! Found it in one of my yellowed notebooks...

sin x = x - (x^3 / 3!) + (x^5 / 5!) - (x^7 / 7!) + (x^9 / 9!) ...

where ^ is "raised to the power of", ie. x^3 = x*x*x

and ! is factorial, ie. 4!=1*2*3*4


Didn't find the asm for it, though. IIRC the angle x was in radians as a signed 16-bit integer and I swapped it down for each "power" except last, and then I had a simple list of precalced factorials longwords, shifted to keep the accumulated sum of quotients within longword bounds.

The result was a signed integer where "1" was $7fffffff, and I think you got one "bit" of result precision for two iterations, so 32 iterations should give 16 bits precision, i.e. 4-5 decimal places.
Photon is offline  
Old 29 June 2008, 16:30   #10
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Quote:
Originally Posted by eLowar View Post
Well, look at the formula, k0 is the period (and 1/k0 the frequency; although maybe the other way around ), and the delta spikes are shifted by k0, left and right from the origin (in the imaginary part). I'm unsure about the rest. I might think more about it next week if there's still a need, I should have a little more time then.

I see what you are saying. I'm going to play around with my calculator with two waves and see what happens. Thank you, and there will probably will be a need for more info, so if you think of more info please let me know.
Ed Cruse is offline  
Old 29 June 2008, 16:45   #11
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Quote:
Originally Posted by Photon View Post
Ah! Found it in one of my yellowed notebooks...

sin x = x - (x^3 / 3!) + (x^5 / 5!) - (x^7 / 7!) + (x^9 / 9!) ...

where ^ is "raised to the power of", ie. x^3 = x*x*x

and ! is factorial, ie. 4!=1*2*3*4


Didn't find the asm for it, though. IIRC the angle x was in radians as a signed 16-bit integer and I swapped it down for each "power" except last, and then I had a simple list of precalced factorials longwords, shifted to keep the accumulated sum of quotients within longword bounds.

The result was a signed integer where "1" was $7fffffff, and I think you got one "bit" of result precision for two iterations, so 32 iterations should give 16 bits precision, i.e. 4-5 decimal places.

It looks to me like what you are showing me is a infinite series which yields sin(x). If I'm not mistaken then that's not what I need, the Amiga math library will do that. If I am mistaken then I appologies, but I don't understand.

What I'm trying to do is do a sin function best fit curve, and I think I can do this by using a fourier transform, but I'm having trouble figuring out how to use the results that my calculator gives me. Before I write code to do a fourier transform I need to know how to use the results. Again if I'm mistaken then I appologies for not being smart enough to understand. I do appreciate the effort you are making.
Ed Cruse is offline  
Old 29 June 2008, 17:03   #12
eLowar
Citizen of Elthesh
eLowar's Avatar
 
Join Date: Sep 2003
Location: London, UK
Posts: 949
Quote:
Originally Posted by Ed Cruse View Post
It looks to me like what you are showing me is a infinite series which yields sin(x). If I'm not mistaken then that's not what I need, the Amiga math library will do that. If I am mistaken then I appologies, but I don't understand.
That's correct, it's a Taylor series for sin(x).

Edit: See e.g. http://www.ucl.ac.uk/Mathematics/geo...es/ser121.html for details.
eLowar is offline  
Old 29 June 2008, 21:40   #13
Photon
Moderator
Photon's Avatar
 
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,577
Oh, I thought you wanted regression, ie. fit data to a curve, and read "sine regression" as some function that describes or fits a sine curve. I saw now that you don't want functions that converge, my bad. So the application is signal analysis?
Photon is offline  
Old 30 June 2008, 01:06   #14
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
Quote:
Originally Posted by Photon View Post
Oh, I thought you wanted regression, ie. fit data to a curve, and read "sine regression" as some function that describes or fits a sine curve. I saw now that you don't want functions that converge, my bad. So the application is signal analysis?
It's signal analysis, but my data is not a real siganl. I've noticed over the years that data seems to be either a straight line or waves. So I'm trying to do a sine regression which is a best fit sine curve for a data set. I think I have figured it out, the next message will explain.

Thank you very much for your help.
Ed Cruse is offline  
Old 30 June 2008, 01:15   #15
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351
eLowar,

I think I’m making a lot of head way with the Fourie Transform. Basically I’m using only the positive side of the Fourie space.

The data point furthest from the origin is a frequency of one, and then goes two, three, four, and so on towards the origin. The amplitude and phase appear to be combined into the real and imaginary parts of the output. After many hours I’ve come up with what I think are the formulas. First the numbers have to be divided by the number of data points in the positive Fourie space and then the following formulas seem to apply. I’m using 32 data points for the input, so I get 32 data points out, which means the positive space has 16 data points.

Phase=atan(rp/ip)
Amp=sqrt(rp^2+ip^2)

It appears the outputs, after being divided by 16, are the sides of a right triangle, the real part is the opposite side and the imaginary part is the adjacent side. The phase is the angle, the amplitude is the hypotenuse.

Does this seem reasonable to you?

Thank you very much for your help, you got me going in the right direction.
Ed Cruse is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sine scroller pmc Coders. Tutorials 95 02 July 2017 16:40
Regression compatibility Crashdisk support.WinUAE 3 01 February 2013 20:09
Debian Linux boot regression mark_k support.WinUAE 15 28 January 2013 18:07
Help with a sine effect bobster Coders. Tutorials 16 17 October 2012 12:11

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:36.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.
Page generated in 0.07940 seconds with 13 queries