Updated linspaces to use LASP_NUMPY_FLOAT_TYPE.

This commit is contained in:
Anne de Jong 2024-07-03 11:56:40 +02:00
parent 6b0442fe90
commit 67bd7e6c9d
6 changed files with 37 additions and 26 deletions

View File

@ -15,6 +15,7 @@ import warnings
import numpy as np
# For designing second-order sections
from scipy.signal import butter
from ..lasp_config import LASP_NUMPY_FLOAT_TYPE
from .fir_design import bandpass_fir_design
from .fir_design import freqResponse as firFreqResponse
@ -254,9 +255,9 @@ class FilterBankDesigner:
fuu = self.fu(xu)
if scale == 'lin':
freq = np.linspace(fll, fuu, npoints)
freq = np.linspace(fll, fuu, npoints, dtype=LASP_NUMPY_FLOAT_TYPE)
elif scale == 'log':
freq = np.logspace(np.log10(fll), np.log10(fuu), npoints)
freq = np.logspace(np.log10(fll), np.log10(fuu), npoints, dtype=LASP_NUMPY_FLOAT_TYPE)
else:
raise ValueError(f'Invalid scale parameter: {scale}')

View File

@ -7,6 +7,7 @@ from dataclasses import dataclass
from dataclasses_json import dataclass_json
from enum import Enum, unique, auto
from .lasp_cpp import DaqChannel
from .lasp_config import LASP_NUMPY_FLOAT_TYPE
"""
Common definitions used throughout the code.
@ -395,7 +396,9 @@ def getTime(fs, N, start=0):
start: Optional start ofset in number of samples
"""
assert N > 0 and fs > 0
return np.linspace(start, start + N/fs, N, endpoint=False)
return np.linspace(
start, start + N / fs, N, endpoint=False, dtype=LASP_NUMPY_FLOAT_TYPE
)
def getFreq(fs, nfft):
@ -408,4 +411,4 @@ def getFreq(fs, nfft):
"""
df = fs / nfft # frequency resolution
K = nfft // 2 + 1 # number of frequency bins
return np.linspace(0, (K-1)*df, K)
return np.linspace(0, (K - 1) * df, K, dtype=LASP_NUMPY_FLOAT_TYPE)

View File

@ -5,9 +5,12 @@ Author: J.A. de Jong - ASCEE
Description: LASP configuration
"""
import numpy as np
from .lasp_cpp import LASP_DOUBLE_PRECISION
__all__ = ["zeros", "ones", "empty", "LASP_NUMPY_FLOAT_TYPE", "LASP_NUMPY_COMPLEX_TYPE"]
if LASP_DOUBLE_PRECISION:
LASP_NUMPY_FLOAT_TYPE = np.float64
LASP_NUMPY_COMPLEX_TYPE = np.complex128
@ -16,28 +19,28 @@ else:
LASP_NUMPY_COMPLEX_TYPE = np.float64
def zeros(shape, dtype=float, order='F'):
if dtype == float:
def zeros(shape, dtype=float, order="F"):
if dtype is float:
return np.zeros(shape, dtype=LASP_NUMPY_FLOAT_TYPE, order=order)
elif dtype == complex:
elif dtype is complex:
return np.zeros(shape, dtype=LASP_NUMPY_COMPLEX_TYPE, order=order)
else:
raise RuntimeError(f"Unknown dtype: {dtype}")
def ones(shape, dtype=float, order='F'):
if dtype == float:
def ones(shape, dtype=float, order="F"):
if dtype is float:
return np.ones(shape, dtype=LASP_NUMPY_FLOAT_TYPE, order=order)
elif dtype == complex:
elif dtype is complex:
return np.ones(shape, dtype=LASP_NUMPY_COMPLEX_TYPE, order=order)
else:
raise RuntimeError(f"Unknown dtype: {dtype}")
def empty(shape, dtype=float, order='F'):
if dtype == float:
def empty(shape, dtype=float, order="F"):
if dtype is float:
return np.empty(shape, dtype=LASP_NUMPY_FLOAT_TYPE, order=order)
elif dtype == complex:
elif dtype is complex:
return np.empty(shape, dtype=LASP_NUMPY_COMPLEX_TYPE, order=order)
else:
raise RuntimeError(f"Unknown dtype: {dtype}")

