Skip to content

Commit

Permalink
Use linear() inside library
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Aug 19, 2023
1 parent 574d350 commit 0a7db7d
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 27 deletions.
16 changes: 8 additions & 8 deletions docs/examples/pulse-shapes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@
"w, H_rc_0p9 = scipy.signal.freqz(rc_0p9, 1, worN=1024, whole=False, fs=sps)\n",
"\n",
"# Compute the relative power in the main lobe of the pulses\n",
"P_rect = 10 * np.log10(np.cumsum(np.abs(H_rect) ** 2) / np.sum(np.abs(H_rect) ** 2))\n",
"P_rc_0p1 = 10 * np.log10(np.cumsum(np.abs(H_rc_0p1) ** 2) / np.sum(np.abs(H_rc_0p1) ** 2))\n",
"P_rc_0p5 = 10 * np.log10(np.cumsum(np.abs(H_rc_0p5) ** 2) / np.sum(np.abs(H_rc_0p5) ** 2))\n",
"P_rc_0p9 = 10 * np.log10(np.cumsum(np.abs(H_rc_0p9) ** 2) / np.sum(np.abs(H_rc_0p9) ** 2))\n",
"P_rect = sdr.db(np.cumsum(np.abs(H_rect) ** 2) / np.sum(np.abs(H_rect) ** 2))\n",
"P_rc_0p1 = sdr.db(np.cumsum(np.abs(H_rc_0p1) ** 2) / np.sum(np.abs(H_rc_0p1) ** 2))\n",
"P_rc_0p5 = sdr.db(np.cumsum(np.abs(H_rc_0p5) ** 2) / np.sum(np.abs(H_rc_0p5) ** 2))\n",
"P_rc_0p9 = sdr.db(np.cumsum(np.abs(H_rc_0p9) ** 2) / np.sum(np.abs(H_rc_0p9) ** 2))\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(w, P_rect, color=\"k\", linestyle=\":\", label=\"Rectangular\")\n",
Expand Down Expand Up @@ -406,10 +406,10 @@
"w, H_srrc_0p9 = scipy.signal.freqz(srrc_0p9, 1, worN=1024, whole=False, fs=sps)\n",
"\n",
"# Compute the relative power in the main lobe of the pulses\n",
"P_rect = 10 * np.log10(np.cumsum(np.abs(H_rect) ** 2) / np.sum(np.abs(H_rect) ** 2))\n",
"P_srrc_0p1 = 10 * np.log10(np.cumsum(np.abs(H_srrc_0p1) ** 2) / np.sum(np.abs(H_srrc_0p1) ** 2))\n",
"P_srrc_0p5 = 10 * np.log10(np.cumsum(np.abs(H_srrc_0p5) ** 2) / np.sum(np.abs(H_srrc_0p5) ** 2))\n",
"P_srrc_0p9 = 10 * np.log10(np.cumsum(np.abs(H_srrc_0p9) ** 2) / np.sum(np.abs(H_srrc_0p9) ** 2))\n",
"P_rect = sdr.db(np.cumsum(np.abs(H_rect) ** 2) / np.sum(np.abs(H_rect) ** 2))\n",
"P_srrc_0p1 = sdr.db(np.cumsum(np.abs(H_srrc_0p1) ** 2) / np.sum(np.abs(H_srrc_0p1) ** 2))\n",
"P_srrc_0p5 = sdr.db(np.cumsum(np.abs(H_srrc_0p5) ** 2) / np.sum(np.abs(H_srrc_0p5) ** 2))\n",
"P_srrc_0p9 = sdr.db(np.cumsum(np.abs(H_srrc_0p9) ** 2) / np.sum(np.abs(H_srrc_0p9) ** 2))\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(w, P_rect, color=\"k\", linestyle=\":\", label=\"Rectangular\")\n",
Expand Down
14 changes: 7 additions & 7 deletions src/sdr/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def ebn0_to_esn0(ebn0: npt.ArrayLike, bps: int, rate: int = 1) -> np.ndarray:
conversions-from-ebn0
"""
ebn0 = np.asarray(ebn0) # Energy per information bit
ecn0 = ebn0 + 10 * np.log10(rate) # Energy per coded bit
esn0 = ecn0 + 10 * np.log10(bps) # Energy per symbol
ecn0 = ebn0 + db(rate) # Energy per coded bit
esn0 = ecn0 + db(bps) # Energy per symbol
return esn0


Expand Down Expand Up @@ -217,7 +217,7 @@ def ebn0_to_snr(ebn0: npt.ArrayLike, bps: int, rate: int = 1, sps: int = 1) -> n
conversions-from-ebn0
"""
esn0 = ebn0_to_esn0(ebn0, bps, rate=rate) # SNR per symbol
snr = esn0 - 10 * np.log10(sps) # SNR per sample
snr = esn0 - db(sps) # SNR per sample
return snr


Expand Down Expand Up @@ -261,8 +261,8 @@ def esn0_to_ebn0(esn0: npt.ArrayLike, bps: int, rate: int = 1) -> np.ndarray:
conversions-from-esn0
"""
esn0 = np.asarray(esn0)
ecn0 = esn0 - 10 * np.log10(bps) # Energy per coded bit
ebn0 = ecn0 - 10 * np.log10(rate) # Energy per information bit
ecn0 = esn0 - db(bps) # Energy per coded bit
ebn0 = ecn0 - db(rate) # Energy per information bit
return ebn0


Expand Down Expand Up @@ -300,7 +300,7 @@ def esn0_to_snr(esn0: npt.ArrayLike, sps: int = 1) -> np.ndarray:
conversions-from-esn0
"""
esn0 = np.asarray(esn0) # SNR per symbol
snr = esn0 - 10 * np.log10(sps) # SNR per sample
snr = esn0 - db(sps) # SNR per sample
return snr


