26 June 2008, 16:33  #1 
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. 
AdSense  

27 June 2008, 00:01  #2 
Moderator
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,499

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).

27 June 2008, 15:32  #3  
Registered User
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351

Quote:
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. 

27 June 2008, 17:05  #4 
Citizen of Elthesh
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. 
28 June 2008, 16:50  #5  
Registered User
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351

Quote:
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. 

28 June 2008, 17:00  #6 
Citizen of Elthesh
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. 
29 June 2008, 01:41  #7  
Registered User
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351

Quote:
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. 

29 June 2008, 01:46  #8 
Citizen of Elthesh
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.

29 June 2008, 02:10  #9 
Moderator
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,499

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 16bit 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. 45 decimal places. 
29 June 2008, 16:30  #10  
Registered User
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351

Quote:
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. 

29 June 2008, 16:45  #11  
Registered User
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351

Quote:
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. 

29 June 2008, 17:03  #12  
Citizen of Elthesh
Join Date: Sep 2003
Location: London, UK
Posts: 949

Quote:
Edit: See e.g. http://www.ucl.ac.uk/Mathematics/geo...es/ser121.html for details. 

29 June 2008, 21:40  #13 
Moderator
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,499

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?

30 June 2008, 01:06  #14  
Registered User
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 65
Posts: 351

Quote:
Thank you very much for your help. 

30 June 2008, 01:15  #15 
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. 
AdSense  

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 

