generated from canonical/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use decimal.Decimal and values in Hz
Signed-off-by: gatici <[email protected]>
- Loading branch information
Showing
4 changed files
with
216 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,67 @@ | ||
# Copyright 2025 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""ARFCN calculations for different frequencies.""" | ||
"""Calculate ARFCN for given 5G RF center frequency.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Optional, Union | ||
from decimal import Decimal, getcontext | ||
|
||
getcontext().prec = 28 | ||
KHZ = Decimal("1000") # HZ | ||
MHZ = Decimal("1000000") # HZ | ||
|
||
|
||
@dataclass | ||
class ARFCNRange: | ||
"""ARFCN range class.""" | ||
|
||
lower: float | ||
upper: float | ||
freq_grid: float | ||
freq_offset: float | ||
lower_frequency: int | Decimal # HZ | ||
upper_frequency: int | Decimal # HZ | ||
freq_grid: int | Decimal # HZ | ||
freq_offset: int | Decimal # HZ | ||
arfcn_offset: int | ||
|
||
|
||
LOW = ARFCNRange(0, 3000, 0.005, 0, 0) | ||
MID = ARFCNRange(3000, 24250, 0.015, 3000, 600000) | ||
HIGH = ARFCNRange(24250, 100000, 0.06, 24250, 2016667) | ||
LOW = ARFCNRange(Decimal("0"), Decimal("3000") * MHZ, Decimal("5") * KHZ, 0, 0) | ||
MID = ARFCNRange( | ||
Decimal("3000") * MHZ, | ||
Decimal("24250") * MHZ, | ||
Decimal("15") * KHZ, | ||
Decimal("3000") * MHZ, | ||
600000, | ||
) | ||
HIGH = ARFCNRange( | ||
Decimal("24250") * MHZ, | ||
Decimal("100000") * MHZ, | ||
Decimal("60") * KHZ, | ||
Decimal("24250") * MHZ, | ||
2016667, | ||
) | ||
|
||
|
||
def freq_to_arfcn(frequency: Union[int, float]) -> Optional[int]: | ||
def freq_to_arfcn(frequency: int) -> int: | ||
"""Calculate Absolute Radio Frequency Channel Number (ARFCN). | ||
Args: | ||
frequency (float or int): Center frequency in MHz. | ||
frequency: (int) Center frequency in Hz. | ||
Returns: | ||
arfcn: int if successful, else None | ||
arfcn: int | ||
Raises: | ||
ValueError: If the FREQ_GRID is 0 or frequency is out of range. | ||
""" | ||
if not isinstance(frequency, (int, float)): | ||
raise TypeError(f"Frequency {frequency} is not a numeric value.") | ||
|
||
ranges = [LOW, MID, HIGH] | ||
|
||
frequency = Decimal(frequency) # type: ignore | ||
|
||
for r in ranges: | ||
if r.lower <= frequency < r.upper: | ||
if r.freq_grid == 0: | ||
raise ValueError("FREQ_GRID cannot be zero.") | ||
return int(r.arfcn_offset + ((frequency - r.freq_offset) / r.freq_grid)) | ||
if Decimal(r.lower_frequency) <= frequency < Decimal(r.upper_frequency): | ||
freq_offset = Decimal(r.freq_offset) | ||
freq_grid = Decimal(r.freq_grid) | ||
arfcn_offset = Decimal(r.arfcn_offset) | ||
return int(arfcn_offset + ((frequency - freq_offset) / freq_grid)) | ||
|
||
raise ValueError(f"Frequency {frequency} is out of supported range.") | ||
raise ValueError( | ||
f"Frequency {frequency} is out of supported range. Supported ranges are: {ranges} Hz" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.