3 # Copyright (C) 2013 Chad Hanna
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 from gstlal import reference_psd
24 from optparse import OptionParser
25 from glue.ligolw import utils as ligolw_utils
27 ### A program to fit a PSD to a polynomial
33 ### 1. Fit a PSD to a polynomial with default settings (consider using gstlal_plot_psd to compare the results)::
35 ### $ gstlal_psd_polyfit --output smooth.xml.gz H1refpsd.xml.gz
38 ### 2. Please add more
42 parser = OptionParser(description = __doc__)
43 parser.add_option("--median-window", type = int, default = 8, help="Median window in sample points to apply running median for removing sharp features. Default 8. Setting it to 1 effectively disables this filter.")
44 parser.add_option("--output", metavar = "filename", help = "Set the name of the LIGO light-weight XML file to output")
45 parser.add_option("--poly-order", type = int, default = 10, help = "Set the order of the fitting polynomial. default 10")
46 parser.add_option("--low-fit-freq", type = float, default = 30, help = "Set the low frequency at which to begin fitting in Hz. default 30")
47 parser.add_option("--high-fit-freq", type = float, default = 6500, help = "Set the high frequency at which to stop fitting in Hz. default 6500")
49 options, filenames = parser.parse_args()
51 psds = lal.series.read_psd_xmldoc(ligolw_utils.load_filename(filenames[0], verbose = True, contenthandler = lal.series.PSDContentHandler))
53 # FIXME Don't assume all psds have same resolution
54 psd = psds.values()[0]
55 minsample = int(options.low_fit_freq / psd.deltaF)
56 maxsample = int(options.high_fit_freq / psd.deltaF)
59 for ifo, psd in psds.items():
60 psd = reference_psd.movingmedian(psd, options.median_window)
61 psd = reference_psd.polyfit(psd, minsample, maxsample, options.poly_order, True)
64 reference_psd.write_psd(options.output, psds)