<s> import numpy as np <EOL> from acoustics import Signal <EOL> import pytest <EOL> import tempfile <EOL> import itertools as it <EOL> from acoustics . signal import EqualBand <EOL> class test_wav ( ) : <EOL> """<STR_LIT>""" <EOL> duration = <NUM_LIT> <EOL> fs = <NUM_LIT> <EOL> samples = int ( fs * duration ) <EOL> channels = <NUM_LIT:3> <EOL> signal = Signal ( np . random . randn ( channels , samples ) , fs ) <EOL> with tempfile . TemporaryFile ( ) as file : <EOL> signal . to_wav ( file ) <EOL> signal = Signal . from_wav ( file ) <EOL> assert signal . samples == samples <EOL> assert signal . fs == fs <EOL> assert signal . channels == channels <EOL> class TestSignal ( ) : <EOL> @ pytest . fixture ( params = [ ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> def signal ( self , request ) : <EOL> return Signal ( np . random . randn ( request . param [ <NUM_LIT:0> ] , request . param [ <NUM_LIT:1> ] ) , request . param [ <NUM_LIT:2> ] ) <EOL> def test_samples ( self , signal ) : <EOL> x = signal . samples <EOL> def test_calibrate_to_scalar ( self , signal ) : <EOL> signal . calibrate_to ( <NUM_LIT> ) <EOL> signal . copy ( ) . calibrate_to ( <NUM_LIT> , inplace = True ) <EOL> def test_calibrate_to_channels ( self , signal ) : <EOL> signal . calibrate_to ( ( np . ones ( signal . channels ) * <NUM_LIT> ) [ ... , None ] ) <EOL> signal . copy ( ) . calibrate_to ( ( np . ones ( signal . channels ) * <NUM_LIT> ) [ ... , None ] , inplace = True ) <EOL> def test_calibrate_to_samples ( self , signal ) : <EOL> signal . calibrate_to ( np . ones ( signal . samples ) ) <EOL> signal . copy ( ) . calibrate_to ( np . ones ( signal . samples ) , inplace = True ) <EOL> def test_calibrate_to_samples_channels ( self , signal ) : <EOL> signal . calibrate_to ( np . ones ( signal . shape ) ) <EOL> signal . copy ( ) . calibrate_to ( np . ones ( signal . shape ) , inplace = True ) <EOL> def test_calibrate_with ( self , signal ) : <EOL> calibration_signal_level = <NUM_LIT> <EOL> decibel = <NUM_LIT> <EOL> calibration_signal = Signal ( np . random . randn ( signal . samples ) , signal . fs ) . calibrate_to ( calibration_signal_level ) <EOL> out = signal . calibrate_with ( calibration_signal , decibel ) <EOL> assert ( ( out . leq ( ) - signal . leq ( ) ) . mean ( ) - ( decibel - calibration_signal_level ) ) < <NUM_LIT> <EOL> def test_channels ( self , signal ) : <EOL> x = signal . channels <EOL> def test_duration ( self , signal ) : <EOL> x = signal . duration <EOL> def test_decimate ( self , signal ) : <EOL> factor = <NUM_LIT:4> <EOL> decimated = signal . decimate ( factor ) <EOL> assert ( signal . fs / factor == decimated . fs ) <EOL> def test_upsample ( self , signal ) : <EOL> factor = <NUM_LIT:2> <EOL> assert ( signal . upsample ( factor ) . fs / signal . fs ) == factor <EOL> def test_gain_scalar ( self , signal ) : <EOL> gain = + <NUM_LIT> <EOL> assert ( np . abs ( signal . gain ( gain ) . leq ( ) - ( signal . leq ( ) + gain ) ) < <NUM_LIT> ) . all ( ) <EOL> assert ( np . abs ( signal . copy ( ) . gain ( gain , inplace = True ) . leq ( ) - ( signal . leq ( ) + gain ) ) < <NUM_LIT> ) . all ( ) <EOL> def test_pick ( self , signal ) : <EOL> x = signal . pick ( signal . duration * <NUM_LIT:0.1> , signal . duration * <NUM_LIT> ) <EOL> def test_times ( self , signal ) : <EOL> times = signal . times ( ) <EOL> def test_energy ( self , signal ) : <EOL> energy = signal . energy ( ) <EOL> def test_power ( self , signal ) : <EOL> power = signal . power ( ) <EOL> def test_ms ( self , signal ) : <EOL> ms = signal . ms ( ) <EOL> def test_rms ( self , signal ) : <EOL> rms = signal . rms ( ) <EOL> def test_correlate ( self , signal ) : <EOL> signal = signal [ ... , <NUM_LIT:0> : <NUM_LIT:100> ] <EOL> if signal . channels > <NUM_LIT:1> : <EOL> with pytest . raises ( ValueError ) : <EOL> assert ( ( signal . correlate ( ) == signal . correlate ( signal ) ) . all ( ) ) <EOL> else : <EOL> assert ( ( signal . correlate ( ) == signal . correlate ( signal ) ) . all ( ) ) <EOL> def test_amplitude_envelope ( self , signal ) : <EOL> x = signal . amplitude_envelope ( ) <EOL> def test_instantaneous_frequency ( self , signal ) : <EOL> x = signal . instantaneous_frequency ( ) <EOL> def test_instantaneous_phase ( self , signal ) : <EOL> x = signal . instantaneous_phase ( ) <EOL> def test_detrend ( self , signal ) : <EOL> x = signal . detrend ( ) <EOL> def test_unwrap ( self , signal ) : <EOL> x = signal . unwrap ( ) <EOL> def test_complex_cepstrum ( self , signal ) : <EOL> t , c , d = signal . complex_cepstrum ( ) <EOL> def test_real_cepstrum ( self , signal ) : <EOL> t , c = signal . real_cepstrum ( ) <EOL> def test_power_spectrum ( self , signal ) : <EOL> freq , power = signal . power_spectrum ( ) <EOL> def test_phase_spectrum ( self , signal ) : <EOL> freq , phase = signal . phase_spectrum ( ) <EOL> def test_peak ( self , signal ) : <EOL> value = signal . peak ( ) <EOL> assert len ( value ) == signal . channels <EOL> def test_peak_level ( self , signal ) : <EOL> value = signal . peak_level ( ) <EOL> assert len ( value ) == signal . channels <EOL> def test_sound_exposure ( self , signal ) : <EOL> value = signal . sound_exposure ( ) <EOL> assert len ( value ) == signal . channels <EOL> def test_sound_exposure_level ( self , signal ) : <EOL> value = signal . sound_exposure_level ( ) <EOL> assert len ( value ) == signal . channels <EOL> def test_octaves ( self , signal ) : <EOL> freq , octaves = signal . octaves ( ) <EOL> def test_levels ( self , signal ) : <EOL> times , levels = signal . levels ( ) <EOL> def test_leq ( self , signal ) : <EOL> leq = signal . leq ( ) <EOL> assert ( type ( leq ) is np . ndarray ) <EOL> def test_bandpass ( self , signal ) : <EOL> x = signal . bandpass ( <NUM_LIT> , <NUM_LIT> ) <EOL> def test_bandstop ( self , signal ) : <EOL> x = signal . bandstop ( <NUM_LIT> , <NUM_LIT> ) <EOL> def test_highpass ( self , signal ) : <EOL> x = signal . highpass ( <NUM_LIT> ) <EOL> def test_lowpass ( self , signal ) : <EOL> x = signal . lowpass ( <NUM_LIT> ) <EOL> def test_octavepass ( self , signal ) : <EOL> x = signal . octavepass ( <NUM_LIT> , fraction = <NUM_LIT:6> ) <EOL> def test_bandpass_frequencies ( self , signal ) : <EOL> f = EqualBand ( center = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , bandwidth = <NUM_LIT> ) <EOL> f , x = signal . bandpass_frequencies ( f ) <EOL> def test_bandpass_octaves ( self , signal ) : <EOL> f , x = signal . octaves ( ) <EOL> def test_bandpass_third_octaves ( self , signal ) : <EOL> f , x = signal . third_octaves ( ) <EOL> def test_bandpass_fractional_octaves ( self , signal ) : <EOL> f , x = signal . fractional_octaves ( ) <EOL> def test_weigh ( self , signal ) : <EOL> s = signal . weigh ( ) <EOL> s = signal . weigh ( '<STR_LIT:C>' ) <EOL> s = signal . weigh ( '<STR_LIT:A>' , zero_phase = True ) <EOL> def test_plot ( self , signal ) : <EOL> signal . plot ( ) <EOL> def test_plot_levels ( self , signal ) : <EOL> signal . plot_levels ( ) <EOL> signal . plot_levels ( method = '<STR_LIT>' , time = <NUM_LIT:1.0> ) <EOL> signal . plot_levels ( method = '<STR_LIT>' , time = <NUM_LIT:1.0> ) <EOL> def test_plot_octaves ( self , signal ) : <EOL> signal . plot_octaves ( ) <EOL> def test_plot_third_octaves ( self , signal ) : <EOL> signal . plot_third_octaves ( ) <EOL> def test_plot_fractional_octaves ( self , signal ) : <EOL> signal . plot_fractional_octaves ( <NUM_LIT:3> ) <EOL> signal . plot_fractional_octaves ( <NUM_LIT:6> ) <EOL> signal . plot_fractional_octaves ( <NUM_LIT:9> ) <EOL> def test_plot_power_spectrum ( self , signal ) : <EOL> signal . plot_power_spectrum ( ) <EOL> def test_plot_phase_spectrum ( self , signal ) : <EOL> signal . plot_phase_spectrum ( ) <EOL> def test_spectrogram ( self , signal ) : <EOL> if signal . channels > <NUM_LIT:1> : <EOL> with pytest . raises ( ValueError ) : <EOL> signal . spectrogram ( ) <EOL> else : <EOL> try : <EOL> signal . spectrogram ( ) <EOL> except NotImplementedError : <EOL> pass <EOL> def test_pickling ( self , signal ) : <EOL> import pickle <EOL> p = pickle . dumps ( signal ) <EOL> obj = pickle . loads ( p ) <EOL> assert ( ( obj == signal ) . all ( ) ) <EOL> assert ( obj . fs == signal . fs ) <EOL> assert ( type ( obj ) is type ( signal ) ) </s>
