./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
5 / 5
100.0%
Branches:
3 / 8
37.5%
Functions:
-
-%

include/terraingen/analysis/spectrum.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Power-law fits to the radially averaged power spectrum of a raster's heights.
5 #pragma once
6
7 #include "terraingen/raster/raster.h"
8
9 namespace terraingen {
10
11 /// The wavelengths, in pixels of the base, over which its spectrum is fitted. The defaults leave
12 /// out what a DEM rarely holds whole: wavelengths under 4 pixels and over a quarter window.
13 2 struct SpectrumFitBand {
14 double minPixels = 4.0; ///< Shortest wavelength fitted, in pixels; at least 2.
15 double maxPixels = 0.0; ///< Longest wavelength fitted, in pixels; 0 for a quarter window.
16
17 2 bool operator==(const SpectrumFitBand& other) const {
18
2/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 return minPixels == other.minPixels && maxPixels == other.maxPixels;
19 }
20 1 bool operator!=(const SpectrumFitBand& other) const {
21
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 return !(*this == other);
22 }
23 };
24
25 /// Checks a fit band.
26 /// @throws std::invalid_argument for a `minPixels` under 2 or not finite, or a `maxPixels` that is
27 /// neither 0 nor above `minPixels`.
28 void ValidateSpectrumFitBand(const SpectrumFitBand& band);
29
30 /// A power-law spectrum P(f) = C f^-beta, by its slope and the RMS height it holds in the octave
31 /// of wavelengths just finer than the base's Nyquist wavelength.
32 struct PowerLawSpectrum {
33 /// Spectral slope beta of the radially averaged PSD, in [2, 4]: beta = 2 H + 2 for a
34 /// self-affine surface of Hurst exponent H (Rosenburg et al. 2011).
35 double slope = 3.0;
36 /// RMS height in meters the power law holds at wavelengths from one to two base pixels.
37 double octaveRms = 0.0;
38 /// Whether the fitted slope fell outside [2, 4] and `slope` holds the nearer limit.
39 bool clipped = false;
40
41 /// The coefficient C in m^(4 - beta) for a base whose Nyquist frequency is `nyquist` cycles per
42 /// meter.
43 double Coefficient(double nyquist) const;
44 };
45
46 /// The power law fitted by least squares to the radially averaged spectrum of `heights` over
47 /// `band`, its slope clamped to [2, 4] (setting `clipped`); a raster without relief gives a slope
48 /// of 3 and an RMS of 0.
49 /// @throws std::invalid_argument for a rotated raster, a window under 8 cells a side, one with no
50 /// data, an invalid `band`, or a band holding fewer than 3 bins.
51 PowerLawSpectrum EstimatePowerLawSpectrum(const Raster<float>& heights,
52 const SpectrumFitBand& band = {});
53
54 } // namespace terraingen
55