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 5cd3d2a commit 592e285
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 166 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
@@ -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 .gitignore
@@ -1,5 +1,5 @@
build*/
*.idea
*.swp
*.swo
build*/
.DS_Store

4 changes: 2 additions & 2 deletions CMakeLists.txt
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
69 changes: 41 additions & 28 deletions README.md
@@ -1,8 +1,10 @@
# 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)
[![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.
**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 +18,17 @@


## 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.
- Approximation maintains precision of at least 0.05% with environmental factors
varying up to 20%.


## 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,15 +37,19 @@ 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_conditions;
```
Expand All @@ -58,12 +68,11 @@ 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
Constraints for details).
```C++
bool is_valid;

// Validate individual parameters
is_valid = ambient_conditions.ValidateTemperature();
bool is_valid = ambient_conditions.ValidateTemperature();
is_valid = ambient_conditions.ValidateHumidity();
is_valid = ambient_conditions.ValidatePressure();
is_valid = ambient_conditions.ValidateCO2MoleFraction();
Expand All @@ -81,10 +90,8 @@ 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_conditions);
double sound_speed = speed_of_sound.Compute(ambient_conditions);
// Full computation, do not reset initial conditions
sound_speed = speed_of_sound.QuickCompute(ambient_conditions);
Expand All @@ -97,8 +104,8 @@ 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_conditions;
Expand All @@ -107,28 +114,25 @@ int main() {
// Initial "full" computation
speedofsound::SpeedOfSound speed_of_sound(ambient_conditions);

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

sound_speed = speed_of_sound.Approximate(ambient_conditions);

std::cout << "It would take a sound wave approximately " << \
distance/sound_speed << " seconds to travel " << \
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." << 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,7 +151,7 @@ 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:
```
Expand All @@ -158,3 +162,12 @@ $ test/test
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*

[approximation-performance-test]:
[approximation-accuracy-test]:
[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?branch=master
[google-test]: https://github.com/google/googletest

4 changes: 1 addition & 3 deletions check-code-style
@@ -1,9 +1,8 @@
#!/usr/bin/python3

import subprocess

output = subprocess.check_output(['git', 'clang-format-5.0',
'--binary', 'clang-format-5.0', '--commit', 'HEAD~1', '--diff'])

if output.strip().lower() not in [b'no modified files to format',
b'clang-format did not modify any files']:
print('Code style does not match clang-format')
Expand All @@ -12,4 +11,3 @@ if output.strip().lower() not in [b'no modified files to format',
else:
print(output.strip().decode('utf-8'))
exit(0)

0 comments on commit 592e285

Please sign in to comment.