-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #719 from AVSLab/feature/add-interpolation-functions
Feature/add interpolation functions
- Loading branch information
Showing
6 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
ISC License | ||
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#ifndef BILINEARINTERPOLATION_H | ||
#define BILINEARINTERPOLATION_H | ||
|
||
#include <cassert> | ||
|
||
/*! This function uses bilinear interpolation to solve for the value of an unknown function of two variables f(x,y) | ||
at the point (x,y). | ||
@return double | ||
@param x1 Data point x1 | ||
@param x2 Data point x2 | ||
@param y1 Data point y1 | ||
@param y2 Data point y2 | ||
@param z11 Function value at point (x1, y1) | ||
@param z12 Function value at point (x1, y2) | ||
@param z21 Function value at point (x2, y1) | ||
@param z22 Function value at point (x2, y2) | ||
@param x Function x coordinate for interpolation | ||
@param y Function y coordinate for interpolation | ||
*/ | ||
double bilinearInterpolation(double x1, | ||
double x2, | ||
double y1, | ||
double y2, | ||
double z11, | ||
double z12, | ||
double z21, | ||
double z22, | ||
double x, | ||
double y) { | ||
|
||
assert(x1 < x && x < x2); | ||
assert(y1 < y && y < y2); | ||
|
||
return 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y) | ||
+ z12 * (x2 - x) * (y - y1) | ||
+ z22 * (x - x1) * (y - y1)); | ||
} | ||
|
||
#endif // BILINEARINTERPOLATION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
ISC License | ||
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#ifndef LINEARINTERPOLATION_H | ||
#define LINEARINTERPOLATION_H | ||
|
||
#include <cassert> | ||
|
||
/*! This function uses linear interpolation to solve for the value of an unknown function of a single variables f(x) | ||
at the point x. | ||
@return double | ||
@param x1 Data point x1 | ||
@param x2 Data point x2 | ||
@param y1 Function value at point x1 | ||
@param y2 Function value at point x2 | ||
@param x Function x coordinate for interpolation | ||
*/ | ||
double linearInterpolation(double x1, double x2, double y1, double y2, double x) { | ||
|
||
assert(x1 < x && x < x2); | ||
|
||
return y1 * (x2 - x) / (x2 - x1) + y2 * (x - x1) / (x2 - x1); | ||
} | ||
|
||
#endif // LINEARINTERPOLATION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/architecture/utilities/tests/test_bilinearInterpolation.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
ISC License | ||
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include "architecture/utilities/bilinearInterpolation.hpp" | ||
#include <gtest/gtest.h> | ||
#include <random> | ||
|
||
std::random_device rd; | ||
std::default_random_engine generator(rd()); | ||
std::uniform_real_distribution<double> valueDistribution(-10, 10); | ||
std::uniform_real_distribution<double> boundDistribution(0, 2); | ||
|
||
TEST(BilinearInterpolationTest, HandlesNormalInputs) { | ||
double x = valueDistribution(generator); | ||
double x1 = x - boundDistribution(generator); | ||
double x2 = x + boundDistribution(generator); | ||
|
||
double y = valueDistribution(generator); | ||
double y1 = y - boundDistribution(generator); | ||
double y2 = y + boundDistribution(generator); | ||
|
||
double z11 = valueDistribution(generator); | ||
double z12 = valueDistribution(generator); | ||
double z21 = valueDistribution(generator); | ||
double z22 = valueDistribution(generator); | ||
|
||
// Bilinearly interpolate to solve for z | ||
double z = 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y) | ||
+ z12 * (x2 - x) * (y - y1) | ||
+ z22 * (x - x1) * (y - y1)); | ||
|
||
EXPECT_EQ(bilinearInterpolation(x1, x2, y1, y2, z11, z12, z21, z22, x, y), z); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/architecture/utilities/tests/test_linearInterpolation.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
ISC License | ||
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include "architecture/utilities/linearInterpolation.hpp" | ||
#include <gtest/gtest.h> | ||
#include <random> | ||
|
||
std::random_device rd; | ||
std::default_random_engine generator(rd()); | ||
std::uniform_real_distribution<double> valueDistribution(-10, 10); | ||
std::uniform_real_distribution<double> boundDistribution(0, 2); | ||
|
||
TEST(LinearInterpolationTest, HandlesNormalInputs) { | ||
double x = valueDistribution(generator); | ||
double x1 = x - boundDistribution(generator); | ||
double x2 = x + boundDistribution(generator); | ||
|
||
double yUnused = valueDistribution(generator); | ||
double y1 = yUnused - boundDistribution(generator); | ||
double y2 = yUnused + boundDistribution(generator); | ||
|
||
// Linearly interpolate to solve for y | ||
double y = y1 * (x2 - x) / (x2 - x1) + y2 * (x - x1) / (x2 - x1); | ||
|
||
EXPECT_EQ(linearInterpolation(x1, x2, y1, y2, x), y); | ||
} |