Download This Notebook: Frequency_units.ipynb

Configuring frequency units

A frequency’s input unit tells scikit-rf what the supplied numbers mean. Its display unit controls how those frequencies are represented. Internally, Frequency.f always stores values in Hz; Frequency.f_scaled expresses the same frequencies in Frequency.unit.

Set rf.constants.FREQ_UNIT_DEFAULT to choose a default unit for newly created frequency objects and networks, including networks read from Touchstone files. An explicit Network(..., f_unit=...) overrides this default for that network. This setting applies to the current Python process and needs no configuration file. The examples below save the current setting and restore it at the end.

[1]:
from io import StringIO

import numpy as np

import skrf as rf

original_unit = rf.constants.FREQ_UNIT_DEFAULT

Keeping input units

The factory setting is None. It preserves explicit constructor units and file units. When unit is omitted from Frequency or Frequency.from_f, the input is interpreted in Hz.

[2]:
rf.constants.FREQ_UNIT_DEFAULT = None

omitted = rf.Frequency(1, 2, 2)
original = rf.Frequency(1000, 2000, 2, unit="MHz")

print("Omitted unit:", omitted.unit, omitted.f_scaled)
print("Explicit unit:", original.unit, original.f_scaled)
assert omitted.unit == "Hz"
assert original.unit == "MHz"
np.testing.assert_allclose(omitted.f, [1, 2])
np.testing.assert_allclose(original.f, [1e9, 2e9])
Omitted unit: Hz [1. 2.]
Explicit unit: MHz [1000. 2000.]

Choosing a common unit

With a global unit of GHz, omitted Frequency input units are interpreted as GHz. An explicit input unit still determines the meaning of the input values, but the resulting object uses GHz for its representation.

All three constructions below describe the same 1-2 GHz band. For each object, f is [1e9, 2e9], unit is "GHz", and f_scaled is [1, 2].

[3]:
rf.constants.FREQ_UNIT_DEFAULT = "GHz"

implicit = rf.Frequency(1, 2, 2)
explicit = rf.Frequency(1000, 2000, 2, unit="MHz")
from_values = rf.Frequency.from_f([1000, 2000], unit="MHz")

for frequency in (implicit, explicit, from_values):
    print(frequency.f, "Hz ->", frequency.f_scaled, frequency.unit)
    assert frequency.unit == "GHz"
    np.testing.assert_allclose(frequency.f, [1e9, 2e9])
    np.testing.assert_allclose(frequency.f_scaled, [1, 2])
[1.e+09 2.e+09] Hz -> [1. 2.] GHz
[1.e+09 2.e+09] Hz -> [1. 2.] GHz
[1.e+09 2.e+09] Hz -> [1. 2.] GHz

Reading Touchstone data

A Touchstone header defines the unit of the file’s frequency values. scikit-rf uses that unit to read the data. An explicit f_unit chooses the resulting network’s display unit, overriding the global default. If f_unit is omitted, the global default applies; when it is None, the file unit is retained. The absolute frequencies, scattering parameters and impedances are unchanged. For files containing noise data, the noise frequency axis uses the same selected display unit.

This small Touchstone example contains two frequencies, 1000 and 2000 MHz. We use an in-memory file so the notebook needs no downloads or data files. For a file on disk, use rf.Network("example.s1p") in the same way.

[4]:
touchstone_data = "# MHz S RI R 50\n1000 0.1 0\n2000 0.2 0\n"

with StringIO(touchstone_data) as stream:
    stream.name = "example.s1p"
    rf.constants.FREQ_UNIT_DEFAULT = None
    file_units = rf.Network(stream)

with StringIO(touchstone_data) as stream:
    stream.name = "example.s1p"
    rf.constants.FREQ_UNIT_DEFAULT = "GHz"
    configured = rf.Network(stream)

with StringIO(touchstone_data) as stream:
    stream.name = "example.s1p"
    overridden = rf.Network(stream, f_unit="Hz")

print("File units:", file_units.frequency.f_scaled, file_units.frequency.unit)
print("Configured units:", configured.frequency.f_scaled, configured.frequency.unit)
print("Explicit override:", overridden.frequency.f_scaled, overridden.frequency.unit)
assert file_units.frequency.unit == "MHz"
assert configured.frequency.unit == "GHz"
assert overridden.frequency.unit == "Hz"
np.testing.assert_allclose(configured.frequency.f_scaled, [1, 2])
np.testing.assert_allclose(overridden.frequency.f_scaled, [1e9, 2e9])
for network in (configured, overridden):
    np.testing.assert_allclose(network.f, [1e9, 2e9])
    np.testing.assert_array_equal(network.f, file_units.f)
    np.testing.assert_array_equal(network.s, file_units.s)
    np.testing.assert_array_equal(network.z0, file_units.z0)
File units: [1000. 2000.] MHz
Configured units: [1. 2.] GHz
Explicit override: [1.e+09 2.e+09] Hz

Supplying raw frequencies to a Network

Network(f=...) keeps its existing input convention: raw numbers are in Hz unless f_unit specifies another input unit. This differs from omitting unit in a Frequency constructor, which uses the configured unit for input. An explicit f_unit also sets the resulting Network’s display unit. When it is omitted, the display unit follows the global setting, or Hz if it is None.

[5]:
raw_hz = rf.Network(f=[1e9, 2e9], s=[0.1, 0.2])
raw_mhz = rf.Network(f=[1000, 2000], f_unit="MHz", s=[0.1, 0.2])

assert raw_hz.frequency.unit == "GHz"
assert raw_mhz.frequency.unit == "MHz"
np.testing.assert_allclose(raw_hz.frequency.f_scaled, [1, 2])
np.testing.assert_allclose(raw_mhz.frequency.f_scaled, [1000, 2000])
for network in (raw_hz, raw_mhz):
    print(network.frequency.f_scaled, network.frequency.unit)
    np.testing.assert_allclose(network.f, [1e9, 2e9])
[1. 2.] GHz
[1000. 2000.] MHz

Existing objects and copies

Changing the global setting does not change existing objects. The original frequency created earlier still uses MHz, and its copies and slices retain that unit. You can explicitly assign .unit to change an individual object’s representation without changing its absolute frequencies.

Constructing a new Network from an existing Frequency applies an explicit f_unit to the network’s copy. When f_unit is omitted, the global setting applies, or the source unit is retained if the setting is None. The source object is unchanged.

[6]:
copied = original.copy()
sliced = original[1:]
network = rf.Network(frequency=original, s=[0.1, 0.2])

assert original.unit == copied.unit == sliced.unit == "MHz"
assert network.frequency.unit == "GHz"
np.testing.assert_array_equal(network.f, original.f)

copied.unit = "kHz"
print("Original:", original.f_scaled, original.unit)
print("Modified copy:", copied.f_scaled, copied.unit)
print("New network:", network.frequency.f_scaled, network.frequency.unit)
assert original.unit == "MHz"
np.testing.assert_array_equal(copied.f, original.f)
Original: [1000. 2000.] MHz
Modified copy: [1000000. 2000000.] kHz
New network: [1. 2.] GHz

Restoring the previous setting

Restore the setting saved at the start so that later work in this kernel uses the same configuration as before the tutorial. The objects already created above keep their units.

[7]:
rf.constants.FREQ_UNIT_DEFAULT = original_unit