Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pyrato/parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@
"""
import numpy as np

def calculate_speed_of_sound(temperature):
r"""Calculate the speed of sound in air depending on the temperature.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The implementation is based on [#]_ .

Speed of sound is calculated as [#]_.

.. math::
c = c_0 \sqrt{\frac{T - T_0}{20 - T_0}}
.. math::
T_0=-273.15
.. math::
c_0 = 343.2

Parameters
----------
temperature : double
Temperature in degrees Celsius.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add explicit formula and cite properly the source, according to pyfar convention

Returns
-------
speed_of_sound : double
Speed of sound in m/s.

References
----------
.. [#] ISO 9613-1 (Formula A.5)
"""
if temperature < -273.15:
raise ValueError(
"Temperature must be greater than absolute zero (-273.15 °C).")
T_0 = -273.15
c_0 = 343.2
speed_of_sound = c_0 * np.sqrt((temperature - T_0)/(20 - T_0))
return speed_of_sound


def energy_decay_curve_analytic(
surfaces, alphas, volume, times, source=None,
receiver=None, method='eyring', c=343.4, frequency=None,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_speed_of_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for reverberation time related things."""
import numpy as np
import numpy.testing as npt

import pyrato as ra
import pytest

@pytest.mark.parametrize('T',[0,20,1000])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing 2 situations in this statement: those who work (20, 1000) and those who fail (0). Extract?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No the zero should also work.

def test_speed_of_sound(T):
speed=ra.parametric.calculate_speed_of_sound(T)
assert isinstance(T, (int, float))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant? Implicit in specification of param

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you specify?

npt.assert_allclose(speed, 343.2 * np.sqrt((T + 273.15) / 293.15))
with pytest.raises(ValueError,match=
"Temperature must be greater than absolute zero"):
ra.parametric.calculate_speed_of_sound(-300)