gstlal 1.13.0
Loading...
Searching...
No Matches
filters.py
1"""Module for filter elements
2
3"""
4from typing import Union
5
6import gi
7
8gi.require_version('Gst', '1.0')
9from gi.repository import GObject
10from gi.repository import Gst
11
12GObject.threads_init()
13Gst.init(None)
14
15from gstlal import gstpipetools
16from gstlal.pipeparts import pipetools
17
18
19
20def audio_cheb_band(pipeline: pipetools.Pipeline, src: pipetools.Element, lower_frequency: float, upper_frequency: float, poles: int = 8) -> pipetools.Element:
21 """Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency band. The number
22 of poles and the ripple parameter control the rolloff. This element has the advantage over the windowed
23 sinc bandpass and bandreject filter that it is much faster and produces almost as good results. It's only
24 disadvantages are the highly non-linear phase and the slower rolloff compared to a windowed sinc filter
25 with a large kernel. For type 1 the ripple parameter specifies how much ripple in dB is allowed in the
26 passband, i.e. some frequencies in the passband will be amplified by that value. A higher ripple value
27 will allow a faster rolloff. For type 2 the ripple parameter specifies the stopband attenuation. In the
28 stopband the gain will be at most this value. A lower ripple value will allow a faster rolloff. As a
29 special case, a Chebyshev type 1 filter with no ripple is a Butterworth filter.
30
31 Args:
32 pipeline:
33 Gst.Pipeline, the pipeline to which the new element will be added
34 src:
35 Gst.Element, the source element
36 lower_frequency:
37 float, Start frequency of the band (Hz)
38 upper_frequency:
39 float, Stop frequency of the band (Hz)
40 poles:
41 int, Number of poles to use, will be rounded up to the next multiple of four
42
43 References:
44 [1] https://gstreamer.freedesktop.org/documentation/audiofx/audiochebband.html?gi-language=python
45
46 Returns:
47 Element
48 """
49 return pipetools.make_element_with_src(pipeline, src, "audiochebband", lower_frequency=lower_frequency, upper_frequency=upper_frequency, poles=poles)
50
51
52
53def audio_cheb_limit(pipeline: pipetools.Pipeline, src: pipetools.Element, cutoff: float, mode: int = 0, poles: int = 8, type: int = 1, ripple: float = 0.25) -> pipetools.Element:
54 """Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the cutoff frequency (high-pass).
55 The number of poles and the ripple parameter control the rolloff. This element has the advantage over the windowed sinc lowpass
56 and highpass filter that it is much faster and produces almost as good results. It's only disadvantages are the highly non-linear
57 phase and the slower rolloff compared to a windowed sinc filter with a large kernel. For type 1 the ripple parameter specifies
58 how much ripple in dB is allowed in the passband, i.e. some frequencies in the passband will be amplified by that value. A higher
59 ripple value will allow a faster rolloff. For type 2 the ripple parameter specifies the stopband attenuation. In the stopband the
60 gain will be at most this value. A lower ripple value will allow a faster rolloff. As a special case, a Chebyshev type 1 filter
61 with no ripple is a Butterworth filter.
62
63 Args:
64 pipeline:
65 Gst.Pipeline, the pipeline to which the new element will be added
66 src:
67 Gst.Element, the source element
68 cutoff:
69 float, Cut off frequency (Hz)
70 mode:
71 int, default 0, 0 for low-pass or 1 for high-pass
72 poles:
73 int, Number of poles to use, will be rounded up to the next multiple of four
74 type:
75 int, default 1, Type of the chebychev filter
76 ripple:
77 float, default 0,25, Amount of ripple (dB)
78
79 References:
80 [1] https://gstreamer.freedesktop.org/documentation/audiofx/audiocheblimit.html?gi-language=python
81
82 Returns:
83 Element
84 """
85 return pipetools.make_element_with_src(pipeline, src, "audiocheblimit", cutoff=cutoff, mode=mode, poles=poles, type=type, ripple=ripple)
86
87
88
89def caps(pipeline: pipetools.Pipeline, src: pipetools.Element, caps: Union[str, pipetools.Caps], **properties: dict) -> pipetools.Element:
90 """The element does not modify data as such, but can enforce limitations on the data format.
91 Note: this element does *not* act as a filter on the data of the source, but rather as a filter on the
92 metadata (CAPS) of the source element.
93
94 Args:
95 pipeline:
96 Gst.Pipeline, the pipeline to which the new element will be added
97 src:
98 Gst.Element, the source element
99 caps:
100 str or Gst.Caps, the capabilities specification to limit the source data format
101 **properties:
102 dict, keyword arguments to be set as element properties
103
104 References:
105 [1] capsfilter docs: https://gstreamer.freedesktop.org/documentation/coreelements/capsfilter.html?gi-language=python
106
107 Returns:
108 Element, the source element limited by the given caps (capabilities)
109 """
110 return pipetools.make_element_with_src(pipeline, src, "capsfilter", caps=gstpipetools.to_caps(caps), **properties)
111
112
113
114def drop(pipeline: pipetools.Pipeline, src: pipetools.Element, drop_samples: int = 0) -> pipetools.Element:
115 """Drop samples from the start of a stream
116
117 Args:
118 pipeline:
119 Gst.Pipeline, the pipeline to which the new element will be added
120 srcs:
121 Iterable[Gst.Element], the source elements
122 drop_samples:
123 int, default 0, number of samples to drop from the beginning of a stream
124
125 References:
126 Implementation: gstlal/gst/lal/gstlal_drop.c
127
128 Returns:
129 Element
130 """
131 return pipetools.make_element_with_src(pipeline, src, "lal_drop", drop_samples=drop_samples)
132
133
134
135def fir(pipeline: pipetools.Pipeline, src: pipetools.Element, kernel: pipetools.GValueArray, latency, **properties: dict) -> pipetools.Element:
136 """Generic audio FIR filter. Before usage the "kernel" property has to be set to the filter kernel that should be
137 used and the "latency" property has to be set to the latency (in samples) that is introduced by the filter kernel.
138 Setting a latency of n samples will lead to the first n samples being dropped from the output and n samples added
139 to the end.
140
141 The filter kernel describes the impulse response of the filter. To calculate the frequency response of the filter
142 you have to calculate the Fourier Transform of the impulse response.
143
144 To change the filter kernel whenever the sampling rate changes the "rate-changed" signal can be used. This should
145 be done for most FIR filters as they're depending on the sampling rate.
146
147 Args:
148 pipeline:
149 Gst.Pipeline, the pipeline to which the new element will be added
150 src:
151 Gst.Element, the source element
152 kernel:
153 Gst.GValueArray, filter kernel for the FIR filter
154 latency:
155 int, filter latency in samples
156 **properties:
157 dict, keyword arguments to be set as element properties
158
159 References:
160 [1] audiofirfilter docs: https://gstreamer.freedesktop.org/documentation/audiofx/audiofirfilter.html?gi-language=python
161
162 Returns:
163 Element, the FIR element
164 """
165 properties.update((name, val) for name, val in (("kernel", kernel), ("latency", latency)) if val is not None)
166 return pipetools.make_element_with_src(pipeline, src, "audiofirfilter", **properties)
167
168
169
170def gate(pipeline: pipetools.Pipeline, src: pipetools.Element, threshold: float = None, control: pipetools.Element = None, **properties) -> pipetools.Element:
171 """Flag buffers as gaps based on the value of a control input
172
173 Args:
174 pipeline:
175 Gst.Pipeline, the pipeline to which the new element will be added
176 src:
177 Gst.Element, the source element
178 threshold:
179 float, default None, uutput will be flagged as non-gap when magnitude of control input is >= this value. See also invert-control.
180 control:
181 Element, optional control element input
182 **properties:
183 emit_signals:
184 bool, Emit start and stop signals (rate-changed is always emited). The start and stop signals
185 are emited on gap-to-non-gap and non-gap-to-gap transitions in the output stream respectively.
186
187 References:
188 Implementation: gstlal/gst/lal/gstlal_gate.c
189
190 Returns:
191 Element
192 """
193 if threshold is not None:
194 elem = pipetools.make_element_with_src(pipeline, None, "lal_gate", threshold=threshold, **properties)
195 else:
196 elem = pipetools.make_element_with_src(pipeline, None, "lal_gate", **properties)
197 for peer, padname in ((src, "sink"), (control, "control")):
198 if isinstance(peer, Gst.Pad):
199 peer.get_parent_element().link_pads(peer, elem, padname)
200 elif peer is not None:
201 peer.link_pads(None, elem, padname)
202 return elem
203
204
205
206def iir(pipeline: pipetools.Pipeline, src: pipetools.Element, a: pipetools.ValueArray, b: pipetools.ValueArray) -> pipetools.Element:
207 """aGeneric audio IIR filter. Before usage the "a" and "b" properties have to be set to the filter coefficients
208 that should be used.
209
210 The filter coefficients describe the numerator and denominator of the transfer function.
211
212 To change the filter coefficients whenever the sampling rate changes the "rate-changed" signal can be used.
213 This should be done for most IIR filters as they're depending on the sampling rate.
214
215 convention is z = \exp(-i 2 \pi f / f_{\rm sampling})
216 H(z) = (\sum_{j=0}^{N} a_j z^{-j}) / (\sum_{j=0}^{N} (-1)^{j} b_j z^{-j})
217
218 Args:
219 pipeline:
220 Gst.Pipeline, the pipeline to which the new element will be added
221 src:
222 Gst.Element, the source element
223 a:
224 ValueArray, Filter coefficients (denominator of transfer function)
225 b:
226 ValueArray, Filter coefficients (numerator of transfer function)
227
228 References:
229 [1] audioiirfilter docs: https://gstreamer.freedesktop.org/documentation/audiofx/audioiirfilter.html?gi-language=python
230
231 Returns:
232 Element, IIR of the sources
233 """
234 #
235 return pipetools.make_element_with_src(pipeline, src, "audioiirfilter", a=a, b=b)
236
237
238
239def remove_fake_disconts(pipeline: pipetools.Pipeline, src: pipetools.Element, silent: bool = True) -> pipetools.Element:
240 """Fix incorrectly-set discontinuity flags
241
242 Args:
243 pipeline:
244 Gst.Pipeline, the pipeline to which the new element will be added
245 srcs:
246 Iterable[Gst.Element], the source elements
247 silent:
248 bool, default True, if True Don't print a message when alterning the flags in a buffer.
249
250 References:
251 Implementation: gstal/gst/lal/gstlal_nofakedisconts.c
252
253 Returns:
254 Element
255 """
256 return pipetools.make_element_with_src(pipeline, src, "lal_nofakedisconts", silent=silent)
257
258
259
260def state_vector(pipeline: pipetools.Pipeline, src: pipetools.Element, **properties) -> pipetools.Element:
261 """Converts a state vector stream into booleans, for example to drive a lal_gate element.
262
263 Args:
264 pipeline:
265 Gst.Pipeline, the pipeline to which the new element will be added
266 src:
267 Gst.Element, the source element
268 **properties:
269
270 References:
271 Implementation: gstlal/gst/lal/gstlal_statevector.c
272
273 Returns:
274 Element
275 """
276 return pipetools.make_element_with_src(pipeline, src, "lal_statevector", **properties)
277
278
279
280def inject(pipeline: pipetools.Pipeline, src: pipetools.Element, filename: str) -> pipetools.Element:
281 """An injection routine calling lalsimulation waveform generators
282
283 Args:
284 pipeline:
285 Gst.Pipeline, the pipeline to which the new element will be added
286 src:
287 Gst.Element, the source element
288 filename:
289 str, path to xml file Name of LIGO Light Weight XML file containing list(s) of software injections
290
291 References:
292 Implementation: gstlal/gst/lal/gstlal_simulation.c
293
294 Returns:
295 Element
296 """
297 return pipetools.make_element_with_src(pipeline, src, "lal_simulation", xml_location=filename)
pipetools.Element caps(pipetools.Pipeline pipeline, pipetools.Element src, Union[str, pipetools.Caps] caps, **dict properties)
Adds a capsfilter element to a pipeline with useful default properties.
Definition filters.py:89
pipetools.Element gate(pipetools.Pipeline pipeline, pipetools.Element src, float threshold=None, pipetools.Element control=None, **properties)
Adds a lal_gate element to a pipeline with useful default properties.
Definition filters.py:170
pipetools.Element drop(pipetools.Pipeline pipeline, pipetools.Element src, int drop_samples=0)
Adds a lal_whiten element to a pipeline with useful default properties.
Definition filters.py:114
pipetools.Element state_vector(pipetools.Pipeline pipeline, pipetools.Element src, **properties)
Adds a lal_statevector element to a pipeline with useful default properties.
Definition filters.py:260
pipetools.Element remove_fake_disconts(pipetools.Pipeline pipeline, pipetools.Element src, bool silent=True)
Adds a lal_nofakedisconts element to a pipeline with useful default properties.
Definition filters.py:239
pipetools.Element inject(pipetools.Pipeline pipeline, pipetools.Element src, str filename)
Adds a lal_simulation element to a pipeline with useful default properties.
Definition filters.py:280
pipetools.Element fir(pipetools.Pipeline pipeline, pipetools.Element src, pipetools.GValueArray kernel, latency, **dict properties)
Adds a audiofirfilter element to a pipeline with useful default properties.
Definition filters.py:135
pipetools.Element audio_cheb_limit(pipetools.Pipeline pipeline, pipetools.Element src, float cutoff, int mode=0, int poles=8, int type=1, float ripple=0.25)
Adds a audiocheblimit element to a pipeline with useful default properties.
Definition filters.py:53
pipetools.Element iir(pipetools.Pipeline pipeline, pipetools.Element src, pipetools.ValueArray a, pipetools.ValueArray b)
Adds a audioiirfilter element to a pipeline with useful default properties.
Definition filters.py:206
pipetools.Element audio_cheb_band(pipetools.Pipeline pipeline, pipetools.Element src, float lower_frequency, float upper_frequency, int poles=8)
Adds a audiochebband element to a pipeline with useful default properties.
Definition filters.py:20