Speed of Sound library

Taking distance measurements with an ultrasonic sensor requires one to multiply the ultrasonic wave's return time with the speed of sound in air (d=v*t). I created a Speed of Sound library written in C++ to provide a method for approximating the speed of sound in air based on a variety of environmental factors. It is intended to be used by low-power microprocessors where computing the speed of sound in air at a high frequency is too computationally expensive.

Quick Facts

  • Computes the speed of sound in air based on temperature, humidity, pressure, and carbon dioxide mole fraction.
  • Speed of sound algorithm runtime 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%.

This library builds on the work I did in a previous post in hopes that the method will become more accessible to other developers. The algorithm is based on Owen Cramer's research:

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


Test are written using Google's C++ GoogleTest framework. To ensure the methods are correct (especially the partial derivatives) each equation was determined using two different methods. The equation was first computed by hand and computed a second time using Mathematica. To verify that the equations arrive at the same value, constants were plugged in to each version of the function. If both evaluated to the same number, it is reasonable to assume that the functions are correct. Otherwise, a mistake was made computing one (or possibly both) of the equations.  It's important to note that the methods were determined (and corrected) independently without cross-contamination. In other words, Mathematica's result was not directly compared to the result determined by hand and vice-versa. This ensures that mistakes in one equation do not carry over to the other. Tests were also written to verify the functionality of the Speed of Sound class and assert the accuracy claims made above.

Coldplay's album artwork for their Speed of Sound single.