-
Notifications
You must be signed in to change notification settings - Fork 2
Feature: Scattering ISO limits #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ahms5
wants to merge
8
commits into
develop
Choose a base branch
from
scattering/diffuse_iso
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc38cb2
add diffuse iso calculation
ahms5 8ce1f04
fix doc
ahms5 082fdd0
fix doc
ahms5 8a13014
Apply suggestions from code review
ahms5 241121a
cleanup
ahms5 6d5dae8
make pyfar.FrequencyData link to pyfar doc
ahms5 2b3e291
Apply suggestions from code review
ahms5 5bde6c6
Update tests/test_scattering_diffuse.py
ahms5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| imkar.scattering.diffuse | ||
| ======================== | ||
|
|
||
| .. automodule:: imkar.scattering.diffuse | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
| """Imkar scattering module.""" | ||
|
|
||
| from . import diffuse | ||
|
|
||
| __all__ = [ | ||
| "diffuse", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """ | ||
| This module contains functions for diffuse scattering calculations based on | ||
| ISO 17497-1:2004. | ||
| """ | ||
| import numpy as np | ||
| import pyfar as pf | ||
|
|
||
|
|
||
| def maximum_sample_absorption_coefficient(frequencies) -> pf.FrequencyData: | ||
| """Maximum absorption coefficient of the test sample. | ||
|
|
||
| Based on section 6.3.4 in ISO 17497-1:2004 [#]_ the absorption coefficient | ||
| of the test sample should not exceed a value of :math:`alpha_s=0.5`. | ||
| However, if sound absorption is part of the sound-scattering structure, | ||
| this absorption shall also be present in the test sample. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| frequencies : numpy.ndarray | ||
| The frequencies at which the absorption coefficient is calculated. | ||
|
|
||
| Returns | ||
| ------- | ||
| alpha_s_max : pyfar.FrequencyData | ||
| The maximum sample absorption coefficient. | ||
|
|
||
| References | ||
| ---------- | ||
| .. [#] ISO 17497-1:2004, Sound-scattering properties of surfaces. Part 1: | ||
| Measurement of the random-incidence scattering coefficient in a | ||
| reverberation room. Geneva, Switzerland: International Organization | ||
| for Standards, 2004. | ||
|
|
||
| """ | ||
| # input checks | ||
| try: | ||
| frequencies = np.asarray(frequencies, dtype=float) | ||
| except ValueError as exc: | ||
| raise TypeError( | ||
| "frequencies must be convertible to a float array.") from exc | ||
| if frequencies.ndim != 1: | ||
| raise ValueError("frequencies must be a 1D array.") | ||
|
|
||
| # Calculate the maximum absorption coefficient | ||
| return pf.FrequencyData( | ||
| data=np.ones_like(frequencies) * 0.5, | ||
| frequencies=frequencies, | ||
| comment="Maximum absorption coefficient of the test sample", | ||
| ) | ||
|
|
||
|
|
||
| def maximum_baseplate_scattering_coefficient(N: int = 1) -> pf.FrequencyData: | ||
| """Maximum scattering coefficient for the base plate alone. | ||
|
|
||
| This is based on Table 1 in ISO 17497-1:2004 [#]_. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| N : int | ||
| ratio of any linear dimension in a physical scale model to the | ||
| same linear dimension in full scale (1:N). The default is N=1. | ||
|
|
||
| Returns | ||
| ------- | ||
| s_base_max : pyfar.FrequencyData | ||
| The maximum baseplate scattering coefficient. | ||
|
|
||
| References | ||
| ---------- | ||
| .. [#] ISO 17497-1:2004, Sound-scattering properties of surfaces. Part 1: | ||
| Measurement of the random-incidence scattering coefficient in a | ||
| reverberation room. Geneva, Switzerland: International Organization | ||
| for Standards, 2004. | ||
|
|
||
| """ | ||
| if not isinstance(N, int): | ||
| raise TypeError("N must be a positive integer.") | ||
| if N <= 0: | ||
| raise TypeError("N must be a positive integer.") | ||
| frequencies = [ | ||
| 100, 125, 160, 200, 250, 315, 400, 500, 630, | ||
| 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, | ||
| ] | ||
| s_base_max = [ | ||
| 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.10, | ||
| 0.10, 0.10, 0.15, 0.15, 0.15, 0.20, 0.20, 0.20, 0.25, | ||
| ] | ||
| return pf.FrequencyData( | ||
| data=s_base_max, | ||
| frequencies=np.array(frequencies)/N, | ||
| comment="Maximum scattering coefficient of the baseplate", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import numpy as np | ||
| import pytest | ||
| import imkar.scattering.diffuse as isd | ||
| import pyfar as pf | ||
|
|
||
|
|
||
| def test_maximum_sample_absorption_coefficient_basic(): | ||
| freqs = np.array([100, 200, 400, 800]) | ||
| result = isd.maximum_sample_absorption_coefficient(freqs) | ||
| assert isinstance(result, pf.FrequencyData) | ||
| np.testing.assert_allclose(result.frequencies, freqs) | ||
| np.testing.assert_allclose(result.freq, 0.5) | ||
| assert "Maximum absorption coefficient" in result.comment | ||
|
|
||
|
|
||
| def test_maximum_sample_absorption_coefficient_list_input(): | ||
| freqs = [100, 200, 400] | ||
| result = isd.maximum_sample_absorption_coefficient(freqs) | ||
| np.testing.assert_allclose(result.frequencies, np.array(freqs)) | ||
| np.testing.assert_allclose(result.freq, 0.5) | ||
|
|
||
|
|
||
| def test_maximum_sample_absorption_coefficient_non_1d_input(): | ||
| freqs = np.array([[100, 200], [300, 400]]) | ||
| with pytest.raises(ValueError, match="frequencies must be a 1D array"): | ||
| isd.maximum_sample_absorption_coefficient(freqs) | ||
|
|
||
|
|
||
| def test_maximum_sample_absorption_coefficient_invalid_type(): | ||
| freqs = "not_a_number" | ||
| with pytest.raises( | ||
| TypeError, | ||
| match="frequencies must be convertible to a float array"): | ||
| isd.maximum_sample_absorption_coefficient(freqs) | ||
|
|
||
|
|
||
| def test_maximum_baseplate_scattering_coefficient_default(): | ||
| result = isd.maximum_baseplate_scattering_coefficient() | ||
| assert isinstance(result, pf.FrequencyData) | ||
| expected_freqs = np.array([ | ||
| 100, 125, 160, 200, 250, 315, 400, 500, 630, | ||
| 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, | ||
| ]) | ||
| expected_data = np.array([[ | ||
| 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.10, | ||
| 0.10, 0.10, 0.15, 0.15, 0.15, 0.20, 0.20, 0.20, 0.25, | ||
| ]]) | ||
| np.testing.assert_allclose(result.frequencies, expected_freqs) | ||
| np.testing.assert_allclose(result.freq, expected_data) | ||
| assert "baseplate" in result.comment | ||
|
|
||
|
|
||
| def test_maximum_baseplate_scattering_coefficient_with_scale(): | ||
| N = 2 | ||
| result = isd.maximum_baseplate_scattering_coefficient(N) | ||
| expected_freqs = np.array([ | ||
| 100, 125, 160, 200, 250, 315, 400, 500, 630, | ||
| 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, | ||
| ]) / N | ||
| np.testing.assert_allclose(result.frequencies, expected_freqs) | ||
|
|
||
| def test_maximum_baseplate_scattering_coefficient_invalid_type(): | ||
| with pytest.raises(TypeError, match="N must be a positive integer."): | ||
| isd.maximum_baseplate_scattering_coefficient(N=1.5) | ||
| with pytest.raises(TypeError, match="N must be a positive integer."): | ||
| isd.maximum_baseplate_scattering_coefficient(N=-1) | ||
|
|
||
| def test_maximum_baseplate_scattering_coefficient_negative_N(): | ||
ahms5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Negative N is technically an integer, but let's check if it works | ||
ahms5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| N = 5 | ||
| result = isd.maximum_baseplate_scattering_coefficient(N) | ||
| expected_freqs = np.array([ | ||
| 100, 125, 160, 200, 250, 315, 400, 500, 630, | ||
| 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, | ||
| ]) / N | ||
| np.testing.assert_allclose(result.frequencies, expected_freqs) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
_positive_N?