def tone_cal(freq, duration=.25, rec_delay=0.1, mic_sens=0.0015, atten=10, fft=False, **kw): if not rec_delay > 0: raise ValueError("Delay of recording must be greater than zero") # We need to initialize device so we can obtain certain parameters from it # (specifically the sampling frequency). device = DAQ.init_device(**DAQ_SETTINGS) samples = device.convert('s', 'nPow2', duration) rec_delay_samples = device.convert('s', 'n', rec_delay) play_samples = samples + rec_delay_samples * 2 play_dur = device.convert('s', 'n', play_samples) tone = Tone(frequency=freq, fs=device.fs, duration=play_dur, amplitude=1) device.configure(play_duration=play_samples, rec_duration=samples, rec_delay=rec_delay_samples, signal=tone.signal) # Prepare equipment for recording equipment.dsp().PA5.SetAtten(atten) result = tone_power(device, samples, freq=freq, fft=fft, **kw) spl = patodb(result[0] / mic_sens) + atten if len(result) == 3: phase, fft_data = result[-2:] fft_data['power'] = patodb(fft_data['power'] / mic_sens) return spl, phase, fft_data else: return spl, result[1]
def fft_analyze(signals, fs, mic_sens=None, filter=None): if filter is not None: signals = [filtfilt(x=s, **filter) for s in signals] transforms = [rfft(fs, s) for s in signals] freq, power, phase = zip(*transforms) signal = np.array(signals).mean(0) if mic_sens is not None: power = patodb(np.array(power).mean(0) / mic_sens) else: power = np.array(power).mean(0) result = { 't': np.arange(len(signal)) / fs, 'signal': signal, 'signals': signals, 'frequencies': freq[0], 'power': power, 'phase': np.array(phase).mean(0), } return result
def spl_analyze(signal, window, fs, sens=0.0015): power = inst_power(signal, window) if sens is not None: power = patodb(power / sens) t = np.arange(len(power)) / fs return t, power