spectrum_analyzer/error.rs
1/*
2MIT License
3
4Copyright (c) 2023 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24//! Errors related to the spectrum analysis via FFT. Most probably, the errors will
25//! result in wrong input data, before the actual calculation has begun.
26//!
27//! This module focuses on the "overall" errors. More specific errors might be
28//! located in submodules.
29
30use crate::limit::FrequencyLimitError;
31use core::error::Error;
32use core::fmt::{Display, Formatter};
33
34/// Describes main errors of the library. Almost all errors
35/// are caused by wrong input.
36#[derive(Debug)]
37pub enum SpectrumAnalyzerError {
38 /// There must be at least two samples.
39 TooFewSamples,
40 /// NaN values in samples are not supported!
41 NaNValuesNotSupported,
42 /// Infinity-values (regarding floating point representation) in samples are not supported!
43 InfinityValuesNotSupported,
44 /// The frequency is invalid. See [`FrequencyLimitError`].
45 InvalidFrequencyLimit(FrequencyLimitError),
46 /// The number of samples must be a power of two in order for the FFT.
47 SamplesLengthNotAPowerOfTwo,
48 /// After applying the scaling function on a specific item, the returned value is either
49 /// infinity or NaN, according to IEEE-754. This is invalid. Check
50 /// your scaling function!
51 ScalingError(f32 /* orig */, f32 /* new */),
52}
53
54impl Display for SpectrumAnalyzerError {
55 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
56 match self {
57 Self::TooFewSamples => write!(f, "Too few samples!"),
58 Self::NaNValuesNotSupported => {
59 write!(f, "NaN values are not supported!")
60 }
61 Self::InfinityValuesNotSupported => {
62 write!(f, "Infinity values are not supported!")
63 }
64 Self::InvalidFrequencyLimit(e) => {
65 write!(f, "Invalid frequency limit: {e}")
66 }
67 Self::SamplesLengthNotAPowerOfTwo => {
68 write!(f, "Samples length must be a power of two!")
69 }
70 Self::ScalingError(a, b) => write!(f, "Scaling error: {a} -> {b}"),
71 }
72 }
73}
74
75impl Error for SpectrumAnalyzerError {
76 fn source(&self) -> Option<&(dyn Error + 'static)> {
77 match self {
78 Self::InvalidFrequencyLimit(e) => Some(e),
79 _ => None,
80 }
81 }
82}