Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Cosine fitting function

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 08-21-2007, 10:16 PM PM User | #1 Jiff New to the CF scene   Join Date: Aug 2007 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts Cosine fitting function I am looking for an algorithm that will take data points and fit a cosine function to them. Anyone know of an algorithm that can fit data to a cosine function in general? The algorithm can be in any language (hence the general) but ultimately I will need it in C. Thanks for your help!
 08-23-2007, 11:28 PM PM User | #2 Jiff New to the CF scene   Join Date: Aug 2007 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts Any help?
 08-24-2007, 06:59 AM PM User | #3 jkd Senior Coder     Join Date: May 2002 Location: metro DC Posts: 3,163 Thanks: 1 Thanked 18 Times in 18 Posts Do you know anything about the distribution that the data points are sampled from? Without that, you would have to do a least-squares regression... Ultimately, any sinusoidal curve should be represented as: y=A*cos(omega*t+phi) Thus, you take 3 partial derivatives of the least squares value and set equal to zero. I did this out I just did out some of the calculus, and the 3 resulting equations cannot be analytically solved for (A,omega,phi) You could run a solver for the 3 variables and get approximations. Alternatively, you could try doing the following: 1. Guess an amplitude. Divide all your data points by it, then take the inverse cosine of these new values. Run a linear regression on this new data set to get omega and phi. 2. Adjust amplitude, repeat (1), and compare errors. The L_2 error is a continuous function of A, so you should be able to find a local minimum without too much work. You could even creatively guess at A... perhaps the average of the two most extreme values, or the average of all the values. Those are the two best possibilities that come to mind. __________________ jasonkarldavis.com
 08-24-2007, 07:02 AM PM User | #4 jkd Senior Coder     Join Date: May 2002 Location: metro DC Posts: 3,163 Thanks: 1 Thanked 18 Times in 18 Posts Incidentally, depending on how tightly grouped on the scale you are examining the values you are sampling are, you might be able to replace the cosine function with the first few terms of its Taylor expansion; the polynomial approximation should be much easier to regress on. __________________ jasonkarldavis.com
 08-24-2007, 07:33 AM PM User | #5 jkd Senior Coder     Join Date: May 2002 Location: metro DC Posts: 3,163 Thanks: 1 Thanked 18 Times in 18 Posts And oops, this was assuming that the sinusoidal curve's offset is 0 along the y-axis (which is not necessarily accurate at all; if that isn't the case, you need to add another parameter to the least-squares analysis). __________________ jasonkarldavis.com

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:47 AM.