Merged in rpi.

This commit is contained in:
Anne de Jong 2024-06-17 15:51:21 +02:00
commit 96b3fd5371
12 changed files with 56 additions and 24 deletions

View File

@ -1,17 +1,27 @@
cmake_minimum_required (VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(LASP LANGUAGES C CXX VERSION 1.1) project(LASP LANGUAGES C CXX VERSION 1.1)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED) set(CMAKE_CXX_STANDARD_REQUIRED)
option(LASP_DOUBLE_PRECISION "Compile as double precision floating point" ON) if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
set(RPI TRUE)
else()
set(RPI FALSE)
endif()
# Setting defaults for PortAudio and RtAudio backend, depending on Linux / # Setting defaults for PortAudio and RtAudio backend, depending on Linux /
# Windows. # Windows.
set(DEFAULT_DOUBLE_PRECISION ON)
if(WIN32) if(WIN32)
set(DEFAULT_RTAUDIO OFF) set(DEFAULT_RTAUDIO OFF)
set(DEFAULT_PORTAUDIO ON) set(DEFAULT_PORTAUDIO ON)
set(DEFAULT_ULDAQ OFF) set(DEFAULT_ULDAQ OFF)
elseif(${RPI})
set(DEFAULT_RTAUDIO OFF)
set(DEFAULT_PORTAUDIO ON)
set(DEFAULT_ULDAQ OFF)
set(DEFAULT_DOUBLE_PRECISION OFF)
else() else()
set(DEFAULT_RTAUDIO OFF) set(DEFAULT_RTAUDIO OFF)
set(DEFAULT_PORTAUDIO ON) set(DEFAULT_PORTAUDIO ON)
@ -19,6 +29,7 @@ else()
endif() endif()
option(LASP_DOUBLE_PRECISION "Compile as double precision floating point" ${DEFAULT_DOUBLE_PRECISION})
option(LASP_HAS_RTAUDIO "Compile with RtAudio Daq backend" ${DEFAULT_RTAUDIO}) option(LASP_HAS_RTAUDIO "Compile with RtAudio Daq backend" ${DEFAULT_RTAUDIO})
option(LASP_HAS_PORTAUDIO "Compile with PortAudio Daq backend" ${DEFAULT_PORTAUDIO}) option(LASP_HAS_PORTAUDIO "Compile with PortAudio Daq backend" ${DEFAULT_PORTAUDIO})
if(LASP_HAS_PORTAUDIO AND LASP_HAS_RTAUDIO) if(LASP_HAS_PORTAUDIO AND LASP_HAS_RTAUDIO)
@ -105,13 +116,17 @@ else()
endif() endif()
# ###################################### Compilation flags # ###################################### Compilation flags
set(CMAKE_C_FLAGS_RELEASE "-O3 -flto -mfpmath=sse -march=x86-64 -mtune=native \ set(CMAKE_C_FLAGS_RELEASE "-O3 -flto -mtune=native \
-fdata-sections -ffunction-sections -fomit-frame-pointer -finline-functions") -fdata-sections -ffunction-sections -fomit-frame-pointer -finline-functions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-type-limits -Werror=return-type") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-type-limits -Werror=return-type")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -flto -mfpmath=sse -march=x86-64 -mtune=native \ set(CMAKE_CXX_FLAGS_RELEASE "-O3 -flto -mtune=native \
-fdata-sections -ffunction-sections -fomit-frame-pointer -finline-functions") -fdata-sections -ffunction-sections -fomit-frame-pointer -finline-functions")
if(NOT ${RPI})
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mfpmath=sse -march=x86-64")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mfpmath=sse -march=x86-64")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wall ") set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wall ")
# ############################# End compilation flags # ############################# End compilation flags

View File

