Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #28 from spectre-team/feature/wavelet-denoising-in…
Browse files Browse the repository at this point in the history
…tegration

Wavelet denoising integration
  • Loading branch information
Sand3r- authored Feb 28, 2018
2 parents 8e6282c + 19db495 commit c2a5ffe
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 1 deletion.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ environment:
configuration:
- Debug
- Release
version: 3.13.0.{build}
version: 3.14.0.{build}

init:
- cmd: echo Project - %APPVEYOR_PROJECT_NAME%
Expand Down
79 changes: 79 additions & 0 deletions src/Spectre.libWavelet.Tests/DaubechiesFiltersDenoiserTest.cpp
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\Common\Main.cpp" />
<ClCompile Include="DaubechiesFiltersDenoiserTest.cpp" />
<ClCompile Include="MedianAbsoluteDeviationNoiseEstimatorTest.cpp" />
<ClCompile Include="ConvolutionTest.cpp" />
<ClCompile Include="SoftThresholderTest.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ClCompile Include="MedianAbsoluteDeviationNoiseEstimatorTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DaubechiesFiltersDenoiserTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
72 changes: 72 additions & 0 deletions src/Spectre.libWavelet/DaubechiesFiltersDenoiser.cpp
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;
}
}
42 changes: 42 additions & 0 deletions src/Spectre.libWavelet/DaubechiesFiltersDenoiser.h
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;
};
}
2 changes: 2 additions & 0 deletions src/Spectre.libWavelet/Spectre.libWavelet.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<ClInclude Include="AlgorithmConstants.h" />
<ClInclude Include="Convolution.h" />
<ClInclude Include="DataTypes.h" />
<ClInclude Include="DaubechiesFiltersDenoiser.h" />
<ClInclude Include="MedianAbsoluteDeviationNoiseEstimator.h" />
<ClInclude Include="PrecomputedDaubechiesCoefficients.h" />
<ClInclude Include="SoftThresholder.h" />
Expand All @@ -139,6 +140,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="Convolution.cpp" />
<ClCompile Include="DaubechiesFiltersDenoiser.cpp" />
<ClCompile Include="MedianAbsoluteDeviationNoiseEstimator.cpp" />
<ClCompile Include="SoftThresholder.cpp" />
<ClCompile Include="WaveletDecomposerRef.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions src/Spectre.libWavelet/Spectre.libWavelet.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClInclude Include="MedianAbsoluteDeviationNoiseEstimator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DaubechiesFiltersDenoiser.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SoftThresholder.cpp">
Expand All @@ -65,6 +68,9 @@
<ClCompile Include="MedianAbsoluteDeviationNoiseEstimator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DaubechiesFiltersDenoiser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit c2a5ffe

Please sign in to comment.