Skip to content

Instantly share code, notes, and snippets.

@DavidLutton
Last active April 6, 2019 22:10
Show Gist options
  • Save DavidLutton/328bc003c7632c541c8f68d457d419ee to your computer and use it in GitHub Desktop.
Save DavidLutton/328bc003c7632c541c8f68d457d419ee to your computer and use it in GitHub Desktop.
Find regions of a filter
import skrf as rf
import numpy as np
from matplotlib import pyplot as plt
rf.stylely()
def filter_regions(network, minimum=-3, maximum=-30, *, offset=0.0):
"""Find regions of a filter.
Parameters
----------
network : :class:`~skrf.network.Network` object
Measured network.
minimum : int, float
What .
maximum : int, float
What .
offset : int, float
What .
"""
# print(offset)
# print(minimum+offset)
# param = getattr(network, sparameter)
# data = np.where((np.max(network.s_db) - 3) < network.s_db)[0] # move to ~args
# data = np.where(stop < network.s_db < start)[0]
# data = np.where(np.logical_and(network.s_db < stop, network.s_db < stop))
# data = np.where(np.logical_and(network.s_db > stop, network.s_db < start))[0]
# data = np.where(np.logical_and(minimum > network.s_db, maximum < network.s_db))[0]
data = np.where(np.logical_and(network.s_db <= minimum+offset, maximum+offset < network.s_db))[0]
### data = np.where(np.logical_and(minimum > network.s_db, network.s_db < maximum))[0]
# data = np.where(network.s_db > stop)[0]
# data = np.where(network.s_db < start)[0]
# data = np.where(network.s_db.all(stop < network.s_db < start))
# data = np.where(stop < network.s_db < start)[0]
# pprint(data)
sequences = np.split(data, np.array(np.where(np.diff(data) > 1)[0]) + 1)
# sequences are consecutive clusters of points
#pprint(sequences)
ranges = []
for seq in sequences:
if len(seq) > 1:
if np.ptp([minimum, maximum])*0.95 < np.ptp(network[np.min(seq):np.max(seq)].s_db): # only select regions that are at least 95% of wanted np.ptp
ranges.append((np.min(seq), np.max(seq))) # spans of points
else:
ranges.append(seq[0]) # single point
# If you have very sparse data you may encouter this
# print(ranges)
return ranges # ranges are [(start stop),] in terms of data points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment