summaryrefslogtreecommitdiff
path: root/lib/flat4.f90
diff options
context:
space:
mode:
authorIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
committerIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
commitd63dd8d081a6fcd0715cd13f26df59fb831769d2 (patch)
treeb9c1f6245147a310aac3762630bad389bbb57335 /lib/flat4.f90
parentc720a3d55c5ec832c29cb7ce50b6fb4b70c55b11 (diff)
Imported Upstream version 1.5.0upstream/1.5.0
Diffstat (limited to 'lib/flat4.f90')
-rw-r--r--lib/flat4.f9050
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/flat4.f90 b/lib/flat4.f90
new file mode 100644
index 00000000..fd96a3ab
--- /dev/null
+++ b/lib/flat4.f90
@@ -0,0 +1,50 @@
+subroutine flat4(s,npts,nflatten)
+
+! Flatten a spectrum for optimum display
+! Input: s(npts) Linear scale in power
+! nflatten If nflatten=0, convert to dB but do not flatten
+! Output: s(npts) Flattened, with dB scale
+
+
+ implicit real*8 (a-h,o-z)
+ real*4 s(6827)
+ real*4 base
+ real*8 x(1000),y(1000),a(5)
+ data nseg/10/,npct/10/
+
+ if(s(1).gt.1.e29) go to 900 !Boundary between Rx intervals: do nothing
+ do i=1,npts
+ s(i)=10.0*log10(s(i)) !Convert to dB scale
+ enddo
+ if(nflatten.eq.0) go to 900
+
+ nlen=npts/nseg !Length of test segment
+ i0=npts/2 !Midpoint
+ k=0
+ do n=2,nseg !Skip first segment, likely rolloff here
+ ib=n*nlen
+ ia=ib-nlen+1
+ if(n.eq.nseg) ib=npts
+ call pctile(s(ia),ib-ia+1,npct,base) !Find lowest npct of points in segment
+ do i=ia,ib
+ if(s(i).le.base) then
+ if (k.lt.1000) k=k+1 !Save these "lower envelope" points
+ x(k)=i-i0
+ y(k)=s(i)
+ endif
+ enddo
+ enddo
+ kz=k
+ a=0.
+ nterms=3
+
+ call polfit(x,y,y,kz,nterms,0,a,chisqr) !Fit a low-order polynomial
+
+ do i=1,npts
+ t=i-i0
+ yfit=a(1)+t*(a(2)+t*(a(3)+t*(a(4)+t*(a(5)))))
+ s(i)=s(i)-yfit !Subtract the fitted baseline
+ enddo
+
+900 return
+end subroutine flat4