From b17178c4a775f3e72734b6f66433943c57fa4164 Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Thu, 23 Jul 2020 16:12:54 +0200 Subject: [PATCH] Better implementation of 'Measurement.fromWaveFile, using scipy, which is able to handle wave files with floating point data --- lasp/device/lasp_daqconfig.py | 1 - lasp/filter/colorednoise.py | 1 + lasp/lasp_common.py | 1 - lasp/lasp_measurement.py | 48 +++++++++++++++-------------------- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/lasp/device/lasp_daqconfig.py b/lasp/device/lasp_daqconfig.py index 216c596..0a908d6 100644 --- a/lasp/device/lasp_daqconfig.py +++ b/lasp/device/lasp_daqconfig.py @@ -88,7 +88,6 @@ class DAQConfiguration: en_input = self.input_channel_configs first_ch_enabled_found = False ch_disabled_found_after = False - print(en_input) for ch in en_input: if ch.channel_enabled: first_ch_enabled_found = True diff --git a/lasp/filter/colorednoise.py b/lasp/filter/colorednoise.py index 57ddc48..69b76ad 100644 --- a/lasp/filter/colorednoise.py +++ b/lasp/filter/colorednoise.py @@ -11,6 +11,7 @@ import numpy as np __all__ = ['PinkNoise'] #, 'BrownianNoise', 'BlueNoise'] + def PinkNoise(fs, fstart=10, fend=None, N=3): """ Creates SOS filter for pink noise. The filter has a flat response below diff --git a/lasp/lasp_common.py b/lasp/lasp_common.py index 1933a55..3406138 100644 --- a/lasp/lasp_common.py +++ b/lasp/lasp_common.py @@ -17,7 +17,6 @@ Common definitions used throughout the code. __all__ = [ 'P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'getFreq', 'lasp_shelve', 'this_lasp_shelve', 'W_REF', 'U_REF', 'I_REF', 'dBFS_REF', - 'SIUnits' ] # Reference sound pressure level diff --git a/lasp/lasp_measurement.py b/lasp/lasp_measurement.py index 1b5d532..39efbce 100644 --- a/lasp/lasp_measurement.py +++ b/lasp/lasp_measurement.py @@ -39,7 +39,7 @@ from contextlib import contextmanager import h5py as h5 import numpy as np from .lasp_config import LASP_NUMPY_FLOAT_TYPE -import wave +from scipy.io import wavfile import os import time @@ -537,31 +537,25 @@ class Measurement: if os.path.exists(newfn) and not force: raise RuntimeError(f'Measurement file name {newfn} already exists in path, set "force" to true to overwrite') - with wave.open(fn, 'r') as wf: - nchannels = wf.getnchannels() - samplerate = wf.getframerate() - sensitivity = np.ones(nchannels) - sampwidth = wf.getsampwidth() - nframes = wf.getnframes() - if sampwidth == 2: - dtype = np.int16 - elif sampwidth == 1: - dtype = np.int8 - elif sampwidth == 4: - dtype = np.int32 + samplerate, data = wavfile.read(fn) + if data.ndim == 2: + nframes, nchannels = data.shape + else: + nchannels = 1 + nframes = len(data) + data = data[:, np.newaxis] + sensitivity = np.ones(nchannels) - with h5.File(newfn, 'w') as hf: - hf.attrs['samplerate'] = samplerate - hf.attrs['nchannels'] = nchannels - hf.attrs['time'] = timestamp - hf.attrs['blocksize'] = 1 - hf.attrs['sensitivity'] = sensitivity - data = np.frombuffer( - wf.readframes(nframes), dtype=dtype).reshape(nframes, nchannels) - ad = hf.create_dataset('audio', (1, data.shape[0], data.shape[1]), - dtype=dtype, - maxshape=(1, data.shape[0], data.shape[1]), - compression='gzip') - ad[0] = data + with h5.File(newfn, 'w') as hf: + hf.attrs['samplerate'] = samplerate + hf.attrs['nchannels'] = nchannels + hf.attrs['time'] = timestamp + hf.attrs['blocksize'] = 1 + hf.attrs['sensitivity'] = sensitivity + ad = hf.create_dataset('audio', (1, nframes, nchannels), + dtype=data.dtype, + maxshape=(1, nframes, nchannels), + compression='gzip') + ad[0] = data - return Measurement(newfn) + return Measurement(newfn)