On Sunday 09 December 2012 17:50:22 dave wrote:
> On Sun, 2012-12-09 at 19:43 -0600, Jon Elson wrote:
> Zu hilfe! Zu hilfe! Zu hilfe!

This version does not fit the frequency(known, constant).

Joachim 

// uses the lmfit library http://joachimwuttke.de/lmfit/
// used version is 3.4

// [email protected]
// this is released under the LMFIT-BEER-WARE licence (see lmmin.c)
// tab = 2

#include <stdio.h>
#include <math.h>
#include <lmcurve.h>  //assumes that you have installed the lmfit lib

#define anzahl_punkte 25

double t[anzahl_punkte];
double y_1[anzahl_punkte];
double y_2[anzahl_punkte];

float pi=M_PI;
float pi2=2.0*M_PI;

float freq=3.0; //frequenz, wird nicht angepasst, ist konstant

//-------------------------------------------------------------------------------------------------------
void feld_fuellen(void)
        {
        int i;
        for (i = 0; i<anzahl_punkte; i++)
                {
                t[i]=(float)i/anzahl_punkte; // 0.0..1.0 
                y_1[i]=1+2*sin(pi2*freq*t[i]);
                y_2[i]=1+2*sin(pi2*freq*t[i]+0.5*pi2);
                //printf("i:%.4d t:%.2f 1:%6.2f 
2:%6.2f\n",i,t[i],y_1[i],y_2[i]);
                }
         }

//-------------------------------------------------------------------------------------------------------
// model function:
// f1(x)=abs+ampl*sin(2*pi*freq*t+2*pi*phase)
// p[0]=abs
// p[1]=ampl
// p[2]=phase
double f( double t, const double *p )
        {
        double z;
        z=p[0]+p[1]*sin(pi2*freq*t+pi2*p[2]);
  return z; 
        }

//-------------------------------------------------------------------------------------------------------
//adjust the phase/(2*pi) to intervall 0.0 .. 1.0
double pasenverschiebung(double palt)
        {
        double pneu;
        pneu=palt;
        while ((pneu<0.0)||(pneu>1.0))
                {
                if ((pneu<0.0)) pneu=pneu+1.0;
                if ((pneu>1.0)) pneu=pneu-1.0;
                }
        return pneu;
        }       
/*
//-------------------------------------------------------------------------------------------------------
//adjust the phase/(2*pi) to intervall -0.5 .. 0.5
double pasenverschiebung(double palt)
        {
        double pneu;
        pneu=palt;
        while ((pneu<-0.5)||(pneu>+0.5))
                {
                if ((pneu<-0.5)) pneu=pneu+1.0;
                if ((pneu>+0.5)) pneu=pneu-1.0;
                }
        return pneu;
        }       
*/
//-------------------------------------------------------------------------------------------------------
int main()
        {
  int n_par = 3; // Anzahl der Parameter
  double par[3]; 
  int m_dat = anzahl_punkte; 
  int i;
        double phase1,phase2;

        feld_fuellen();

  lm_status_struct status;
  lm_control_struct control = lm_control_double;
  control.printflags = 0; 

        //first fit y_1[]
        //Startwerte
        par[0]=0;
        par[1]=-1;
        par[2]=6.2;

  lmcurve_fit( n_par, par, m_dat, t, y_1, f, &control, &status );
  printf( "Status nach %d Berechnungen:\n  %s\n", status.nfev, 
lm_infmsg[status.info] );
  for ( i = 0; i < n_par; ++i) printf("  par[%i] = %12g\n", i, par[i]);
  printf("Fehler der Anpassung:\n  %12g\n", status.fnorm );

        par[2]=pasenverschiebung(par[2]);
        if (par[1]<0.0) //ampl was negativ
                {
                par[1]=-par[1]; 
                par[2]=par[2]+0.5;
                par[2]=pasenverschiebung(par[2]);
                lmcurve_fit( n_par, par, m_dat, t, y_1, f, &control, &status );
                printf( "Status nach %d Berechnungen:\n  %s\n", status.nfev, 
lm_infmsg[status.info] );
                for ( i = 0; i < n_par; ++i) printf("  par[%i] = %12g\n", i, 
par[i]);
                printf("Fehler der Anpassung:\n  %12g\n", status.fnorm );
                }
        phase1=par[2];

        //second fit y_2[]
        par[0]=0;
        par[1]=-1;
        par[2]=-12.2;

  lmcurve_fit( n_par, par, m_dat, t, y_2, f, &control, &status );
  printf( "Status nach %d Berechnungen:\n  %s\n", status.nfev, 
lm_infmsg[status.info] );
  for ( i = 0; i < n_par; ++i) printf("  par[%i] = %12g\n", i, par[i]);
  printf("Fehler der Anpassung:\n  %12g\n", status.fnorm );

        par[2]=pasenverschiebung(par[2]);
        if (par[1]<0.0) //ampl was negativ
                {
                par[1]=-par[1]; 
                par[2]=par[2]+0.5;
                par[2]=pasenverschiebung(par[2]);
                lmcurve_fit( n_par, par, m_dat, t, y_2, f, &control, &status );
                printf( "Status nach %d Berechnungen:\n  %s\n", status.nfev, 
lm_infmsg[status.info] );
                for ( i = 0; i < n_par; ++i) printf("  par[%i] = %12g\n", i, 
par[i]);
                printf("Fehler der Anpassung:\n  %12g\n", status.fnorm );
                }
        phase2=par[2];

        printf("p1:%6.3f[rad] p2:%6.3f[rad] dp:
%6.3f[rad]\n",pi2*phase1,pi2*phase2,pi2*(phase2-phase1));

  return 0;
        }


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Emc-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/emc-developers

Reply via email to