From 28675d16a246ac44d1914cb54ca1ac994a7f9d13 Mon Sep 17 00:00:00 2001 From: Matej Simurda Date: Thu, 23 May 2019 11:14:36 +0200 Subject: [PATCH 1/4] Implemented weighting from NOAA guidelines --- acoustics/weighting.py | 246 ++++++++++++++++++++++++++++++++++++++-- tests/test_weighting.py | 87 +++++++++++--- 2 files changed, 308 insertions(+), 25 deletions(-) diff --git a/acoustics/weighting.py b/acoustics/weighting.py index 25f58804..0585f405 100644 --- a/acoustics/weighting.py +++ b/acoustics/weighting.py @@ -2,7 +2,9 @@ Weighting ========= -Weightings according to IEC 61672-1:2003. +Weightings according to IEC 61672-1:2003 and NOAA weightings as defined +in 'Technical Guidance for Assessing the Effects of Anthropogenic Sound on Marine Mammal Hearing' +(https://www.fisheries.noaa.gov/resource/document/technical-guidance-assessing-effects-anthropogenic-sound-marine-mammal) """ @@ -12,18 +14,70 @@ THIRD_OCTAVE_A_WEIGHTING = np.array([ -63.4, -56.7, -50.5, -44.7, -39.4, -34.6, -30.2, -26.2, -22.5, -19.1, -16.1, -13.4, -10.9, -8.6, -6.6, -4.8, -3.2, - -1.9, -0.8, +0.0, +0.6, +1.0, +1.2, +1.3, +1.2, +1.0, +0.5, -0.1, -1.1, -2.5, -4.3, -6.6, -9.3 + -1.9, -0.8, +0.0, +0.6, +1.0, +1.2, +1.3, +1.2, +1.0, +0.5, -0.1, -1.1, -2.5, -4.3, -6.6, -9.3, -12.3, -15.7, -19.4, + -23.0, -26.8, -30.9, -34.7, -38.5, -42.8, -46.6 ]) """ -A-weighting filter for preferred 1/3-octave band center frequencies, as specified in :attr:`acoustics.bands.THIRD_OCTAVE_CENTER_FREQUENCIES`. +A-weighting filter for preferred 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ THIRD_OCTAVE_C_WEIGHTING = np.array([ -11.2, -8.5, -6.2, -4.4, -3.0, -2.0, -1.3, -0.8, -0.5, -0.3, -0.2, -0.1, +0.0, +0.0, +0.0, +0.0, +0.0, +0.0, +0.0, - +0.0, +0.0, -0.1, -0.2, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.4, -6.2, -8.5, -11.2 + +0.0, +0.0, -0.1, -0.2, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.4, -6.2, -8.5, -11.2, -14.3, -17.6, -21.3, -24.9, + -28.8, -32.8, -36.6, -40.4, -44.7, -48.6 ]) """ -C-weighting filter for preferred 1/3-octave band center frequencies, as specified in :attr:`acoustics.bands.THIRD_OCTAVE_CENTER_FREQUENCIES`. +C-weighting filter for preferred 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_LF_WEIGHTING = np.array([ + -24.0, -21.8, -19.9, -18.0, -16.0, -14.0, -12.2, -10.3, -8.5, -6.9, -5.4, -4.0, -2.9, -2.0, -1.3, -0.8, -0.5, -0.3, + -0.1, -0.1, -0.0, 0.0, -0.0, -0.0, -0.1, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.5, -6.3, -8.6, -11.3, -14.6, + -17.9, -21.4, -25.3, -29.0, -32.8, -37.0, -40.8 +]) + +""" +NOAA LF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_MF_WEIGHTING = np.array([ + -89.9, -86.5, -83.4, -80.3, -77.1, -73.8, -70.7, -67.4, -64.1, -61.0, -57.9, -54.5, -51.4, -48.3, -45.1, -41.8, + -38.7, -35.5, -32.2, -29.1, -26.1, -22.7, -19.7, -16.8, -13.9, -11.1, -8.6, -6.3, -4.4, -2.9, -1.7, -0.8, -0.3, + -0.0, -0.0, -0.2, -0.6, -1.4, -2.6, -4.1, -6.0, -8.7, -11.5 +]) + +""" +NOAA MF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_HF_WEIGHTING = np.array([ + -106.0, -102.1, -98.7, -95.2, -91.6, -87.8, -84.3, -80.7, -77.0, -73.5, -70.0, -66.1, -62.7, -59.2, -55.6, -51.8, + -48.3, -44.7, -41.0, -37.5, -34.1, -30.3, -26.9, -23.5, -20.1, -16.6, -13.6, -10.6, -7.9, -5.7, -3.8, -2.2, -1.2, + -0.5, -0.1, 0.0, -0.1, -0.5, -1.3, -2.3, -3.8, -5.9, -8.3 +]) + +""" +NOAA HF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_PW_WEIGHTING = np.array([ + -42.9, -40.7, -38.8, -36.9, -34.9, -32.8, -30.8, -28.8, -26.8, -24.8, -22.9, -20.8, -18.9, -16.9, -15.0, -13.0, + -11.1, -9.3, -7.5, -5.9, -4.5, -3.1, -2.1, -1.3, -0.7, -0.3, -0.1, -0.0, -0.1, -0.3, -0.7, -1.5, -2.5, -3.9, -5.7, + -8.1, -10.8, -13.9, -17.4, -20.9, -24.5, -28.6, -32.4 +]) + +""" +NOAA PW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_OW_WEIGHTING = np.array([ + -74.4, -70.1, -66.2, -62.4, -58.4, -54.2, -50.4, -46.4, -42.2, -38.4, -34.6, -30.4, -26.6, -23.0, -19.3, -15.7, + -12.5, -9.5, -6.9, -4.9, -3.3, -2.0, -1.1, -0.6, -0.2, -0.0, -0.0, -0.1, -0.3, -0.7, -1.3, -2.4, -3.7, -5.4, -7.6, + -10.4, -13.3, -16.7, -20.4, -24.0, -27.7, -31.8, -35.6 +]) + +""" +NOAA OW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ @@ -32,7 +86,7 @@ def a_weighting(first, last): Select frequency weightings between ``first`` and ``last`` centerfrequencies from A-weighting. Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 20,000 Hz (including them). + between 12.5 Hz and 200,000 Hz (including them). Parameters ---------- @@ -55,7 +109,7 @@ def c_weighting(first, last): Select frequency weightings between ``first`` and ``last`` centerfrequencies from C-weighting. Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 20,000 Hz (including them). + between 12.5 Hz and 200,000 Hz (including them). Parameters ---------- @@ -73,8 +127,124 @@ def c_weighting(first, last): return _weighting("c", first, last) +def noaa_lf_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA LF-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA LF-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_lf", first, last) + + +def noaa_mf_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA MF-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA MF-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_mf", first, last) + + +def noaa_hf_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA HF-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA HF-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_hf", first, last) + + +def noaa_pw_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA PW-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA PW-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_pw", first, last) + + + +def noaa_ow_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA OW-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA OW-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_ow", first, last) + + def _weighting(filter_type, first, last): - third_oct_bands = third(12.5, 20000.0).tolist() + third_oct_bands = third(12.5, 200000.0).tolist() low = third_oct_bands.index(first) high = third_oct_bands.index(last) @@ -84,6 +254,21 @@ def _weighting(filter_type, first, last): elif filter_type == "c": freq_weightings = THIRD_OCTAVE_C_WEIGHTING + elif filter_type == "noaa_lf": + freq_weightings = THIRD_OCTAVE_NOAA_LF_WEIGHTING + + elif filter_type == "noaa_mf": + freq_weightings = THIRD_OCTAVE_NOAA_MF_WEIGHTING + + elif filter_type == "noaa_hf": + freq_weightings = THIRD_OCTAVE_NOAA_HF_WEIGHTING + + elif filter_type == "noaa_pw": + freq_weightings = THIRD_OCTAVE_NOAA_PW_WEIGHTING + + elif filter_type == "noaa_ow": + freq_weightings = THIRD_OCTAVE_NOAA_OW_WEIGHTING + return freq_weightings[low:high + 1] @@ -125,15 +310,60 @@ def c2a(levels, first, last): return z2a(dB, first, last) +def noaa_lf(levels, first, last): + """Apply NOAA LF-weighting to unweighted signal. + """ + return levels + noaa_lf_weighting(first, last) + + +def noaa_mf(levels, first, last): + """Apply NOAA MF-weighting to unweighted signal. + """ + return levels + noaa_mf_weighting(first, last) + + +def noaa_hf(levels, first, last): + """Apply NOAA HF-weighting to unweighted signal. + """ + return levels + noaa_hf_weighting(first, last) + + +def noaa_pw(levels, first, last): + """Apply NOAA PW-weighting to unweighted signal. + """ + return levels + noaa_pw_weighting(first, last) + + +def noaa_ow(levels, first, last): + """Apply NOAA OW-weighting to unweighted signal. + """ + return levels + noaa_ow_weighting(first, last) + + __all__ = [ 'THIRD_OCTAVE_A_WEIGHTING', 'THIRD_OCTAVE_C_WEIGHTING', + 'THIRD_OCTAVE_NOAA_LF_WEIGHTING', + 'THIRD_OCTAVE_NOAA_MF_WEIGHTING', + 'THIRD_OCTAVE_NOAA_HF_WEIGHTING', + 'THIRD_OCTAVE_NOAA_PW_WEIGHTING', + 'THIRD_OCTAVE_NOAA_OW_WEIGHTING', 'a_weighting', 'c_weighting', + 'noaa_lf_weighting', + 'noaa_mf_weighting', + 'noaa_hf_weighting', + 'noaa_pw_weighting', + 'noaa_ow_weighting', 'z2a', 'a2z', 'z2c', 'c2z', 'a2c', 'c2a', + 'noaa_lf', + 'noaa_mf', + 'noaa_hf', + 'noaa_pw', + 'noaa_ow', ] diff --git a/tests/test_weighting.py b/tests/test_weighting.py index 998ff74a..64ce6e2d 100644 --- a/tests/test_weighting.py +++ b/tests/test_weighting.py @@ -2,21 +2,22 @@ import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal -from acoustics.weighting import z2a, a2z, z2c, c2z, a2c, c2a +from acoustics.weighting import z2a, a2z, z2c, c2z, a2c, c2a, noaa_lf, noaa_mf, noaa_hf, noaa_pw, noaa_ow def setup_module(weighting): - weighting.levels = 100 * np.ones(33) + weighting.levels = 100 * np.ones(43) weighting.lowest = 12.5 - weighting.highest = 20000 + weighting.highest = 200000 def test_z2a(): generated = z2a(levels, lowest, highest) real = np.array([ 36.6, 43.3, 49.5, 55.3, 60.6, 65.4, 69.8, 73.8, 77.5, 80.9, 83.9, 86.6, 89.1, 91.4, 93.4, 95.2, 96.8, 98.1, - 99.2, 100, 100.6, 101, 101.2, 101.3, 101.2, 101, 100.5, 99.9, 98.9, 97.5, 95.7, 93.4, 90.7 - ]) + 99.2, 100., 100.6, 101., 101.2, 101.3, 101.2, 101., 100.5, 99.9, 98.9, 97.5, 95.7, 93.4, 90.7, 87.7, 84.3, + 80.6, 77., 73.2, 69.1, 65.3, 61.5, 57.2, 53.4 + ]) assert_array_equal(real, generated) @@ -24,16 +25,17 @@ def test_a2z(): generated = a2z(levels, lowest, highest) real = np.array([ 163.4, 156.7, 150.5, 144.7, 139.4, 134.6, 130.2, 126.2, 122.5, 119.1, 116.1, 113.4, 110.9, 108.6, 106.6, 104.8, - 103.2, 101.9, 100.8, 100, 99.4, 99, 98.8, 98.7, 98.8, 99, 99.5, 100.1, 101.1, 102.5, 104.3, 106.6, 109.3 - ]) + 103.2, 101.9, 100.8, 100., 99.4, 99., 98.8, 98.7, 98.8, 99., 99.5, 100.1, 101.1, 102.5, 104.3, 106.6, 109.3, + 112.3, 115.7, 119.4, 123., 126.8, 130.9, 134.7, 138.5, 142.8, 146.6]) assert_array_equal(real, generated) def test_z2c(): generated = z2c(levels, lowest, highest) real = np.array([ - 88.8, 91.5, 93.8, 95.6, 97, 98, 98.7, 99.2, 99.5, 99.7, 99.8, 99.9, 100, 100, 100, 100, 100, 100, 100, 100, 100, - 99.9, 99.8, 99.7, 99.5, 99.2, 98.7, 98, 97, 95.6, 93.8, 91.5, 88.8 + 88.8, 91.5, 93.8, 95.6, 97., 98., 98.7, 99.2, 99.5, 99.7, 99.8, 99.9, 100., 100., 100., 100., 100., 100., 100., + 100., 100., 99.9, 99.8, 99.7, 99.5, 99.2, 98.7, 98., 97., 95.6, 93.8, 91.5, 88.8, 85.7, 82.4, 78.7, 75.1, 71.2, + 67.2, 63.4, 59.6, 55.3, 51.4 ]) assert_array_equal(real, generated) @@ -41,26 +43,77 @@ def test_z2c(): def test_c2z(): generated = c2z(levels, lowest, highest) real = np.array([ - 111.2, 108.5, 106.2, 104.4, 103, 102, 101.3, 100.8, 100.5, 100.3, 100.2, 100.1, 100, 100, 100, 100, 100, 100, - 100, 100, 100, 100.1, 100.2, 100.3, 100.5, 100.8, 101.3, 102, 103, 104.4, 106.2, 108.5, 111.2 - ]) + 111.2, 108.5, 106.2, 104.4, 103., 102., 101.3, 100.8, 100.5,100.3, 100.2, 100.1, 100., 100., 100., 100., 100., + 100., 100., 100., 100., 100.1, 100.2, 100.3, 100.5, 100.8, 101.3,102., 103., 104.4, 106.2, 108.5, 111.2, 114.3, + 117.6, 121.3, 124.9, 128.8, 132.8, 136.6, 140.4, 144.7, 148.6]) assert_array_equal(real, generated) def test_a2c(): generated = a2c(levels, lowest, highest) real = np.array([ - 152.2, 148.2, 144.3, 140.3, 136.4, 132.6, 128.9, 125.4, 122, 118.8, 115.9, 113.3, 110.9, 108.6, 106.6, 104.8, - 103.2, 101.9, 100.8, 100, 99.4, 98.9, 98.6, 98.4, 98.3, 98.2, 98.2, 98.1, 98.1, 98.1, 98.1, 98.1, 98.1 - ]) + 152.2, 148.2, 144.3, 140.3, 136.4, 132.6, 128.9, 125.4, 122., 118.8, 115.9, 113.3, 110.9, 108.6, 106.6, 104.8, + 103.2, 101.9, 100.8, 100., 99.4, 98.9, 98.6, 98.4, 98.3, 98.2, 98.2, 98.1, 98.1, 98.1, 98.1, 98.1, 98.1, 98., + 98.1, 98.1, 98.1, 98., 98.1, 98.1, 98.1, 98.1, 98.]) assert_array_almost_equal(real, generated) def test_c2a(): generated = c2a(levels, lowest, highest) real = np.array([ - 47.8, 51.8, 55.7, 59.7, 63.6, 67.4, 71.1, 74.6, 78, 81.2, 84.1, 86.7, 89.1, 91.4, 93.4, 95.2, 96.8, 98.1, 99.2, - 100, 100.6, 101.1, 101.4, 101.6, 101.7, 101.8, 101.8, 101.9, 101.9, 101.9, 101.9, 101.9, 101.9 + 47.8, 51.8, 55.7, 59.7, 63.6, 67.4, 71.1, 74.6, 78., 81.2, 84.1, 86.7, 89.1, 91.4, 93.4, 95.2, 96.8, 98.1, + 99.2, 100., 100.6, 101.1, 101.4, 101.6, 101.7, 101.8, 101.8, 101.9, 101.9, 101.9, 101.9, 101.9, 101.9, 102., + 101.9, 101.9, 101.9, 102., 101.9, 101.9, 101.9, 101.9, 102. + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_lf(): + generated = noaa_lf(levels, lowest, highest) + real = np.array([ + 76.0, 78.2, 80.1, 82.0, 84.0, 86.0, 87.8, 89.7, 91.5, 93.1, 94.6, 96.0, 97.1, 98.0, 98.7, 99.2, 99.5, 99.7, + 99.9, 99.9, 100.0, 100.0, 100.0, 100.0, 99.9, 99.7, 99.5, 99.2, 98.7, 98.0, 97.0, 95.5, 93.7, 91.4, 88.7, 85.4, + 82.1, 78.6, 74.7, 71.0, 67.2, 63.0, 59.2 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_mf(): + generated = noaa_mf(levels, lowest, highest) + real = np.array([ + 10.1, 13.5, 16.6, 19.7, 22.9, 26.2, 29.3, 32.6, 35.9, 39.0, 42.1, 45.5, 48.6, 51.7, 54.9, 58.2, 61.3, 64.5, + 67.8, 70.9, 73.9, 77.3, 80.3, 83.2, 86.1, 88.9, 91.4, 93.7, 95.6, 97.1, 98.3, 99.2, 99.7, 100.0, 100.0, 99.8, + 99.4, 98.6, 97.4, 95.9, 94.0, 91.3, 88.5 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_hf(): + generated = noaa_hf(levels, lowest, highest) + real = np.array([ + -6.0, -2.1, 1.3, 4.8, 8.4, 12.2, 15.7, 19.3, 23.0, 26.5, 30.0, 33.9, 37.3, 40.8, 44.4, 48.2, 51.7, + 55.3, 59.0, 62.5, 65.9, 69.7, 73.1, 76.5, 79.9, 83.4, 86.4, 89.4, 92.1, 94.3, 96.2, 97.8, 98.8, + 99.5, 99.9, 100.0, 99.9, 99.5, 98.7, 97.7, 96.2, 94.1, 91.7 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_pw(): + generated = noaa_pw(levels, lowest, highest) + real = np.array([ + 57.1, 59.3, 61.2, 63.1, 65.1, 67.2, 69.2, 71.2, 73.2, 75.2, 77.1, 79.2, 81.1, 83.1, 85.0, 87.0, 88.9, 90.7, + 92.5, 94.1, 95.5, 96.9, 97.9, 98.7, 99.3, 99.7, 99.9, 100.0, 99.9, 99.7, 99.3, 98.5, 97.5, 96.1, 94.3, 91.9, + 89.2, 86.1, 82.6, 79.1, 75.5, 71.4, 67.6 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_ow(): + generated = noaa_ow(levels, lowest, highest) + real = np.array([ + 25.6, 29.9, 33.8, 37.6, 41.6, 45.8, 49.6, 53.6, 57.8, 61.6, 65.4, 69.6, 73.4, 77.0, 80.7, 84.3, 87.5, 90.5, + 93.1, 95.1, 96.7, 98.0, 98.9, 99.4, 99.8, 100.0, 100.0, 99.9, 99.7, 99.3, 98.7, 97.6, 96.3, 94.6, 92.4, 89.6, + 86.7, 83.3, 79.6, 76.0, 72.3, 68.2, 64.4 ]) assert_array_almost_equal(real, generated) From bd03d7b2a75cc9e6455bf291590961408fdf213a Mon Sep 17 00:00:00 2001 From: Matej Simurda Date: Thu, 23 May 2019 12:55:03 +0200 Subject: [PATCH 2/4] Keep original 'weighting.py' and 'test_weighting.py' and moving all noaa-related logic/tests into separate files 'weighting_noaa.py' and 'test_weighting_noaa.py' --- acoustics/weighting.py | 246 ++-------------------------------- acoustics/weighting_noaa.py | 251 +++++++++++++++++++++++++++++++++++ tests/test_weighting.py | 87 +++--------- tests/test_weighting_noaa.py | 65 +++++++++ 4 files changed, 341 insertions(+), 308 deletions(-) create mode 100644 acoustics/weighting_noaa.py create mode 100644 tests/test_weighting_noaa.py diff --git a/acoustics/weighting.py b/acoustics/weighting.py index 0585f405..25f58804 100644 --- a/acoustics/weighting.py +++ b/acoustics/weighting.py @@ -2,9 +2,7 @@ Weighting ========= -Weightings according to IEC 61672-1:2003 and NOAA weightings as defined -in 'Technical Guidance for Assessing the Effects of Anthropogenic Sound on Marine Mammal Hearing' -(https://www.fisheries.noaa.gov/resource/document/technical-guidance-assessing-effects-anthropogenic-sound-marine-mammal) +Weightings according to IEC 61672-1:2003. """ @@ -14,70 +12,18 @@ THIRD_OCTAVE_A_WEIGHTING = np.array([ -63.4, -56.7, -50.5, -44.7, -39.4, -34.6, -30.2, -26.2, -22.5, -19.1, -16.1, -13.4, -10.9, -8.6, -6.6, -4.8, -3.2, - -1.9, -0.8, +0.0, +0.6, +1.0, +1.2, +1.3, +1.2, +1.0, +0.5, -0.1, -1.1, -2.5, -4.3, -6.6, -9.3, -12.3, -15.7, -19.4, - -23.0, -26.8, -30.9, -34.7, -38.5, -42.8, -46.6 + -1.9, -0.8, +0.0, +0.6, +1.0, +1.2, +1.3, +1.2, +1.0, +0.5, -0.1, -1.1, -2.5, -4.3, -6.6, -9.3 ]) """ -A-weighting filter for preferred 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +A-weighting filter for preferred 1/3-octave band center frequencies, as specified in :attr:`acoustics.bands.THIRD_OCTAVE_CENTER_FREQUENCIES`. """ THIRD_OCTAVE_C_WEIGHTING = np.array([ -11.2, -8.5, -6.2, -4.4, -3.0, -2.0, -1.3, -0.8, -0.5, -0.3, -0.2, -0.1, +0.0, +0.0, +0.0, +0.0, +0.0, +0.0, +0.0, - +0.0, +0.0, -0.1, -0.2, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.4, -6.2, -8.5, -11.2, -14.3, -17.6, -21.3, -24.9, - -28.8, -32.8, -36.6, -40.4, -44.7, -48.6 + +0.0, +0.0, -0.1, -0.2, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.4, -6.2, -8.5, -11.2 ]) """ -C-weighting filter for preferred 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. -""" - -THIRD_OCTAVE_NOAA_LF_WEIGHTING = np.array([ - -24.0, -21.8, -19.9, -18.0, -16.0, -14.0, -12.2, -10.3, -8.5, -6.9, -5.4, -4.0, -2.9, -2.0, -1.3, -0.8, -0.5, -0.3, - -0.1, -0.1, -0.0, 0.0, -0.0, -0.0, -0.1, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.5, -6.3, -8.6, -11.3, -14.6, - -17.9, -21.4, -25.3, -29.0, -32.8, -37.0, -40.8 -]) - -""" -NOAA LF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. -""" - -THIRD_OCTAVE_NOAA_MF_WEIGHTING = np.array([ - -89.9, -86.5, -83.4, -80.3, -77.1, -73.8, -70.7, -67.4, -64.1, -61.0, -57.9, -54.5, -51.4, -48.3, -45.1, -41.8, - -38.7, -35.5, -32.2, -29.1, -26.1, -22.7, -19.7, -16.8, -13.9, -11.1, -8.6, -6.3, -4.4, -2.9, -1.7, -0.8, -0.3, - -0.0, -0.0, -0.2, -0.6, -1.4, -2.6, -4.1, -6.0, -8.7, -11.5 -]) - -""" -NOAA MF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. -""" - -THIRD_OCTAVE_NOAA_HF_WEIGHTING = np.array([ - -106.0, -102.1, -98.7, -95.2, -91.6, -87.8, -84.3, -80.7, -77.0, -73.5, -70.0, -66.1, -62.7, -59.2, -55.6, -51.8, - -48.3, -44.7, -41.0, -37.5, -34.1, -30.3, -26.9, -23.5, -20.1, -16.6, -13.6, -10.6, -7.9, -5.7, -3.8, -2.2, -1.2, - -0.5, -0.1, 0.0, -0.1, -0.5, -1.3, -2.3, -3.8, -5.9, -8.3 -]) - -""" -NOAA HF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. -""" - -THIRD_OCTAVE_NOAA_PW_WEIGHTING = np.array([ - -42.9, -40.7, -38.8, -36.9, -34.9, -32.8, -30.8, -28.8, -26.8, -24.8, -22.9, -20.8, -18.9, -16.9, -15.0, -13.0, - -11.1, -9.3, -7.5, -5.9, -4.5, -3.1, -2.1, -1.3, -0.7, -0.3, -0.1, -0.0, -0.1, -0.3, -0.7, -1.5, -2.5, -3.9, -5.7, - -8.1, -10.8, -13.9, -17.4, -20.9, -24.5, -28.6, -32.4 -]) - -""" -NOAA PW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. -""" - -THIRD_OCTAVE_NOAA_OW_WEIGHTING = np.array([ - -74.4, -70.1, -66.2, -62.4, -58.4, -54.2, -50.4, -46.4, -42.2, -38.4, -34.6, -30.4, -26.6, -23.0, -19.3, -15.7, - -12.5, -9.5, -6.9, -4.9, -3.3, -2.0, -1.1, -0.6, -0.2, -0.0, -0.0, -0.1, -0.3, -0.7, -1.3, -2.4, -3.7, -5.4, -7.6, - -10.4, -13.3, -16.7, -20.4, -24.0, -27.7, -31.8, -35.6 -]) - -""" -NOAA OW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +C-weighting filter for preferred 1/3-octave band center frequencies, as specified in :attr:`acoustics.bands.THIRD_OCTAVE_CENTER_FREQUENCIES`. """ @@ -86,7 +32,7 @@ def a_weighting(first, last): Select frequency weightings between ``first`` and ``last`` centerfrequencies from A-weighting. Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). + between 12.5 Hz and 20,000 Hz (including them). Parameters ---------- @@ -109,7 +55,7 @@ def c_weighting(first, last): Select frequency weightings between ``first`` and ``last`` centerfrequencies from C-weighting. Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). + between 12.5 Hz and 20,000 Hz (including them). Parameters ---------- @@ -127,124 +73,8 @@ def c_weighting(first, last): return _weighting("c", first, last) -def noaa_lf_weighting(first, last): - """ - Select frequency weightings between ``first`` and ``last`` - centerfrequencies from NOAA LF-weighting. - Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). - - Parameters - ---------- - first : scalar - First third-octave centerfrequency. - - last : scalar - Last third-octave centerfrequency. - - Returns - ------- - NumPy array with NOAA LF-weighting between ``first`` and ``last`` - centerfrequencies. - """ - return _weighting("noaa_lf", first, last) - - -def noaa_mf_weighting(first, last): - """ - Select frequency weightings between ``first`` and ``last`` - centerfrequencies from NOAA MF-weighting. - Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). - - Parameters - ---------- - first : scalar - First third-octave centerfrequency. - - last : scalar - Last third-octave centerfrequency. - - Returns - ------- - NumPy array with NOAA MF-weighting between ``first`` and ``last`` - centerfrequencies. - """ - return _weighting("noaa_mf", first, last) - - -def noaa_hf_weighting(first, last): - """ - Select frequency weightings between ``first`` and ``last`` - centerfrequencies from NOAA HF-weighting. - Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). - - Parameters - ---------- - first : scalar - First third-octave centerfrequency. - - last : scalar - Last third-octave centerfrequency. - - Returns - ------- - NumPy array with NOAA HF-weighting between ``first`` and ``last`` - centerfrequencies. - """ - return _weighting("noaa_hf", first, last) - - -def noaa_pw_weighting(first, last): - """ - Select frequency weightings between ``first`` and ``last`` - centerfrequencies from NOAA PW-weighting. - Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). - - Parameters - ---------- - first : scalar - First third-octave centerfrequency. - - last : scalar - Last third-octave centerfrequency. - - Returns - ------- - NumPy array with NOAA PW-weighting between ``first`` and ``last`` - centerfrequencies. - """ - return _weighting("noaa_pw", first, last) - - - -def noaa_ow_weighting(first, last): - """ - Select frequency weightings between ``first`` and ``last`` - centerfrequencies from NOAA OW-weighting. - Possible values for these frequencies are third-octave frequencies - between 12.5 Hz and 200,000 Hz (including them). - - Parameters - ---------- - first : scalar - First third-octave centerfrequency. - - last : scalar - Last third-octave centerfrequency. - - Returns - ------- - NumPy array with NOAA OW-weighting between ``first`` and ``last`` - centerfrequencies. - """ - return _weighting("noaa_ow", first, last) - - def _weighting(filter_type, first, last): - third_oct_bands = third(12.5, 200000.0).tolist() + third_oct_bands = third(12.5, 20000.0).tolist() low = third_oct_bands.index(first) high = third_oct_bands.index(last) @@ -254,21 +84,6 @@ def _weighting(filter_type, first, last): elif filter_type == "c": freq_weightings = THIRD_OCTAVE_C_WEIGHTING - elif filter_type == "noaa_lf": - freq_weightings = THIRD_OCTAVE_NOAA_LF_WEIGHTING - - elif filter_type == "noaa_mf": - freq_weightings = THIRD_OCTAVE_NOAA_MF_WEIGHTING - - elif filter_type == "noaa_hf": - freq_weightings = THIRD_OCTAVE_NOAA_HF_WEIGHTING - - elif filter_type == "noaa_pw": - freq_weightings = THIRD_OCTAVE_NOAA_PW_WEIGHTING - - elif filter_type == "noaa_ow": - freq_weightings = THIRD_OCTAVE_NOAA_OW_WEIGHTING - return freq_weightings[low:high + 1] @@ -310,60 +125,15 @@ def c2a(levels, first, last): return z2a(dB, first, last) -def noaa_lf(levels, first, last): - """Apply NOAA LF-weighting to unweighted signal. - """ - return levels + noaa_lf_weighting(first, last) - - -def noaa_mf(levels, first, last): - """Apply NOAA MF-weighting to unweighted signal. - """ - return levels + noaa_mf_weighting(first, last) - - -def noaa_hf(levels, first, last): - """Apply NOAA HF-weighting to unweighted signal. - """ - return levels + noaa_hf_weighting(first, last) - - -def noaa_pw(levels, first, last): - """Apply NOAA PW-weighting to unweighted signal. - """ - return levels + noaa_pw_weighting(first, last) - - -def noaa_ow(levels, first, last): - """Apply NOAA OW-weighting to unweighted signal. - """ - return levels + noaa_ow_weighting(first, last) - - __all__ = [ 'THIRD_OCTAVE_A_WEIGHTING', 'THIRD_OCTAVE_C_WEIGHTING', - 'THIRD_OCTAVE_NOAA_LF_WEIGHTING', - 'THIRD_OCTAVE_NOAA_MF_WEIGHTING', - 'THIRD_OCTAVE_NOAA_HF_WEIGHTING', - 'THIRD_OCTAVE_NOAA_PW_WEIGHTING', - 'THIRD_OCTAVE_NOAA_OW_WEIGHTING', 'a_weighting', 'c_weighting', - 'noaa_lf_weighting', - 'noaa_mf_weighting', - 'noaa_hf_weighting', - 'noaa_pw_weighting', - 'noaa_ow_weighting', 'z2a', 'a2z', 'z2c', 'c2z', 'a2c', 'c2a', - 'noaa_lf', - 'noaa_mf', - 'noaa_hf', - 'noaa_pw', - 'noaa_ow', ] diff --git a/acoustics/weighting_noaa.py b/acoustics/weighting_noaa.py new file mode 100644 index 00000000..1487b048 --- /dev/null +++ b/acoustics/weighting_noaa.py @@ -0,0 +1,251 @@ +""" +Weighting_NOAA +========= + +NOAA weightings as defined +in 'Technical Guidance for Assessing the Effects of Anthropogenic Sound on Marine Mammal Hearing' +(https://www.fisheries.noaa.gov/resource/document/technical-guidance-assessing-effects-anthropogenic-sound-marine-mammal) + + +""" +import numpy as np + +from acoustics.bands import third + +THIRD_OCTAVE_NOAA_LF_WEIGHTING = np.array([ + -24.0, -21.8, -19.9, -18.0, -16.0, -14.0, -12.2, -10.3, -8.5, -6.9, -5.4, -4.0, -2.9, -2.0, -1.3, -0.8, -0.5, -0.3, + -0.1, -0.1, -0.0, 0.0, -0.0, -0.0, -0.1, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.5, -6.3, -8.6, -11.3, -14.6, + -17.9, -21.4, -25.3, -29.0, -32.8, -37.0, -40.8 +]) + +""" +NOAA LF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_MF_WEIGHTING = np.array([ + -89.9, -86.5, -83.4, -80.3, -77.1, -73.8, -70.7, -67.4, -64.1, -61.0, -57.9, -54.5, -51.4, -48.3, -45.1, -41.8, + -38.7, -35.5, -32.2, -29.1, -26.1, -22.7, -19.7, -16.8, -13.9, -11.1, -8.6, -6.3, -4.4, -2.9, -1.7, -0.8, -0.3, + -0.0, -0.0, -0.2, -0.6, -1.4, -2.6, -4.1, -6.0, -8.7, -11.5 +]) + +""" +NOAA MF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_HF_WEIGHTING = np.array([ + -106.0, -102.1, -98.7, -95.2, -91.6, -87.8, -84.3, -80.7, -77.0, -73.5, -70.0, -66.1, -62.7, -59.2, -55.6, -51.8, + -48.3, -44.7, -41.0, -37.5, -34.1, -30.3, -26.9, -23.5, -20.1, -16.6, -13.6, -10.6, -7.9, -5.7, -3.8, -2.2, -1.2, + -0.5, -0.1, 0.0, -0.1, -0.5, -1.3, -2.3, -3.8, -5.9, -8.3 +]) + +""" +NOAA HF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_PW_WEIGHTING = np.array([ + -42.9, -40.7, -38.8, -36.9, -34.9, -32.8, -30.8, -28.8, -26.8, -24.8, -22.9, -20.8, -18.9, -16.9, -15.0, -13.0, + -11.1, -9.3, -7.5, -5.9, -4.5, -3.1, -2.1, -1.3, -0.7, -0.3, -0.1, -0.0, -0.1, -0.3, -0.7, -1.5, -2.5, -3.9, -5.7, + -8.1, -10.8, -13.9, -17.4, -20.9, -24.5, -28.6, -32.4 +]) + +""" +NOAA PW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + +THIRD_OCTAVE_NOAA_OW_WEIGHTING = np.array([ + -74.4, -70.1, -66.2, -62.4, -58.4, -54.2, -50.4, -46.4, -42.2, -38.4, -34.6, -30.4, -26.6, -23.0, -19.3, -15.7, + -12.5, -9.5, -6.9, -4.9, -3.3, -2.0, -1.1, -0.6, -0.2, -0.0, -0.0, -0.1, -0.3, -0.7, -1.3, -2.4, -3.7, -5.4, -7.6, + -10.4, -13.3, -16.7, -20.4, -24.0, -27.7, -31.8, -35.6 +]) + +""" +NOAA OW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. +""" + + +def noaa_lf_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA LF-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA LF-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_lf", first, last) + + +def noaa_mf_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA MF-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA MF-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_mf", first, last) + + +def noaa_hf_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA HF-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA HF-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_hf", first, last) + + +def noaa_pw_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA PW-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA PW-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_pw", first, last) + + + +def noaa_ow_weighting(first, last): + """ + Select frequency weightings between ``first`` and ``last`` + centerfrequencies from NOAA OW-weighting. + Possible values for these frequencies are third-octave frequencies + between 12.5 Hz and 200,000 Hz (including them). + + Parameters + ---------- + first : scalar + First third-octave centerfrequency. + + last : scalar + Last third-octave centerfrequency. + + Returns + ------- + NumPy array with NOAA OW-weighting between ``first`` and ``last`` + centerfrequencies. + """ + return _weighting("noaa_ow", first, last) + + +def _weighting(filter_type, first, last): + third_oct_bands = third(12.5, 200000.0).tolist() + low = third_oct_bands.index(first) + high = third_oct_bands.index(last) + + if filter_type == "noaa_lf": + freq_weightings = THIRD_OCTAVE_NOAA_LF_WEIGHTING + + elif filter_type == "noaa_mf": + freq_weightings = THIRD_OCTAVE_NOAA_MF_WEIGHTING + + elif filter_type == "noaa_hf": + freq_weightings = THIRD_OCTAVE_NOAA_HF_WEIGHTING + + elif filter_type == "noaa_pw": + freq_weightings = THIRD_OCTAVE_NOAA_PW_WEIGHTING + + elif filter_type == "noaa_ow": + freq_weightings = THIRD_OCTAVE_NOAA_OW_WEIGHTING + + return freq_weightings[low:high + 1] + + +def noaa_lf(levels, first, last): + """Apply NOAA LF-weighting to unweighted signal. + """ + return levels + noaa_lf_weighting(first, last) + + +def noaa_mf(levels, first, last): + """Apply NOAA MF-weighting to unweighted signal. + """ + return levels + noaa_mf_weighting(first, last) + + +def noaa_hf(levels, first, last): + """Apply NOAA HF-weighting to unweighted signal. + """ + return levels + noaa_hf_weighting(first, last) + + +def noaa_pw(levels, first, last): + """Apply NOAA PW-weighting to unweighted signal. + """ + return levels + noaa_pw_weighting(first, last) + + +def noaa_ow(levels, first, last): + """Apply NOAA OW-weighting to unweighted signal. + """ + return levels + noaa_ow_weighting(first, last) + + +__all__ = [ + 'THIRD_OCTAVE_NOAA_LF_WEIGHTING', + 'THIRD_OCTAVE_NOAA_MF_WEIGHTING', + 'THIRD_OCTAVE_NOAA_HF_WEIGHTING', + 'THIRD_OCTAVE_NOAA_PW_WEIGHTING', + 'THIRD_OCTAVE_NOAA_OW_WEIGHTING', + 'noaa_lf_weighting', + 'noaa_mf_weighting', + 'noaa_hf_weighting', + 'noaa_pw_weighting', + 'noaa_ow_weighting', + 'noaa_lf', + 'noaa_mf', + 'noaa_hf', + 'noaa_pw', + 'noaa_ow', +] diff --git a/tests/test_weighting.py b/tests/test_weighting.py index 64ce6e2d..998ff74a 100644 --- a/tests/test_weighting.py +++ b/tests/test_weighting.py @@ -2,22 +2,21 @@ import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal -from acoustics.weighting import z2a, a2z, z2c, c2z, a2c, c2a, noaa_lf, noaa_mf, noaa_hf, noaa_pw, noaa_ow +from acoustics.weighting import z2a, a2z, z2c, c2z, a2c, c2a def setup_module(weighting): - weighting.levels = 100 * np.ones(43) + weighting.levels = 100 * np.ones(33) weighting.lowest = 12.5 - weighting.highest = 200000 + weighting.highest = 20000 def test_z2a(): generated = z2a(levels, lowest, highest) real = np.array([ 36.6, 43.3, 49.5, 55.3, 60.6, 65.4, 69.8, 73.8, 77.5, 80.9, 83.9, 86.6, 89.1, 91.4, 93.4, 95.2, 96.8, 98.1, - 99.2, 100., 100.6, 101., 101.2, 101.3, 101.2, 101., 100.5, 99.9, 98.9, 97.5, 95.7, 93.4, 90.7, 87.7, 84.3, - 80.6, 77., 73.2, 69.1, 65.3, 61.5, 57.2, 53.4 - ]) + 99.2, 100, 100.6, 101, 101.2, 101.3, 101.2, 101, 100.5, 99.9, 98.9, 97.5, 95.7, 93.4, 90.7 + ]) assert_array_equal(real, generated) @@ -25,17 +24,16 @@ def test_a2z(): generated = a2z(levels, lowest, highest) real = np.array([ 163.4, 156.7, 150.5, 144.7, 139.4, 134.6, 130.2, 126.2, 122.5, 119.1, 116.1, 113.4, 110.9, 108.6, 106.6, 104.8, - 103.2, 101.9, 100.8, 100., 99.4, 99., 98.8, 98.7, 98.8, 99., 99.5, 100.1, 101.1, 102.5, 104.3, 106.6, 109.3, - 112.3, 115.7, 119.4, 123., 126.8, 130.9, 134.7, 138.5, 142.8, 146.6]) + 103.2, 101.9, 100.8, 100, 99.4, 99, 98.8, 98.7, 98.8, 99, 99.5, 100.1, 101.1, 102.5, 104.3, 106.6, 109.3 + ]) assert_array_equal(real, generated) def test_z2c(): generated = z2c(levels, lowest, highest) real = np.array([ - 88.8, 91.5, 93.8, 95.6, 97., 98., 98.7, 99.2, 99.5, 99.7, 99.8, 99.9, 100., 100., 100., 100., 100., 100., 100., - 100., 100., 99.9, 99.8, 99.7, 99.5, 99.2, 98.7, 98., 97., 95.6, 93.8, 91.5, 88.8, 85.7, 82.4, 78.7, 75.1, 71.2, - 67.2, 63.4, 59.6, 55.3, 51.4 + 88.8, 91.5, 93.8, 95.6, 97, 98, 98.7, 99.2, 99.5, 99.7, 99.8, 99.9, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 99.9, 99.8, 99.7, 99.5, 99.2, 98.7, 98, 97, 95.6, 93.8, 91.5, 88.8 ]) assert_array_equal(real, generated) @@ -43,77 +41,26 @@ def test_z2c(): def test_c2z(): generated = c2z(levels, lowest, highest) real = np.array([ - 111.2, 108.5, 106.2, 104.4, 103., 102., 101.3, 100.8, 100.5,100.3, 100.2, 100.1, 100., 100., 100., 100., 100., - 100., 100., 100., 100., 100.1, 100.2, 100.3, 100.5, 100.8, 101.3,102., 103., 104.4, 106.2, 108.5, 111.2, 114.3, - 117.6, 121.3, 124.9, 128.8, 132.8, 136.6, 140.4, 144.7, 148.6]) + 111.2, 108.5, 106.2, 104.4, 103, 102, 101.3, 100.8, 100.5, 100.3, 100.2, 100.1, 100, 100, 100, 100, 100, 100, + 100, 100, 100, 100.1, 100.2, 100.3, 100.5, 100.8, 101.3, 102, 103, 104.4, 106.2, 108.5, 111.2 + ]) assert_array_equal(real, generated) def test_a2c(): generated = a2c(levels, lowest, highest) real = np.array([ - 152.2, 148.2, 144.3, 140.3, 136.4, 132.6, 128.9, 125.4, 122., 118.8, 115.9, 113.3, 110.9, 108.6, 106.6, 104.8, - 103.2, 101.9, 100.8, 100., 99.4, 98.9, 98.6, 98.4, 98.3, 98.2, 98.2, 98.1, 98.1, 98.1, 98.1, 98.1, 98.1, 98., - 98.1, 98.1, 98.1, 98., 98.1, 98.1, 98.1, 98.1, 98.]) + 152.2, 148.2, 144.3, 140.3, 136.4, 132.6, 128.9, 125.4, 122, 118.8, 115.9, 113.3, 110.9, 108.6, 106.6, 104.8, + 103.2, 101.9, 100.8, 100, 99.4, 98.9, 98.6, 98.4, 98.3, 98.2, 98.2, 98.1, 98.1, 98.1, 98.1, 98.1, 98.1 + ]) assert_array_almost_equal(real, generated) def test_c2a(): generated = c2a(levels, lowest, highest) real = np.array([ - 47.8, 51.8, 55.7, 59.7, 63.6, 67.4, 71.1, 74.6, 78., 81.2, 84.1, 86.7, 89.1, 91.4, 93.4, 95.2, 96.8, 98.1, - 99.2, 100., 100.6, 101.1, 101.4, 101.6, 101.7, 101.8, 101.8, 101.9, 101.9, 101.9, 101.9, 101.9, 101.9, 102., - 101.9, 101.9, 101.9, 102., 101.9, 101.9, 101.9, 101.9, 102. - ]) - assert_array_almost_equal(real, generated) - - -def test_noaa_lf(): - generated = noaa_lf(levels, lowest, highest) - real = np.array([ - 76.0, 78.2, 80.1, 82.0, 84.0, 86.0, 87.8, 89.7, 91.5, 93.1, 94.6, 96.0, 97.1, 98.0, 98.7, 99.2, 99.5, 99.7, - 99.9, 99.9, 100.0, 100.0, 100.0, 100.0, 99.9, 99.7, 99.5, 99.2, 98.7, 98.0, 97.0, 95.5, 93.7, 91.4, 88.7, 85.4, - 82.1, 78.6, 74.7, 71.0, 67.2, 63.0, 59.2 - ]) - assert_array_almost_equal(real, generated) - - -def test_noaa_mf(): - generated = noaa_mf(levels, lowest, highest) - real = np.array([ - 10.1, 13.5, 16.6, 19.7, 22.9, 26.2, 29.3, 32.6, 35.9, 39.0, 42.1, 45.5, 48.6, 51.7, 54.9, 58.2, 61.3, 64.5, - 67.8, 70.9, 73.9, 77.3, 80.3, 83.2, 86.1, 88.9, 91.4, 93.7, 95.6, 97.1, 98.3, 99.2, 99.7, 100.0, 100.0, 99.8, - 99.4, 98.6, 97.4, 95.9, 94.0, 91.3, 88.5 - ]) - assert_array_almost_equal(real, generated) - - -def test_noaa_hf(): - generated = noaa_hf(levels, lowest, highest) - real = np.array([ - -6.0, -2.1, 1.3, 4.8, 8.4, 12.2, 15.7, 19.3, 23.0, 26.5, 30.0, 33.9, 37.3, 40.8, 44.4, 48.2, 51.7, - 55.3, 59.0, 62.5, 65.9, 69.7, 73.1, 76.5, 79.9, 83.4, 86.4, 89.4, 92.1, 94.3, 96.2, 97.8, 98.8, - 99.5, 99.9, 100.0, 99.9, 99.5, 98.7, 97.7, 96.2, 94.1, 91.7 - ]) - assert_array_almost_equal(real, generated) - - -def test_noaa_pw(): - generated = noaa_pw(levels, lowest, highest) - real = np.array([ - 57.1, 59.3, 61.2, 63.1, 65.1, 67.2, 69.2, 71.2, 73.2, 75.2, 77.1, 79.2, 81.1, 83.1, 85.0, 87.0, 88.9, 90.7, - 92.5, 94.1, 95.5, 96.9, 97.9, 98.7, 99.3, 99.7, 99.9, 100.0, 99.9, 99.7, 99.3, 98.5, 97.5, 96.1, 94.3, 91.9, - 89.2, 86.1, 82.6, 79.1, 75.5, 71.4, 67.6 - ]) - assert_array_almost_equal(real, generated) - - -def test_noaa_ow(): - generated = noaa_ow(levels, lowest, highest) - real = np.array([ - 25.6, 29.9, 33.8, 37.6, 41.6, 45.8, 49.6, 53.6, 57.8, 61.6, 65.4, 69.6, 73.4, 77.0, 80.7, 84.3, 87.5, 90.5, - 93.1, 95.1, 96.7, 98.0, 98.9, 99.4, 99.8, 100.0, 100.0, 99.9, 99.7, 99.3, 98.7, 97.6, 96.3, 94.6, 92.4, 89.6, - 86.7, 83.3, 79.6, 76.0, 72.3, 68.2, 64.4 + 47.8, 51.8, 55.7, 59.7, 63.6, 67.4, 71.1, 74.6, 78, 81.2, 84.1, 86.7, 89.1, 91.4, 93.4, 95.2, 96.8, 98.1, 99.2, + 100, 100.6, 101.1, 101.4, 101.6, 101.7, 101.8, 101.8, 101.9, 101.9, 101.9, 101.9, 101.9, 101.9 ]) assert_array_almost_equal(real, generated) diff --git a/tests/test_weighting_noaa.py b/tests/test_weighting_noaa.py new file mode 100644 index 00000000..77b19737 --- /dev/null +++ b/tests/test_weighting_noaa.py @@ -0,0 +1,65 @@ +import pytest +import numpy as np +from numpy.testing import assert_array_equal, assert_array_almost_equal + +from acoustics.weighting_noaa import noaa_lf, noaa_mf, noaa_hf, noaa_pw, noaa_ow + + +def setup_module(weighting_noaa): + weighting_noaa.levels = 100 * np.ones(43) + weighting_noaa.lowest = 12.5 + weighting_noaa.highest = 200000 + + +def test_noaa_lf(): + generated = noaa_lf(levels, lowest, highest) + real = np.array([ + 76.0, 78.2, 80.1, 82.0, 84.0, 86.0, 87.8, 89.7, 91.5, 93.1, 94.6, 96.0, 97.1, 98.0, 98.7, 99.2, 99.5, 99.7, + 99.9, 99.9, 100.0, 100.0, 100.0, 100.0, 99.9, 99.7, 99.5, 99.2, 98.7, 98.0, 97.0, 95.5, 93.7, 91.4, 88.7, 85.4, + 82.1, 78.6, 74.7, 71.0, 67.2, 63.0, 59.2 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_mf(): + generated = noaa_mf(levels, lowest, highest) + real = np.array([ + 10.1, 13.5, 16.6, 19.7, 22.9, 26.2, 29.3, 32.6, 35.9, 39.0, 42.1, 45.5, 48.6, 51.7, 54.9, 58.2, 61.3, 64.5, + 67.8, 70.9, 73.9, 77.3, 80.3, 83.2, 86.1, 88.9, 91.4, 93.7, 95.6, 97.1, 98.3, 99.2, 99.7, 100.0, 100.0, 99.8, + 99.4, 98.6, 97.4, 95.9, 94.0, 91.3, 88.5 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_hf(): + generated = noaa_hf(levels, lowest, highest) + real = np.array([ + -6.0, -2.1, 1.3, 4.8, 8.4, 12.2, 15.7, 19.3, 23.0, 26.5, 30.0, 33.9, 37.3, 40.8, 44.4, 48.2, 51.7, + 55.3, 59.0, 62.5, 65.9, 69.7, 73.1, 76.5, 79.9, 83.4, 86.4, 89.4, 92.1, 94.3, 96.2, 97.8, 98.8, + 99.5, 99.9, 100.0, 99.9, 99.5, 98.7, 97.7, 96.2, 94.1, 91.7 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_pw(): + generated = noaa_pw(levels, lowest, highest) + real = np.array([ + 57.1, 59.3, 61.2, 63.1, 65.1, 67.2, 69.2, 71.2, 73.2, 75.2, 77.1, 79.2, 81.1, 83.1, 85.0, 87.0, 88.9, 90.7, + 92.5, 94.1, 95.5, 96.9, 97.9, 98.7, 99.3, 99.7, 99.9, 100.0, 99.9, 99.7, 99.3, 98.5, 97.5, 96.1, 94.3, 91.9, + 89.2, 86.1, 82.6, 79.1, 75.5, 71.4, 67.6 + ]) + assert_array_almost_equal(real, generated) + + +def test_noaa_ow(): + generated = noaa_ow(levels, lowest, highest) + real = np.array([ + 25.6, 29.9, 33.8, 37.6, 41.6, 45.8, 49.6, 53.6, 57.8, 61.6, 65.4, 69.6, 73.4, 77.0, 80.7, 84.3, 87.5, 90.5, + 93.1, 95.1, 96.7, 98.0, 98.9, 99.4, 99.8, 100.0, 100.0, 99.9, 99.7, 99.3, 98.7, 97.6, 96.3, 94.6, 92.4, 89.6, + 86.7, 83.3, 79.6, 76.0, 72.3, 68.2, 64.4 + ]) + assert_array_almost_equal(real, generated) + + +def teardown_module(weighting): + pass From 1f2166c281ed02598fad6647ae733f3fac6c1076 Mon Sep 17 00:00:00 2001 From: Matej Simurda Date: Thu, 23 May 2019 20:26:22 +0200 Subject: [PATCH 3/4] Implemeting comments from FRidh --- acoustics/weighting_noaa.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/acoustics/weighting_noaa.py b/acoustics/weighting_noaa.py index 1487b048..586467a7 100644 --- a/acoustics/weighting_noaa.py +++ b/acoustics/weighting_noaa.py @@ -1,6 +1,6 @@ """ Weighting_NOAA -========= +============== NOAA weightings as defined in 'Technical Guidance for Assessing the Effects of Anthropogenic Sound on Marine Mammal Hearing' @@ -17,7 +17,6 @@ -0.1, -0.1, -0.0, 0.0, -0.0, -0.0, -0.1, -0.3, -0.5, -0.8, -1.3, -2.0, -3.0, -4.5, -6.3, -8.6, -11.3, -14.6, -17.9, -21.4, -25.3, -29.0, -32.8, -37.0, -40.8 ]) - """ NOAA LF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ @@ -27,7 +26,6 @@ -38.7, -35.5, -32.2, -29.1, -26.1, -22.7, -19.7, -16.8, -13.9, -11.1, -8.6, -6.3, -4.4, -2.9, -1.7, -0.8, -0.3, -0.0, -0.0, -0.2, -0.6, -1.4, -2.6, -4.1, -6.0, -8.7, -11.5 ]) - """ NOAA MF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ @@ -37,7 +35,6 @@ -48.3, -44.7, -41.0, -37.5, -34.1, -30.3, -26.9, -23.5, -20.1, -16.6, -13.6, -10.6, -7.9, -5.7, -3.8, -2.2, -1.2, -0.5, -0.1, 0.0, -0.1, -0.5, -1.3, -2.3, -3.8, -5.9, -8.3 ]) - """ NOAA HF-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ @@ -47,7 +44,6 @@ -11.1, -9.3, -7.5, -5.9, -4.5, -3.1, -2.1, -1.3, -0.7, -0.3, -0.1, -0.0, -0.1, -0.3, -0.7, -1.5, -2.5, -3.9, -5.7, -8.1, -10.8, -13.9, -17.4, -20.9, -24.5, -28.6, -32.4 ]) - """ NOAA PW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ @@ -57,7 +53,6 @@ -12.5, -9.5, -6.9, -4.9, -3.3, -2.0, -1.1, -0.6, -0.2, -0.0, -0.0, -0.1, -0.3, -0.7, -1.3, -2.4, -3.7, -5.4, -7.6, -10.4, -13.3, -16.7, -20.4, -24.0, -27.7, -31.8, -35.6 ]) - """ NOAA OW-weighting filter for 1/3-octave band center frequencies, as specified by :attr:`acoustics.bands.third(12.5,200000)`. """ From d69a7dcb28d38f78b038edb476db39c59a455775 Mon Sep 17 00:00:00 2001 From: Matej Simurda Date: Thu, 23 May 2019 20:32:20 +0200 Subject: [PATCH 4/4] URL according to restructuredtext guidelines --- acoustics/weighting_noaa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acoustics/weighting_noaa.py b/acoustics/weighting_noaa.py index 586467a7..e46fc62c 100644 --- a/acoustics/weighting_noaa.py +++ b/acoustics/weighting_noaa.py @@ -3,8 +3,8 @@ ============== NOAA weightings as defined -in 'Technical Guidance for Assessing the Effects of Anthropogenic Sound on Marine Mammal Hearing' -(https://www.fisheries.noaa.gov/resource/document/technical-guidance-assessing-effects-anthropogenic-sound-marine-mammal) +in `Technical Guidance for Assessing the Effects of Anthropogenic Sound on Marine Mammal Hearing' +`_. """