Skip to content

Commit

Permalink
Gardening
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandjansen committed Nov 9, 2017
1 parent c5d0cdc commit 972e800
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 182 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
resources/* linguist-vendored
external/* linguist-vendored
resources/* linguist-vendored
.ycm_extra_conf.py linguist-vendored

4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ set(clang_tidy_line_filters "[{'name': 'external/*'}]")
set(base_clang_tidy "clang-tidy-5.0;-line-filter=${clang_tidy_line_filters}")
if(CMAKE_BUILD_TYPE MATCHES DEBUG)
message("DEBUG mode")
set(build_type_flags "-g -Og --coverage")
set(build_type_flags "-g -Og --coverage -fno-exceptions")
set(CMAKE_CXX_CLANG_TIDY "${base_clang_tidy}")
else()
message("RELEASE mode")
set(build_type_flags "-O3 -Werror")
set(build_type_flags "-O3 -Werror -fno-exceptions")
set(CMAKE_CXX_CLANG_TIDY "${base_clang_tidy};-warnings-as-errors=*")
endif()

Expand Down
120 changes: 68 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Speed of Sound
[![CircleCI](https://circleci.com/gh/lelandjansen/speed-of-sound.svg?style=svg)](https://circleci.com/gh/lelandjansen/speed-of-sound)
[![Coverage Status](https://coveralls.io/repos/github/lelandjansen/speed-of-sound/badge.svg)](https://coveralls.io/github/lelandjansen/speed-of-sound)

**Speed of Sound** is a library to efficiently compute the speed of sound in air. It is intended for use in embedded systems that require a high-precision computation of the speed of sound in air based a variety of ambient conditions.
[![CircleCI][ci-badge]][ci]
[![Coverage Status][coverage-badge]][coverage]

**Speed of Sound** is a library to efficiently compute the speed of sound in
air. It is intended for use in embedded systems that require a high-precision
computation of the speed of sound in air based a variety of ambient conditions.


## Contents
Expand All @@ -16,13 +19,18 @@


## Quick facts
- Computes the speed of sound in air based on temperature, relative humidity, pressure, and carbon dioxide mole fraction.
- Runtime of subsequent speed of sound computations reduced by 67.7% through the use of calculus linear approximations ([reference](https://github.com/lelandjansen/speed-of-sound/blob/master/test/speed_of_sound_test.cc#L312)).
- Approximation maintains precision of at least 0.05% with environmental factors varying up to 20% ([reference](https://github.com/lelandjansen/speed-of-sound/blob/master/test/speed_of_sound_test.cc#L275)).
- Computes the speed of sound in air based on temperature, relative humidity,
pressure, and carbon dioxide mole fraction.
- Runtime of subsequent speed of sound computations reduced by 67.7% through the
use of calculus linear approximations
([reference][linear-approximation-performance-test]).
- Approximation maintains precision of at least 0.05% with environmental
factors varying up to 20% ([reference][linear-approximation-accuracy-test]).


## Environmental parameters
The speed of sound computation takes on the following parameters and is only valid in within the following range.
The speed of sound computation takes on the following parameters and is only
valid in within the following range.

| Variable (units) | Minimum | Default | Maximum |
| -------------------------------------------- | ----------- | -------- | ----------- |
Expand All @@ -31,104 +39,102 @@ The speed of sound computation takes on the following parameters and is only val
| pressure (Pa) | 75000.0 | 101325.0 | 102000.0 |
| Carbon dioxide mole fraction (dimensionless) | 0.0 | 0.000314 | 0.01 |

**Note:** This library is intended for use with embedded systems and therefore does **not** throw an exception if passed an invalid quantity. Instead, methods are provided to validate that values are within the given constraints (see [Usage](#usage)).
**Note:** This library is intended for use with embedded systems and therefore
does **not** throw an exception if passed an invalid quantity. Instead, methods
are provided to validate that values are within the given constraints (see
[Usage](#usage)).


## Usage
```C++
#include "speed_of_sound.h"
```

Create `Environment` object to hold ambient conditions (initializes values to default).
Create `Environment` object to hold ambient conditions (initializes values to
default).
```C++
speedofsound::Environment ambient_conitions;
speedofsound::Environment ambient_conditions;
```

Access `Environment` object members.
Access `Environment` member variables.
```C++
ambient_conitions.temperature_;
ambient_conitions.humidity_;
ambient_conitions.pressure_;
ambient_conitions.co2_mole_fraction_;
ambient_conditions.temperature_;
ambient_conditions.humidity_;
ambient_conditions.pressure_;
ambient_conditions.co2_mole_fraction_;
```

Adjust environment variables.
```C++
ambient_conitions.temperature_ = 25.0;
ambient_conditions.temperature_ = 25.0;
...
```

Check if environmental parameters are within specified constraints (see Constraints for details).
Check if environmental parameters are within specified constraints (see
[Environmental Parameters](#environmental-parameters) for details).
```C++
bool is_valid;

// Validate individual parameters
is_valid = ambient_conitions.ValidateTemperature();
is_valid = ambient_conitions.ValidateHumidity();
is_valid = ambient_conitions.ValidatePressure();
is_valid = ambient_conitions.ValidateCO2MoleFraction();
bool is_valid = ambient_conditions.ValidateTemperature();
is_valid = ambient_conditions.ValidateHumidity();
is_valid = ambient_conditions.ValidatePressure();
is_valid = ambient_conditions.ValidateCO2MoleFraction();

// Validate all parameters
is_valid = ambient_conitions.ValidateEnvironment();
is_valid = ambient_conditions.ValidateEnvironment();
```

Create `SpeedOfSound` object and set initial conditions.
```C++
speedofsound::SpeedOfSound speed_of_sound(ambient_conitions);

speedofsound::SpeedOfSound speed_of_sound(ambient_conditions);
speedofsound::SpeedOfSound speed_of_sound(); // Default Environment values
```
Compute speed of sound.
```C++
double sound_speed;
// Full computation, reset initial conditions
sound_speed = speed_of_sound.Compute(ambient_conitions);
double sound_speed = speed_of_sound.Compute(ambient_conditions);
// Full computation, do not reset initial conditions
sound_speed = speed_of_sound.QuickCompute(ambient_conitions);
sound_speed = speed_of_sound.QuickCompute(ambient_conditions);
// Linear approximation, do not reset initial conditions
sound_speed = speed_of_sound.Approximate(ambient_conitions);
sound_speed = speed_of_sound.Approximate(ambient_conditions);
```


### Example
```C++
#include <iostream>
#include "speed_of_sound.h"

#include "some_sensor.h"
#include "speed_of_sound.h"

int main() {
speedofsound::Environment ambient_conitions;
ambient_conitions.temperature_ = sensor::MeasureTemperature();
auto main() -> int {
speedofsound::Environment ambient_conditions;
ambient_conditions.temperature_ = some_sensor::MeasureTemperature();

// Initial "full" computation
speedofsound::SpeedOfSound speed_of_sound(ambient_conitions);
// Initial "full" computation using ambient conditions
speedofsound::SpeedOfSound speed_of_sound(ambient_conditions);

double sound_speed;
const double distance = 100.0; // meters
const auto distance_meters = 100.0;
while (true) {
ambient_conitions.temperature_ = sensor::MeasureTemperature();
if (!ambient_conitions.ValidateTemperature()) break;

sound_speed = speed_of_sound.Approximate(ambient_conitions);

std::cout << "It would take a sound wave approximately " << \
distance/sound_speed << " seconds to travel " << \
distance << " meters." << std::endl;
ambient_conditions.temperature_ = sensor::MeasureTemperature();
if (!ambient_conditions.ValidateTemperature()) break;
const auto sound_speed = speed_of_sound.Approximate(ambient_conditions);
std::cout << "It would take a sound wave approximately "
<< distance / sound_speed << " seconds to travel "
<< distance_meters << " meters." << std::endl;
}

return 0;
}
```


## Notes on notation
The following abbreviations are used in theory-related computations.

Lower-case variables (`t`, `h`, etc.) represent measured environmental parameters. Variables starting with an uppercase character (`C`, `Psv`, etc.) represent computed values.
Lower-case variables (`t`, `h`, etc.) represent measured environmental
parameters. Variables starting with an uppercase character (`C`, `Psv`, etc.)
represent computed values.

| Variable | Meaning | Units |
| ----------------- | -------------------------------- | ------------------------ |
Expand All @@ -147,14 +153,24 @@ Lower-case variables (`t`, `h`, etc.) represent measured environmental parameter


## Testing
Tests are written using [GoogleTest](https://github.com/google/googletest).
Tests are written using [Google Test][google-test].

Run all tests:
```
$ test/test
```

## Attributions
The equation for computing the speed of sound in air uses Owen Cramer's research.
The equation for computing the speed of sound in air uses Owen Cramer's
research.

*J. Acoust. Soc. Am. 93, 2510 (1993); http://dx.doi.org/10.1121/1.405827*


[ci]: https://circleci.com/gh/lelandjansen/speed-of-sound
[ci-badge]: https://circleci.com/gh/lelandjansen/speed-of-sound.svg?style=svg
[coverage]: https://coveralls.io/github/lelandjansen/speed-of-sound?branch=master
[coverage-badge]: https://coveralls.io/repos/github/lelandjansen/speed-of-sound/badge.svg
[google-test]: https://github.com/google/googletest
[linear-approximation-accuracy-test]: https://github.com/lelandjansen/speed-of-sound/blob/master/test/speed-of-sound_test.cc#L240-L276
[linear-approximation-performance-test]: https://github.com/lelandjansen/speed-of-sound/blob/master/test/speed-of-sound_test.cc#L278-L323

0 comments on commit 972e800

Please sign in to comment.