Better implementation of 'Measurement.fromWaveFile, using scipy, which is able to handle wave files with floating point data

This commit is contained in:
Anne de Jong 2020-07-23 16:12:54 +02:00
parent 3e2e8c8953
commit b17178c4a7
4 changed files with 22 additions and 29 deletions

View File

@ -88,7 +88,6 @@ class DAQConfiguration:
en_input = self.input_channel_configs en_input = self.input_channel_configs
first_ch_enabled_found = False first_ch_enabled_found = False
ch_disabled_found_after = False ch_disabled_found_after = False
print(en_input)
for ch in en_input: for ch in en_input:
if ch.channel_enabled: if ch.channel_enabled:
first_ch_enabled_found = True first_ch_enabled_found = True

View File

@ -11,6 +11,7 @@ import numpy as np
__all__ = ['PinkNoise'] #, 'BrownianNoise', 'BlueNoise'] __all__ = ['PinkNoise'] #, 'BrownianNoise', 'BlueNoise']
def PinkNoise(fs, fstart=10, fend=None, N=3): def PinkNoise(fs, fstart=10, fend=None, N=3):
""" """
Creates SOS filter for pink noise. The filter has a flat response below Creates SOS filter for pink noise. The filter has a flat response below

View File

@ -17,7 +17,6 @@ Common definitions used throughout the code.
__all__ = [ __all__ = [
'P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'getFreq', 'P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'getFreq',
'lasp_shelve', 'this_lasp_shelve', 'W_REF', 'U_REF', 'I_REF', 'dBFS_REF', 'lasp_shelve', 'this_lasp_shelve', 'W_REF', 'U_REF', 'I_REF', 'dBFS_REF',
'SIUnits'
] ]
# Reference sound pressure level # Reference sound pressure level

View File

@ -39,7 +39,7 @@ from contextlib import contextmanager
import h5py as h5 import h5py as h5
import numpy as np import numpy as np
from .lasp_config import LASP_NUMPY_FLOAT_TYPE from .lasp_config import LASP_NUMPY_FLOAT_TYPE
import wave from scipy.io import wavfile
import os import os
import time import time
@ -537,31 +537,25 @@ class Measurement:
if os.path.exists(newfn) and not force: 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') raise RuntimeError(f'Measurement file name {newfn} already exists in path, set "force" to true to overwrite')
with wave.open(fn, 'r') as wf: samplerate, data = wavfile.read(fn)
nchannels = wf.getnchannels() if data.ndim == 2:
samplerate = wf.getframerate() nframes, nchannels = data.shape
sensitivity = np.ones(nchannels) else:
sampwidth = wf.getsampwidth() nchannels = 1
nframes = wf.getnframes() nframes = len(data)
if sampwidth == 2: data = data[:, np.newaxis]
dtype = np.int16 sensitivity = np.ones(nchannels)
elif sampwidth == 1:
dtype = np.int8
elif sampwidth == 4:
dtype = np.int32
with h5.File(newfn, 'w') as hf: with h5.File(newfn, 'w') as hf:
hf.attrs['samplerate'] = samplerate hf.attrs['samplerate'] = samplerate
hf.attrs['nchannels'] = nchannels hf.attrs['nchannels'] = nchannels
hf.attrs['time'] = timestamp hf.attrs['time'] = timestamp
hf.attrs['blocksize'] = 1 hf.attrs['blocksize'] = 1
hf.attrs['sensitivity'] = sensitivity hf.attrs['sensitivity'] = sensitivity
data = np.frombuffer( ad = hf.create_dataset('audio', (1, nframes, nchannels),
wf.readframes(nframes), dtype=dtype).reshape(nframes, nchannels) dtype=data.dtype,
ad = hf.create_dataset('audio', (1, data.shape[0], data.shape[1]), maxshape=(1, nframes, nchannels),
dtype=dtype, compression='gzip')
maxshape=(1, data.shape[0], data.shape[1]), ad[0] = data
compression='gzip')
ad[0] = data
return Measurement(newfn) return Measurement(newfn)