@ -11,7 +11,7 @@ using std::cerr;
using std::endl; using std::endl;
// Safe some typing. Linspace form 0 up to (and NOT including N). // Safe some typing. Linspace form 0 up to (and NOT including N).
#define lin0N arma::linspace(0, N - 1, N) #define lin0N arma::linspace<vd>(0, N - 1, N)
vd Window::hann(const us N) { vd Window::hann(const us N) {
return arma::pow(arma::sin((arma::datum::pi/N) * lin0N), 2); return arma::pow(arma::sin((arma::datum::pi/N) * lin0N), 2);
@ -24,8 +24,8 @@ vd Window::blackman(const us N) {
d a0 = 7938. / 18608.; d a0 = 7938. / 18608.;
d a1 = 9240. / 18608.; d a1 = 9240. / 18608.;
d a2 = 1430. / 18608.; d a2 = 1430. / 18608.;
return a0 - a1 * d_cos((2 * number_pi/N) * lin0N) + return a0 - a1 * arma::cos((2 * number_pi/N) * lin0N) +
a2 * d_cos((4 * number_pi / N)* lin0N ); a2 * arma::cos((4 * number_pi / N)* lin0N );
} }
vd Window::rectangular(const us N) { return arma::ones(N); } vd Window::rectangular(const us N) { return arma::ones(N); }

View File

@ -44,6 +44,12 @@ void init_siggen(py::module &m);
PYBIND11_MODULE(lasp_cpp, m) { PYBIND11_MODULE(lasp_cpp, m) {
#if LASP_DOUBLE_PRECISION == 1
m.attr("LASP_DOUBLE_PRECISION") = true;
#else
m.attr("LASP_DOUBLE_PRECISION") = false;
#endif
init_dsp(m); init_dsp(m);
init_deviceinfo(m); init_deviceinfo(m);
init_daqconfiguration(m); init_daqconfiguration(m);
@ -51,6 +57,5 @@ PYBIND11_MODULE(lasp_cpp, m) {
init_streammgr(m); init_streammgr(m);
init_datahandler(m); init_datahandler(m);
init_siggen(m); init_siggen(m);
} }
/** @} */ /** @} */

View File

@ -12,6 +12,7 @@ __all__ = ['freqResponse', 'bandpass_fir_design', 'lowpass_fir_design',
import numpy as np import numpy as np
from scipy.signal import freqz, hann, firwin2 from scipy.signal import freqz, hann, firwin2
from ..lasp_config import empty
def freqResponse(fs, freq, coefs_b, coefs_a=1.): def freqResponse(fs, freq, coefs_b, coefs_a=1.):
@ -44,7 +45,7 @@ def bandpass_fir_design(L, fs, fl, fu, window=hann):
Omg2 = 2*np.pi*fu/fs Omg2 = 2*np.pi*fu/fs
Omg1 = 2*np.pi*fl/fs Omg1 = 2*np.pi*fl/fs
fir = np.empty(L, dtype=float) fir = empty(L, dtype=float)
# First Create ideal band-pass filter # First Create ideal band-pass filter
fir[L//2] = (Omg2-Omg1)/np.pi fir[L//2] = (Omg2-Omg1)/np.pi
@ -64,7 +65,7 @@ def lowpass_fir_design(L, fs, fc, window=hann):
" than upper cut-off" " than upper cut-off"
Omgc = 2*np.pi*fc/fs Omgc = 2*np.pi*fc/fs
fir = np.empty(L, dtype=float) fir = empty(L, dtype=float)
# First Create ideal band-pass filter # First Create ideal band-pass filter
fir[L//2] = Omgc/np.pi fir[L//2] = Omgc/np.pi

View File

@ -6,8 +6,14 @@ Author: J.A. de Jong - ASCEE
Description: LASP configuration Description: LASP configuration
""" """
import numpy as np import numpy as np
from .lasp_cpp import LASP_DOUBLE_PRECISION
LASP_NUMPY_FLOAT_TYPE = np.float64 if LASP_DOUBLE_PRECISION:
LASP_NUMPY_FLOAT_TYPE = np.float64
LASP_NUMPY_COMPLEX_TYPE = np.float128
else:
LASP_NUMPY_FLOAT_TYPE = np.float32
LASP_NUMPY_COMPLEX_TYPE = np.float64
def zeros(shape): def zeros(shape):

View File

@ -64,6 +64,7 @@ from .lasp_version import LASP_VERSION_MAJOR, LASP_VERSION_MINOR
from .lasp_cpp import Window, DaqChannel, AvPowerSpectra from .lasp_cpp import Window, DaqChannel, AvPowerSpectra
from typing import List from typing import List
from functools import lru_cache from functools import lru_cache
from .lasp_config import ones
# Measurement file extension # Measurement file extension
MEXT = 'h5' MEXT = 'h5'
@ -223,7 +224,7 @@ class IterData(IterRawData):
def __init__(self, fa, channels, sensitivity, **kwargs): def __init__(self, fa, channels, sensitivity, **kwargs):
super().__init__(fa, channels, **kwargs) super().__init__(fa, channels, **kwargs)
self.sens = np.asarray(sensitivity)[self.channels] self.sens = np.asarray(sensitivity, dtype=LASP_NUMPY_FLOAT_TYPE)[self.channels]
assert self.sens.ndim == 1 assert self.sens.ndim == 1
def __next__(self): def __next__(self):
@ -329,10 +330,10 @@ class Measurement:
try: try:
sens = f.attrs["sensitivity"] sens = f.attrs["sensitivity"]
self._sens = ( self._sens = (
sens * np.ones(self.nchannels) if isinstance(sens, float) else sens sens * ones(self.nchannels) if isinstance(sens, float) else sens
) )
except KeyError: except KeyError:
self._sens = np.ones(self.nchannels) self._sens = ones(self.nchannels)
# The time is cached AND ALWAYS ASSUMED TO BE AN IMMUTABLE OBJECT. # The time is cached AND ALWAYS ASSUMED TO BE AN IMMUTABLE OBJECT.
# It is also cached. Changing the measurement timestamp should not # It is also cached. Changing the measurement timestamp should not
@ -885,7 +886,7 @@ class Measurement:
""" """
if isinstance(sens, float): if isinstance(sens, float):
# Put all sensitivities equal # Put all sensitivities equal
sens = sens * np.ones(self.nchannels) sens = sens * ones(self.nchannels)
elif isinstance(sens, list): elif isinstance(sens, list):
sens = np.asarray(sens) sens = np.asarray(sens)
@ -1199,7 +1200,7 @@ class Measurement:
nchannels = 1 nchannels = 1
nframes = len(data) nframes = len(data)
data = data[:, np.newaxis] data = data[:, np.newaxis]
sensitivity = np.ones(nchannels) sensitivity = ones(nchannels)
with h5.File(newfn, "w") as hf: with h5.File(newfn, "w") as hf:
hf.attrs["samplerate"] = samplerate hf.attrs["samplerate"] = samplerate

View File

@ -77,6 +77,7 @@ class FirFilterBank:
self.fs = fs self.fs = fs
self.xs = list(range(xmin, xmax + 1)) self.xs = list(range(xmin, xmax + 1))
raise RuntimeError('Not working code anymore')
maxdecimation = self.designer.firDecimation(self.xs[0]) maxdecimation = self.designer.firDecimation(self.xs[0])
self.decimators = [] self.decimators = []
@ -245,7 +246,7 @@ class SosFilterBank:
for i, x in enumerate(self.xs): for i, x in enumerate(self.xs):
channel = self.designer.createSOSFilter(x) channel = self.designer.createSOSFilter(x)
if sos is None: if sos is None:
sos = np.empty((channel.size, len(self.xs))) sos = empty((channel.size, len(self.xs)))
sos[:, i] = channel.flatten() sos[:, i] = channel.flatten()
self._fb = BiquadBank(sos) self._fb = BiquadBank(sos)

View File

@ -6,6 +6,7 @@ Description:
Reverberation time estimation tool using least squares Reverberation time estimation tool using least squares
""" """
from .lasp_common import getTime from .lasp_common import getTime
from .lasp_config import ones
import numpy as np import numpy as np
@ -56,7 +57,7 @@ class ReverbTime:
x = self._t[istart:istop][:, np.newaxis] x = self._t[istart:istop][:, np.newaxis]
# Solve the least-squares problem, by creating a matrix of # Solve the least-squares problem, by creating a matrix of
A = np.hstack([x, np.ones(x.shape)]) A = np.hstack([x, ones(x.shape)])
# print(A.shape) # print(A.shape)
# print(points.shape) # print(points.shape)

View File

@ -5,6 +5,7 @@ Sound level meter implementation
@author: J.A. de Jong - ASCEE @author: J.A. de Jong - ASCEE
""" """
from .lasp_cpp import cppSLM from .lasp_cpp import cppSLM
from .lasp_config import empty
import numpy as np import numpy as np
from .lasp_common import (TimeWeighting, FreqWeighting, P_REF) from .lasp_common import (TimeWeighting, FreqWeighting, P_REF)
from .filter import SPLFilterDesigner from .filter import SPLFilterDesigner
@ -101,7 +102,7 @@ class SLM:
assert fbdesigner.fs == fs assert fbdesigner.fs == fs
sos_firstx = fbdesigner.createSOSFilter(self.xs[0]).flatten() sos_firstx = fbdesigner.createSOSFilter(self.xs[0]).flatten()
self.nom_txt.append(fbdesigner.nominal_txt(self.xs[0])) self.nom_txt.append(fbdesigner.nominal_txt(self.xs[0]))
sos = np.empty((sos_firstx.size, nfilters), dtype=float, order='C') sos = empty((sos_firstx.size, nfilters), dtype=float, order='C')
sos[:, 0] = sos_firstx sos[:, 0] = sos_firstx
for i, x in enumerate(self.xs[1:]): for i, x in enumerate(self.xs[1:]):

View File

@ -47,7 +47,7 @@ class WeighCal:
P = 2048 # Filter length (number of taps) P = 2048 # Filter length (number of taps)
self._firs = np.empty((P, self.nchannels)) self._firs = empty((P, self.nchannels))
self._fbs = [] self._fbs = []
for chan in range(self.nchannels): for chan in range(self.nchannels):
fir = arbitrary_fir_design(fs, P, freq_design, fir = arbitrary_fir_design(fs, P, freq_design,

View File

@ -21,6 +21,7 @@ import copy
import numpy as np import numpy as np
from numpy import log2, pi, sin from numpy import log2, pi, sin
from ..lasp_cpp import freqSmooth from ..lasp_cpp import freqSmooth
from ..lasp_config import zeros
@unique @unique

View File

@ -31,7 +31,7 @@ def test_backward_fft():
nfft = 2048 nfft = 2048
freq = getFreq(nfft, nfft) freq = getFreq(nfft, nfft)
# Sig = np.zeros(nfft//2+1, dtype=complex) # Sig = zeros(nfft//2+1, dtype=complex)
Sigr = np.random.randn(nfft//2+1) Sigr = np.random.randn(nfft//2+1)
Sigi = np.random.randn(nfft//2+1) Sigi = np.random.randn(nfft//2+1)