Skip to content

Commit 719c87e

Browse files
authored
Merge pull request #267 from lasp/feature/add-interpolation-functions
Feature/add interpolation functions
2 parents 4476026 + 282b2c4 commit 719c87e

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
18+
*/
19+
20+
#ifndef BILINEARINTERPOLATION_H
21+
#define BILINEARINTERPOLATION_H
22+
23+
#include <cassert>
24+
25+
/*! This function uses bilinear interpolation to solve for the value of an unknown function of two variables f(x,y)
26+
at the point (x,y).
27+
@return double
28+
@param x1 Data point x1
29+
@param x2 Data point x2
30+
@param y1 Data point y1
31+
@param y2 Data point y2
32+
@param z11 Function value at point (x1, y1)
33+
@param z12 Function value at point (x1, y2)
34+
@param z21 Function value at point (x2, y1)
35+
@param z22 Function value at point (x2, y2)
36+
@param x Function x coordinate for interpolation
37+
@param y Function y coordinate for interpolation
38+
*/
39+
double bilinearInterpolation(double x1,
40+
double x2,
41+
double y1,
42+
double y2,
43+
double z11,
44+
double z12,
45+
double z21,
46+
double z22,
47+
double x,
48+
double y) {
49+
50+
assert(x1 < x && x < x2);
51+
assert(y1 < y && y < y2);
52+
53+
return 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y)
54+
+ z12 * (x2 - x) * (y - y1)
55+
+ z22 * (x - x1) * (y - y1));
56+
}
57+
58+
#endif // BILINEARINTERPOLATION_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
18+
*/
19+
20+
#ifndef LINEARINTERPOLATION_H
21+
#define LINEARINTERPOLATION_H
22+
23+
#include <cassert>
24+
25+
/*! This function uses linear interpolation to solve for the value of an unknown function of a single variables f(x)
26+
at the point x.
27+
@return double
28+
@param x1 Data point x1
29+
@param x2 Data point x2
30+
@param y1 Function value at point x1
31+
@param y2 Function value at point x2
32+
@param x Function x coordinate for interpolation
33+
*/
34+
double linearInterpolation(double x1, double x2, double y1, double y2, double x) {
35+
36+
assert(x1 < x && x < x2);
37+
38+
return y1 * (x2 - x) / (x2 - x1) + y2 * (x - x1) / (x2 - x1);
39+
}
40+
41+
#endif // LINEARINTERPOLATION_H

src/architecture/utilities/tests/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ add_executable(test_signalProcessing test_signalProcessing.cpp)
2626
target_link_libraries(test_signalProcessing GTest::gtest_main)
2727
target_link_libraries(test_signalProcessing ArchitectureUtilities)
2828

29+
add_executable(test_linearInterpolation test_linearInterpolation.cpp)
30+
target_link_libraries(test_linearInterpolation GTest::gtest_main)
31+
target_link_libraries(test_linearInterpolation ArchitectureUtilities)
32+
33+
add_executable(test_bilinearInterpolation test_bilinearInterpolation.cpp)
34+
target_link_libraries(test_bilinearInterpolation GTest::gtest_main)
35+
target_link_libraries(test_bilinearInterpolation ArchitectureUtilities)
36+
2937
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64" AND CMAKE_GENERATOR STREQUAL "Xcode")
3038
set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)
3139
endif()
@@ -37,3 +45,5 @@ gtest_discover_tests(test_saturate)
3745
gtest_discover_tests(test_geodeticConversion)
3846
gtest_discover_tests(test_rigidBodyKinematics)
3947
gtest_discover_tests(test_signalProcessing)
48+
gtest_discover_tests(test_linearInterpolation)
49+
gtest_discover_tests(test_bilinearInterpolation)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
18+
*/
19+
20+
#include "architecture/utilities/bilinearInterpolation.hpp"
21+
#include <gtest/gtest.h>
22+
#include <random>
23+
24+
std::random_device rd;
25+
std::default_random_engine generator(rd());
26+
std::uniform_real_distribution<double> valueDistribution(-10, 10);
27+
std::uniform_real_distribution<double> boundDistribution(0, 2);
28+
29+
TEST(BilinearInterpolationTest, HandlesNormalInputs) {
30+
double x = valueDistribution(generator);
31+
double x1 = x - boundDistribution(generator);
32+
double x2 = x + boundDistribution(generator);
33+
34+
double y = valueDistribution(generator);
35+
double y1 = y - boundDistribution(generator);
36+
double y2 = y + boundDistribution(generator);
37+
38+
double z11 = valueDistribution(generator);
39+
double z12 = valueDistribution(generator);
40+
double z21 = valueDistribution(generator);
41+
double z22 = valueDistribution(generator);
42+
43+
// Bilinearly interpolate to solve for z
44+
double z = 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y)
45+
+ z12 * (x2 - x) * (y - y1)
46+
+ z22 * (x - x1) * (y - y1));
47+
48+
EXPECT_EQ(bilinearInterpolation(x1, x2, y1, y2, z11, z12, z21, z22, x, y), z);
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
18+
*/
19+
20+
#include "architecture/utilities/linearInterpolation.hpp"
21+
#include <gtest/gtest.h>
22+
#include <random>
23+
24+
std::random_device rd;
25+
std::default_random_engine generator(rd());
26+
std::uniform_real_distribution<double> valueDistribution(-10, 10);
27+
std::uniform_real_distribution<double> boundDistribution(0, 2);
28+
29+
TEST(LinearInterpolationTest, HandlesNormalInputs) {
30+
double x = valueDistribution(generator);
31+
double x1 = x - boundDistribution(generator);
32+
double x2 = x + boundDistribution(generator);
33+
34+
double yUnused = valueDistribution(generator);
35+
double y1 = yUnused - boundDistribution(generator);
36+
double y2 = yUnused + boundDistribution(generator);
37+
38+
// Linearly interpolate to solve for y
39+
double y = y1 * (x2 - x) / (x2 - x1) + y2 * (x - x1) / (x2 - x1);
40+
41+
EXPECT_EQ(linearInterpolation(x1, x2, y1, y2, x), y);
42+
}

0 commit comments

Comments
 (0)