lasp/lasp/lasp_common.py

132 lines
3.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
from .wrappers import Window as wWindow
"""
Common definitions used throughout the code.
"""
__all__ = ['P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'calfile',
'getFreq',
'W_REF', 'U_REF', 'I_REF']
# Reference sound pressure level
P_REF = 2e-5
W_REF = 1e-12 # 1 picoWatt
I_REF = 1e-12 # 1 picoWatt/ m^2
# Reference velocity for sound velocity level
U_REF = 5e-8
# Todo: fix This
calfile = None
class Window:
hann = (wWindow.hann, 'Hann')
hamming = (wWindow.hamming, 'Hamming')
rectangular = (wWindow.rectangular, 'Rectangular')
bartlett = (wWindow.bartlett, 'Bartlett')
blackman = (wWindow.blackman, 'Blackman')
types = (hann, hamming, rectangular, bartlett, blackman)
default = 0
@staticmethod
def fillComboBox(cb):
"""
Fill Windows to a combobox
Args:
cb: QComboBox to fill
"""
cb.clear()
for tw in Window.types:
cb.addItem(tw[1], tw)
cb.setCurrentIndex(Window.default)
def getCurrent(cb):
return Window.types[cb.currentIndex()]
class TimeWeighting:
none = (None, 'Raw (no time weighting)')
uufast = (1e-4, '0.1 ms')
ufast = (30e-3, '30 ms')
fast = (0.125, 'Fast (0.125 s)')
slow = (1.0, 'Slow (1.0 s)')
tens = (10, '10 s')
infinite = (np.Inf, 'Infinite')
types = (none, uufast, ufast, fast, slow, tens, infinite)
default = 2
@staticmethod
def fillComboBox(cb):
"""
Fill TimeWeightings to a combobox
Args:
cb: QComboBox to fill
"""
cb.clear()
for tw in TimeWeighting.types:
cb.addItem(tw[1], tw)
cb.setCurrentIndex(TimeWeighting.default)
def getCurrent(cb):
return TimeWeighting.types[cb.currentIndex()]
class FreqWeighting:
"""
Frequency weighting types
"""
A = ('A', 'A-weighting')
C = ('C', 'C-weighting')
Z = ('Z', 'Z-weighting')
types = (A, C, Z)
default = 0
@staticmethod
def fillComboBox(cb):
"""
Fill FreqWeightings to a combobox
Args:
cb: QComboBox to fill
"""
cb.clear()
for fw in FreqWeighting.types:
cb.addItem(fw[1], fw)
cb.setCurrentIndex(FreqWeighting.default)
def getCurrent(cb):
return FreqWeighting.types[cb.currentIndex()]
def getTime(fs, N, start=0):
"""
Return a time array for given number of points and sampling frequency.
Args:
fs: Sampling frequency [Hz]
N: Number of time samples
start: Optional start ofset in number of samples
"""
return np.linspace(start/fs, N/fs, N, endpoint=False)
def getFreq(fs, nfft):
"""
return an array of frequencies for single-sided spectra
Args:
fs: Sampling frequency [Hz]
nfft: Fft length (int)
"""
df = fs/nfft # frequency resolution
K = nfft//2+1 # number of frequency bins
return np.linspace(0, (K-1)*df, K)