This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #28 from spectre-team/feature/wavelet-denoising-in…
…tegration Wavelet denoising integration
- Loading branch information
Showing
8 changed files
with
206 additions
and
1 deletion.
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
79 changes: 79 additions & 0 deletions
79
src/Spectre.libWavelet.Tests/DaubechiesFiltersDenoiserTest.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,79 @@ | ||
/* | ||
* DaubechiesFiltersDenoiserTest.cpp | ||
* Tests wavelet denoising using Daubechies filters. | ||
* | ||
Copyright 2018 Michal Gallus | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include "FloatingPointVectorMatcher.h" | ||
#include "Spectre.libWavelet\DaubechiesFiltersDenoiser.h" | ||
#include "Spectre.libWavelet\WaveletDecomposerRef.h" | ||
#include "Spectre.libWavelet\WaveletReconstructorRef.h" | ||
|
||
namespace | ||
{ | ||
using namespace spectre::algorithm::wavelet; | ||
|
||
TEST(DaubechiesFiltersDenoiserInitialization, initializes) | ||
{ | ||
EXPECT_NO_THROW(DaubechiesFiltersDenoiser()); | ||
} | ||
|
||
class DaubechiesFiltersDenoiserTest : public ::testing::Test | ||
{ | ||
public: | ||
DaubechiesFiltersDenoiserTest() | ||
{ | ||
signal = { | ||
0.0, | ||
1.0, | ||
0.2, | ||
0.0, | ||
3.0, | ||
6.0, | ||
0.3, | ||
7.0, | ||
0.1, | ||
0.2, | ||
0.1 | ||
}; | ||
correctResult = { | ||
-0.1322, | ||
-0.0672, | ||
-0.0567, | ||
-0.0068, | ||
0.6509, | ||
1.2891, | ||
-1.0844, | ||
1.7828, | ||
-0.1743, | ||
0.0527, | ||
-0.1567 | ||
}; | ||
} | ||
protected: | ||
Signal signal; | ||
Signal correctResult; | ||
DaubechiesFiltersDenoiser denoiser; | ||
}; | ||
|
||
TEST_F(DaubechiesFiltersDenoiserTest, denoises_signal) | ||
{ | ||
Signal denoisedSignal = denoiser.Denoise(signal); | ||
auto doubleNear = double_near(0.001); | ||
EXPECT_THAT(correctResult, testing::Pointwise(doubleNear, denoisedSignal)); | ||
} | ||
} |
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
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,72 @@ | ||
/* | ||
* DaubechiesFiltersDenoiser.cpp | ||
* Denoises the signal using undecimated wavelet transform. | ||
* | ||
Copyright 2018 Michal Gallus | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#include "DaubechiesFiltersDenoiser.h" | ||
#include "SoftThresholder.h" | ||
|
||
namespace spectre::algorithm::wavelet | ||
{ | ||
DaubechiesFiltersDenoiser::DaubechiesFiltersDenoiser() | ||
{ | ||
} | ||
|
||
static inline void ExtractCoefficientsForNoiseEstimation(Signal& highFrequencyCoeffs, | ||
WaveletCoefficients& coefficients) | ||
{ | ||
CoefficientList& noiseEstimationCoefficients = coefficients.data[0][0]; | ||
auto chunkStart = noiseEstimationCoefficients.begin(); | ||
auto chunkEnd = chunkStart + highFrequencyCoeffs.size(); | ||
highFrequencyCoeffs.assign(chunkStart, chunkEnd); | ||
} | ||
|
||
static inline WaveletCoefficients TresholdSignal(const NoiseEstimator& noiseEstimator, | ||
WaveletCoefficients& coefficients, size_t signalLength) | ||
{ | ||
Signal highFreqCoefficients(signalLength); | ||
ExtractCoefficientsForNoiseEstimation(highFreqCoefficients, coefficients); | ||
DataType noiseTreshold = noiseEstimator.Estimate(highFreqCoefficients); | ||
SoftThresholder tresholder(noiseTreshold); | ||
return tresholder(std::move(coefficients)); | ||
} | ||
|
||
static inline WaveletCoefficients Decompose(const WaveletDecomposerRef& decomposer, | ||
Signal& signal) | ||
{ | ||
return decomposer.Decompose(std::move(signal)); | ||
} | ||
|
||
static inline Signal Reconstruct(const WaveletReconstructorRef& reconstructor, | ||
WaveletCoefficients& coefficients, size_t signalLength) | ||
{ | ||
return reconstructor.Reconstruct(std::move(coefficients), signalLength); | ||
} | ||
|
||
Signal DaubechiesFiltersDenoiser::Denoise(Signal& signal) const | ||
{ | ||
const size_t signalLength = signal.size(); | ||
|
||
if (signalLength < 2) | ||
return signal; | ||
|
||
WaveletCoefficients coefficients = Decompose(m_Decomposer, signal); | ||
coefficients = TresholdSignal(m_NoiseEstimator, coefficients, signalLength); | ||
Signal denoisedSignal = Reconstruct(m_Reconstructor, coefficients, signalLength); | ||
|
||
return denoisedSignal; | ||
} | ||
} |
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 @@ | ||
/* | ||
* DaubechiesFiltersDenoiser.h | ||
* Denoises the signal using undecimated wavelet transform. | ||
* | ||
Copyright 2018 Michal Gallus | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#pragma once | ||
#include <memory> | ||
#include "DataTypes.h" | ||
#include "Spectre.libWavelet/MedianAbsoluteDeviationNoiseEstimator.h" | ||
#include "Spectre.libWavelet/WaveletDecomposerRef.h" | ||
#include "Spectre.libWavelet/WaveletReconstructorRef.h" | ||
|
||
namespace spectre::algorithm::wavelet | ||
{ | ||
using NoiseEstimator = MedianAbsoluteDeviationNoiseEstimator; | ||
|
||
class DaubechiesFiltersDenoiser | ||
{ | ||
public: | ||
explicit DaubechiesFiltersDenoiser(); | ||
Signal Denoise(Signal& signal) const; | ||
private: | ||
static constexpr unsigned m_Base = 4; | ||
static constexpr unsigned m_LevelsOfDecomposition = 10; | ||
const NoiseEstimator m_NoiseEstimator; | ||
const WaveletDecomposerRef m_Decomposer; | ||
const WaveletReconstructorRef m_Reconstructor; | ||
}; | ||
} |
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