90 "Power spectrum plot",
92 "Generates a video showing a power spectrum (e.g., as measured by lal_whiten)",
100 "Lower bound of plot in Hz.",
101 0, GObject.G_MAXDOUBLE, 10.0,
102 GObject.PARAM_READWRITE | GObject.PARAM_CONSTRUCT
107 "Upper bound of plot in Hz.",
108 0, GObject.G_MAXDOUBLE, 4000.0,
109 GObject.PARAM_READWRITE | GObject.PARAM_CONSTRUCT
114 Gst.PadTemplate.new(
"sink",
115 Gst.PadDirection.SINK,
116 Gst.PadPresence.ALWAYS,
117 Gst.caps_from_string(
119 "rate = " + GstAudio.AUDIO_RATE_RANGE +
", " +
120 "channels = " + GstAudio.AUDIO_CHANNELS_RANGE +
", " +
121 "format = (string) { F64NE }, " +
122 "delta-f = (double) [0, MAX], " +
123 "endianness = (int) BYTE_ORDER, " +
127 Gst.PadTemplate.new(
"src",
128 Gst.PadDirection.SRC,
129 Gst.PadPresence.ALWAYS,
130 Gst.caps_from_string(
131 matplotlibcaps +
", " +
132 "width = (int) [1, MAX], " +
133 "height = (int) [1, MAX], " +
134 "framerate = (fraction) [0/1, 2147483647/1]"
141 super(lal_spectrumplot, self).
__init__()
151 def do_set_property(self, prop, val):
152 if prop.name ==
"f-min":
154 elif prop.name ==
"f-max":
157 raise AssertionError(
"no property %s" % prop.name)
160 def do_get_property(self, prop):
161 if prop.name ==
"f-min":
163 elif prop.name ==
"f-max":
166 raise AssertionError(
"no property %s" % prop.name)
169 def do_set_caps(self, incaps, outcaps):
175 info = GstAudio.AudioInfo()
176 if info.from_caps(incaps):
180 s = incaps.get_structure(0)
181 if not s
or s.get_name() !=
"audio/x-raw":
183 success, chnls = s.get_int(
"channels")
185 success, rate = s.get_int(
"rate")
188 fmt = s.get_string(
"format")
193 elif fmt ==
"Z128LE":
200 def do_get_unit_size(self, caps):
201 return pipeio.get_unit_size(caps)
204 def do_event(self, event):
205 if event.type == Gst.EVENT_TAG:
206 tags = pipeio.parse_framesrc_tags(event.parse_tag())
213 def do_transform(self, inbuf, outbuf):
218 fig = figure.Figure()
220 fig.set_size_inches(self.
out_width / float(fig.get_dpi()), self.
out_height / float(fig.get_dpi()))
221 axes = fig.gca(rasterized =
True)
223 data = numpy.transpose(pipeio.array_from_audio_buffer(inbuf))
224 f = numpy.arange(len(data[0]), dtype =
"double") * self.
delta_f
226 imin = bisect.bisect_left(f, self.
f_min)
227 imax = bisect.bisect_right(f, self.
f_max)
230 fseries = lal.CreateREAL8FrequencySeries(
235 sampleUnits = lal.Unit(
"s strain^2"),
238 fseries.data.data[:] = psd
239 axes.loglog(f[imin:imax], psd[imin:imax], alpha = 0.7, label =
"%s:%s (%.4g Mpc BNS horizon)" % ((self.
instrument or "Unknown Instrument"), (self.
channel_name or "Unknown Channel").replace(
"_",
r"\_"), HorizonDistance(self.
f_min, self.
f_max, self.
delta_f, 1.4, 1.4)(fseries)))
243 axes.set_title(
r"Spectral Density at %.11g s" % (float(inbuf.timestamp) / Gst.SECOND))
244 axes.set_xlabel(
r"Frequency (Hz)")
245 axes.set_ylabel(
r"Spectral Density (%s)" % self.
sample_units)
246 axes.legend(loc =
"lower left")
253 rgba_buffer = fig.canvas.buffer_rgba(0, 0)
254 rgba_buffer_size = len(rgba_buffer)
260 outbuf[0:rgba_buffer_size] = rgba_buffer
261 outbuf.datasize = rgba_buffer_size
267 outbuf.offset_end = outbuf.offset = Gst.BUFFER_OFFSET_NONE
268 outbuf.timestamp = inbuf.timestamp
269 outbuf.duration = Gst.CLOCK_TIME_NONE
275 return Gst.FlowReturn.OK
278 def do_transform_caps(self, direction, caps):
279 if direction == Gst.PAD_SRC:
284 rate, = [struct[
"framerate"]
for struct
in caps]
286 for struct
in self.get_pad(
"sink").get_pad_template_caps():
287 struct = struct.copy()
288 struct[
"rate"] = rate
289 result.append_structure(struct)
292 elif direction == Gst.PAD_SINK:
297 rate, = [struct[
"rate"]
for struct
in caps]
299 for struct
in self.get_pad(
"src").get_pad_template_caps():
300 struct = struct.copy()
301 struct[
"framerate"] = rate
302 result.append_structure(struct)
305 raise ValueError(direction)
308 def do_transform_size(self, direction, caps, size, othercaps):
309 if direction == Gst.PadDirection.SRC:
314 frames = size * 8 // (caps[0][
"bpp"] * caps[0][
"width"] * caps[0][
"height"])
327 elif direction == Gst.PadDirection.SINK:
335 othercaps = self.get_pad(
"src").get_allowed_caps()
336 return othercaps[0][
"width"] * othercaps[0][
"height"] * othercaps[0][
"bpp"] // 8
338 raise ValueError(direction)
346GObject.type_register(lal_spectrumplot)