Expand Down Expand Up @@ -384,5 +384,5 @@ def snr_to_esn0(snr: npt.ArrayLike, sps: int = 1) -> np.ndarray:
conversions-from-snr
"""
snr = np.asarray(snr)
esn0 = snr + 10 * np.log10(sps)
esn0 = snr + db(sps)
return esn0
3 changes: 2 additions & 1 deletion src/sdr/_link_budget/_antenna.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy.typing as npt
import scipy.constants

from .._conversion import db
from .._helper import export


Expand Down Expand Up @@ -63,7 +64,7 @@ def parabolic_antenna(

lambda_ = scipy.constants.speed_of_light / freq # Wavelength in meters
G = (np.pi * diameter / lambda_) ** 2 * efficiency # Gain in linear units
G = 10 * np.log10(G) # Gain in dBi
G = db(G) # Gain in dBi

theta = np.arcsin(3.83 * lambda_ / (np.pi * diameter)) # Beamwidth in radians
theta = np.rad2deg(theta) # Beamwidth in degrees
Expand Down
3 changes: 2 additions & 1 deletion src/sdr/_link_budget/_capacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy.typing as npt
import scipy.stats

from .._conversion import linear
from .._helper import export


Expand Down Expand Up @@ -180,7 +181,7 @@ def awgn_capacity(snr: npt.ArrayLike, bandwidth: float | None = None) -> np.ndar
link-budget-channel-capacity
"""
snr = np.asarray(snr)
snr_linear = 10 ** (snr / 10)
snr_linear = linear(snr)

if bandwidth:
return bandwidth * np.log2(1 + snr_linear) # bits/s
Expand Down
2 changes: 1 addition & 1 deletion src/sdr/_measurement/_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ def papr(x: npt.ArrayLike) -> float:
measurement-power
"""
x = np.asarray(x)
return 10 * np.log10(peak_power(x) / average_power(x))
return to_db(peak_power(x) / average_power(x))
10 changes: 5 additions & 5 deletions src/sdr/_modulation/_psk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import scipy.special
from typing_extensions import Literal

from .._conversion import ebn0_to_esn0, esn0_to_ebn0
from .._conversion import ebn0_to_esn0, esn0_to_ebn0, linear
from .._data import unpack
from .._helper import export, extend_docstring
from .._probability import Q
Expand Down Expand Up @@ -130,9 +130,9 @@ def ber(self, ebn0: npt.ArrayLike | None = None, diff_encoded: bool = False) ->
M = self.order
k = self.bps
ebn0 = np.asarray(ebn0)
ebn0_linear = 10 ** (ebn0 / 10)
ebn0_linear = linear(ebn0)
esn0 = ebn0_to_esn0(ebn0, k)
esn0_linear = 10 ** (esn0 / 10)
esn0_linear = linear(esn0)

if not diff_encoded:
if M == 2:
Expand Down Expand Up @@ -219,9 +219,9 @@ def ser(self, esn0: npt.ArrayLike | None = None, diff_encoded: bool = False) ->
M = self.order
k = self.bps
esn0 = np.asarray(esn0)
esn0_linear = 10 ** (esn0 / 10)
esn0_linear = linear(esn0)
ebn0 = esn0_to_ebn0(esn0, k)
ebn0_linear = 10 ** (ebn0 / 10)
ebn0_linear = linear(ebn0)

if not diff_encoded:
if M == 2:
Expand Down
3 changes: 2 additions & 1 deletion src/sdr/_pll.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

from ._conversion import linear
from ._filter import IIR
from ._helper import export
from ._loop_filter import LoopFilter
Expand Down Expand Up @@ -189,7 +190,7 @@ def phase_error_variance(self, cn0: float) -> float:
Examples:
See the :ref:`phase-locked-loop` example.
"""
cn0_linear = 10 ** (cn0 / 10)
cn0_linear = linear(cn0)
return self.Bn / cn0_linear

@property
Expand Down
7 changes: 4 additions & 3 deletions src/sdr/_simulation/_impairment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import numpy.typing as npt

from .._conversion import linear
from .._farrow import FarrowResampler
from .._helper import export
from .._measurement import average_power
Expand Down Expand Up @@ -86,7 +87,7 @@ def awgn(
"""
x = np.asarray(x)
if snr is not None:
snr_linear = 10 ** (snr / 10)
snr_linear = linear(snr)
signal_power = average_power(x)
noise_power = signal_power / snr_linear
elif noise is not None:
Expand Down Expand Up @@ -181,8 +182,8 @@ def iq_imbalance(x: npt.ArrayLike, amplitude: float, phase: float = 0) -> np.nda
phase = np.deg2rad(phase)

# TODO: Should the phase be negative for I?
gain_i = 10 ** (0.5 * amplitude / 20) * np.exp(1j * -0.5 * phase)
gain_q = 10 ** (-0.5 * amplitude / 20) * np.exp(1j * 0.5 * phase)
gain_i = linear(0.5 * amplitude, type="voltage") * np.exp(1j * -0.5 * phase)
gain_q = linear(-0.5 * amplitude, type="voltage") * np.exp(1j * 0.5 * phase)

y = gain_i * x.real + 1j * gain_q * x.imag

Expand Down

0 comments on commit 0a7db7d

Please sign in to comment.