-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,181 additions
and
4 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
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 |
---|---|---|
@@ -1,13 +1,24 @@ | ||
module Postprocessing | ||
|
||
using LinearAlgebra | ||
using FFTW | ||
using OffsetArrays | ||
using ToeplitzMatrices | ||
|
||
include("total_variation.jl") | ||
""" | ||
reference(algorithm) | ||
Returns a citable reference for `algorithm`, e.g. a bibtex entry | ||
for a scientific article or a book. | ||
""" | ||
function reference end | ||
|
||
export total_variation_denoising, total_variation_denoising!, | ||
group_sparse_total_variation_denoising, group_sparse_total_variation_denoising! | ||
include("total_variation.jl") | ||
include("fourier_pade.jl") | ||
|
||
export reference | ||
export total_variation, total_variation_denoising, total_variation_denoising!, | ||
group_sparse_total_variation_denoising, group_sparse_total_variation_denoising! | ||
export fourier_pade | ||
|
||
end # module |
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,66 @@ | ||
""" | ||
fourier_pade(x, u, degree_num, degree_den) | ||
Compute the Fourier-Padé reconstruction of `u` with degrees | ||
`(degree_num, degree_den)` and evaluate it at the points `x`, cf. | ||
Driscoll and Fornberg (2001) A Padé-based algorithm for overcoming the Gibbs phenomenon, | ||
doi: 10.1023/A:1016648530648. | ||
""" | ||
function fourier_pade(x, u, degree_num, degree_den) | ||
N0 = length(u) | ||
if iseven(N0) | ||
throw(ArgumentError("u has $N0 coeffficients but is assumed to have an odd number of coefficients.")) | ||
end | ||
N = degree_num + degree_den | ||
if N0 <= N | ||
throw(ArgumentError("Cannot perform a Fourier-Padé reconstruction with degrees $degree_num and $degree_den given $N0 coefficients.")) | ||
end | ||
|
||
uh = fft(u) / N0 | ||
uhat = uh[1:N+1] | ||
|
||
# denominator | ||
col_den = uhat[degree_num+2:N+1] | ||
row_den = uhat[degree_num+2:-1:max(1, degree_num+2-degree_den)] | ||
if degree_den > degree_num | ||
row_den = vcat(row_den, fill(0, degree_den-degree_num-1)) | ||
end | ||
Z = nullspace(Matrix(Toeplitz(col_den, row_den))) | ||
den_p = Z[:, end] | ||
den_p ./= den_p[findfirst(!iszero, den_p)] | ||
den_m = conj.(den_p) | ||
|
||
# numerator | ||
col_num = uhat[1:degree_num+1] | ||
col_num[1] /= 2 | ||
row_num = zeros(eltype(col_num), degree_den+1) | ||
row_num[1] = col_num[1] | ||
A = Toeplitz(col_num, row_num) | ||
num_p = A * den_p | ||
num_m = conj.(num_p) | ||
|
||
# evaluate Fourier-Padé approximation | ||
T = real(eltype(uhat)) | ||
n = length(x) | ||
xx = range(zero(T), 2*one(T)*π*(n-1)/n, length=n) | ||
x_p = @. exp(im * xx) | ||
x_m = @. exp(-im * xx) | ||
|
||
real.(evalpoly.(x_p, Ref(num_p)) ./ evalpoly.(x_p, Ref(den_p)) .+ evalpoly.(x_m, Ref(num_m)) ./ evalpoly.(x_m, Ref(den_m))) | ||
end | ||
|
||
function reference(::typeof(fourier_pade)) | ||
""" | ||
@article{driscoll2001pade, | ||
title={A {P}ad{\'e}-based algorithm for overcoming the {G}ibbs phenomenon}, | ||
author={Driscoll, Tobin A and Fornberg, Bengt}, | ||
journal={Numerical Algorithms}, | ||
volume={26}, | ||
number={1}, | ||
pages={77--92}, | ||
year={2001}, | ||
publisher={Springer}, | ||
doi={10.1023/A:1016648530648} | ||
} | ||
""" | ||
end |
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,31 @@ | ||
using Postprocessing | ||
using Test | ||
using LinearAlgebra, DelimitedFiles | ||
|
||
@testset "Fourier Padé reconstruction" begin | ||
println(devnull, reference(fourier_pade)) | ||
|
||
for T in (Float32, Float64) | ||
data = readdlm(joinpath(@__DIR__, "test_u_piecewise_constant.txt"), comments=true) | ||
x = T.(data[:, 1]) | ||
u = T.(data[:, 2]) | ||
u_true = @. ifelse(abs(x - 0.6) < 0.2, one(T), zero(T)) | ||
|
||
u_reconstructed = fourier_pade(x, u, 50, 50) | ||
@test norm(u_reconstructed - u_true) < norm(u - u_true) | ||
@test total_variation(u_reconstructed) < total_variation(u) | ||
|
||
u_reconstructed = fourier_pade(x, u, 50, 45) | ||
@test norm(u_reconstructed - u_true) < norm(u - u_true) | ||
@test total_variation(u_reconstructed) < total_variation(u) | ||
|
||
# this choice of the degrees leads to increased oscillations | ||
# near the discontinuity but should still be computable | ||
u_reconstructed = fourier_pade(x, u, 50, 52) | ||
|
||
@test_throws ArgumentError fourier_pade(x, u[1:end-1], 50, 50) | ||
|
||
N = length(u) | ||
@test_throws ArgumentError fourier_pade(x, u[1:end-1], N-(N÷2)+1, N÷2) | ||
end | ||
end |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
@time include("total_variation_test.jl") | ||
@time include("fourier_pade_test.jl") |
Oops, something went wrong.