View File

@ -11,6 +11,7 @@ __all__ = ["OverallFilterBank", "SosOctaveFilterBank", "SosThirdOctaveFilterBank
from .filter.filterbank_design import OctaveBankDesigner, ThirdOctaveBankDesigner
from .lasp_cpp import BiquadBank
from .lasp_config import empty, LASP_NUMPY_FLOAT_TYPE
import numpy as np
@ -46,7 +47,7 @@ class OverallFilterBank:
Ncur = data.shape[0]
tend = tstart + Ncur / self.fs
t = np.linspace(tstart, tend, Ncur, endpoint=False)
t = np.linspace(tstart, tend, Ncur, endpoint=False, dtype=LASP_NUMPY_FLOAT_TYPE)
self.N += Ncur
output["Overall"] = {"t": t, "data": data, "x": 0}
@ -114,7 +115,7 @@ class SosFilterBank:
Ncur = data.shape[0]
tend = tstart + Ncur / self.fs
t = np.linspace(tstart, tend, Ncur, endpoint=False)
t = np.linspace(tstart, tend, Ncur, endpoint=False, dtype=LASP_NUMPY_FLOAT_TYPE)
self.N += Ncur
for i, x in enumerate(self.xs):

View File

@ -4,14 +4,15 @@
Sound level meter implementation
@author: J.A. de Jong - ASCEE
"""
from .lasp_cpp import cppSLM
from .lasp_config import empty
from .lasp_config import empty, LASP_NUMPY_FLOAT_TYPE
import numpy as np
from .lasp_common import (TimeWeighting, FreqWeighting, P_REF)
from .lasp_common import TimeWeighting, FreqWeighting, P_REF
from .filter import SPLFilterDesigner
import logging
__all__ = ['SLM', 'Dummy']
__all__ = ["SLM", "Dummy"]
class Dummy:
@ -89,7 +90,7 @@ class SLM:
elif fw == FreqWeighting.Z:
prefilter = None
else:
raise ValueError(f'Not implemented prefilter {fw}')
raise ValueError(f"Not implemented prefilter {fw}")
# 'Probe' size of filter coefficients
self.nom_txt = []
@ -102,7 +103,7 @@ class SLM:
assert fbdesigner.fs == fs
sos_firstx = fbdesigner.createSOSFilter(self.xs[0]).flatten()
self.nom_txt.append(fbdesigner.nominal_txt(self.xs[0]))
sos = empty((sos_firstx.size, nfilters), dtype=float, order='C')
sos = empty((sos_firstx.size, nfilters), dtype=float, order="C")
sos[:, 0] = sos_firstx
for i, x in enumerate(self.xs[1:]):

View File

@ -7,6 +7,7 @@ Weighting and calibration filter in one
from .lasp_common import FreqWeighting
from .filter import SPLFilterDesigner
from lasp.lasp_config import ones, empty
from ..lasp_config import LASP_NUMPY_FLOAT_TYPE
from .wrappers import FilterBank
import numpy as np
@ -39,7 +40,7 @@ class WeighCal:
self.calfile = calfile
# Frequencies used for the filter design
freq_design = np.linspace(0, 17e3, 3000)
freq_design = np.linspace(0, 17e3, 3000, dtype=LASP_NUMPY_FLOAT_TYPE)
freq_design[-1] = fs/2
# Objective function for the frequency response
@ -140,6 +141,7 @@ class WeighCal:
"""
Returns the frequency response of the designed FIR filter
"""
raise RuntimeError('This code bugs. TODO: Re-implement?')
if freq is None:
freq = np.logspace(1, np.log10(self.fs/2), 500)
return (freq, frp(self.fs, freq, self._firs[chan]),