Skip to content

Commit

Permalink
get magnitudes in cython for less memory consumption (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
jopohl authored Feb 22, 2022
1 parent 7642e6d commit eb9d531
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/urh/cythonext/util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import numpy as np
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, int64_t
from libc.stdlib cimport malloc, calloc, free
from cython.parallel import prange
from libc.math cimport log10,pow
from libc.math cimport log10,pow,sqrt
from libcpp cimport bool

from cpython cimport array
Expand Down Expand Up @@ -124,6 +124,17 @@ cpdef uint64_t crc(uint8_t[:] inpt, uint8_t[:] polynomial, uint8_t[:] start_valu

return crc & crc_mask


cpdef np.ndarray[np.double_t, ndim=1] get_magnitudes(IQ arr):
cdef uint64_t i, n = len(arr)

cdef np.ndarray[np.double_t, ndim=1] result = np.zeros(n, dtype = np.double)

for i in range(0, n):
result[i] = sqrt(arr[i][0] * arr[i][0] + arr[i][1] * arr[i][1])

return result

cpdef np.ndarray[np.uint64_t, ndim=1] calculate_cache(uint8_t[:] polynomial, bool reverse_polynomial=False, uint8_t bits=8):
cdef uint8_t j, poly_order = len(polynomial)
cdef uint64_t crc_mask = <uint64_t> pow(2, poly_order - 1) - 1
Expand Down
8 changes: 3 additions & 5 deletions src/urh/signalprocessing/IQArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import numpy as np

from urh.cythonext.util import get_magnitudes


class IQArray(object):
def __init__(self, data: np.ndarray, dtype=None, n=None, skip_conversion=False):
Expand Down Expand Up @@ -75,13 +77,9 @@ def imag(self):
def imag(self, value):
self.__data[:, 1] = value

@property
def magnitudes_squared(self):
return self.real**2.0 + self.imag**2.0

@property
def magnitudes(self):
return np.sqrt(self.magnitudes_squared)
return get_magnitudes(self.__data)

@property
def magnitudes_normalized(self):
Expand Down

0 comments on commit eb9d531

Please sign in to comment.