diff options
| author | Christoph Berg <myon@debian.org> | 2021-09-09 22:49:00 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2021-09-09 22:49:00 +0200 |
| commit | 85895feeaf88bcde26bbfc71e8f6b2047c0f5f41 (patch) | |
| tree | 79807e00771e4af4ce279e368262f2222806102f /map65/libm65/polfit.f90 | |
| parent | 2f095a04ed69d9aa14b47dc4acde6b3225b2d65e (diff) | |
New upstream version 2.5.0~rc6+repackupstream/2.5.0_rc6+repack
Diffstat (limited to 'map65/libm65/polfit.f90')
| -rw-r--r-- | map65/libm65/polfit.f90 | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/map65/libm65/polfit.f90 b/map65/libm65/polfit.f90 new file mode 100644 index 00000000..33e8d44e --- /dev/null +++ b/map65/libm65/polfit.f90 @@ -0,0 +1,87 @@ +subroutine polfit(y,npts,a) + +! Input: y(npts) !Expect npts=4 +! Output: a(1) = baseline +! a(2) = amplitude +! a(3) = theta (deg) + + real y(npts) + real a(3) + real deltaa(3) + integer ipk(1) + save + +! Set starting values: + a(1)=minval(y) + a(2)=maxval(y)-a(1) + ipk=maxloc(y) + a(3)=(ipk(1)-1)*45.0 + + deltaa(1:2)=0.1*a(2) + deltaa(3)=10.0 + nterms=3 + +! Start the iteration + chisqr=0. + chisqr0=1.e6 + iters=10 + + do iter=1,iters + do j=1,nterms + chisq1=fchisq_pol(y,npts,a) + fn=0. + delta=deltaa(j) +10 a(j)=a(j)+delta + chisq2=fchisq_pol(y,npts,a) + if(chisq2.eq.chisq1) go to 10 + if(chisq2.gt.chisq1) then + delta=-delta !Reverse direction + a(j)=a(j)+delta + tmp=chisq1 + chisq1=chisq2 + chisq2=tmp + endif +20 fn=fn+1.0 + a(j)=a(j)+delta + chisq3=fchisq_pol(y,npts,a) + if(chisq3.lt.chisq2) then + chisq1=chisq2 + chisq2=chisq3 + go to 20 + endif + +! Find minimum of parabola defined by last three points + delta=delta*(1./(1.+(chisq1-chisq2)/(chisq3-chisq2))+0.5) + a(j)=a(j)-delta + deltaa(j)=deltaa(j)*fn/3. +! write(*,4000) iter,j,a,deltaa,chisq2 +!4000 format(2i2,2(2x,3f8.2),f12.5) + enddo ! j=1,nterms + chisqr=fchisq_pol(y,npts,a) +! write(*,4000) 0,0,a,chisqr + if(chisqr.lt.1.0) exit + if(deltaa(1).lt.0.01*(a(2)-a(1)) .and. deltaa(2).lt.0.01*(a(2)-a(1)) & + .and. deltaa(3).lt.1.0) exit + if(chisqr/chisqr0.gt.0.99) exit + chisqr0=chisqr + enddo ! iter + a(3)=mod(a(3)+360.0,180.0) + + return +end subroutine polfit + +real function fchisq_pol(y,npts,a) + + real y(npts),a(3) + data rad/57.2957795/ + + chisq = 0. + do i=1,npts + theta=(i-1)*45.0 + yfit=a(1) + a(2)*cos((theta-a(3))/rad)**2 + chisq=chisq + (y(i) - yfit)**2 + enddo + fchisq_pol=chisq + + return +end function fchisq_pol |
