Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of 1 Dimensional Filtering Using Median Filter #343

Closed
TheCedarPrince opened this issue Feb 15, 2020 · 3 comments
Closed

Comments

@TheCedarPrince
Copy link
Contributor

System Information:

OS: Fedora 30
Julia Version: 1.3.0

Issue Information

Hi all,

I created a very basic implementation of a median filter to filter out 1 Dimensional signal noise; here is the function:

function medfilt(;input_range::Array, input_signal::Array, window::Int64)

    median_signal::Array{Float64} = []
    for i in 1:length(input_range)
        if length(input_range) - (window + i - 1) < 0
            forward = input_signal[i:end]
            wrap = input_signal[1:abs(length(input_signal) - (window + i - 1))]
            append!(median_signal, median(vcat(forward, wrap)))
        else
            forward = input_signal[i:(window + i - 1)]
            append!(median_signal, median(forward))
        end

    end
    return median_signal
end

and here is a very basic implementation of it:

using Statistics
using Plots
gr()

function medfilt(;input_range::Array, input_signal::Array, window::Int64)

    median_signal::Array{Float64} = []
    for i in 1:length(input_range)
        if length(input_range) - (window + i - 1) < 0
            forward = input_signal[i:end]
            wrap = input_signal[1:abs(length(input_signal) - (window + i - 1))]
            append!(median_signal, median(vcat(forward, wrap)))
        else
            forward = input_signal[i:(window + i - 1)]
            append!(median_signal, median(forward))
        end

    end
    return median_signal
end

    # CHOOSE WINDOW AND INPUT VALUES OVER WHICH TO CALCULATE
    input = collect(0:0.001:1)
    window = 30

    # CREATING NOISY SIGNAL RANDOM NOISE TO FUNCTION
    noisy_signal = [sin(2 * pi * i) + rand([-1, 1]) * round(rand(), digits = 2)
                    for i in input]

    # GENERATING GENERIC FUNCTION
    signal = [sin(2 * pi * i) for i in input]
    filtered_signal = medfilt(input_range = input, input_signal = noisy_signal, window = window)

    # PLOTTING RESULTS
    plot(input, noisy_signal, label = "Noisy Signal", title = "Example of Median Filter")
    plot!(input, filtered_signal, label="Median Filtered Signal", linewidth = 3)
    plot!(input, signal, label = "sin(2pi)", linewidth = 5)

Here is an example of what the output should look like:

image

I would love to contribute this to the package - what all needs to be done to add it?

@baggepinnen
Copy link
Contributor

This functionality exists in many places if the ecosystem, e.g. RollingFunctions.jl, LocalFilters and imagefiltering.jl. The implementations in those packages are more general, and can map any function over moving windows and over N-dimensional windows in the case of imagefiltering.jl. Does you implementation have something to offer that can not be found in those other packages?

@TheCedarPrince
Copy link
Contributor Author

Hey @baggepinnen - thanks for the response! I was not aware of those other packages.

I do not have anything additional to offer over packages. We can go ahead and close this.

Yet, a concern I have is those other repositories do not seem to be regularly maintained or updated. I do not see a need to have to import additional smaller packages when it seems like DSP.jl is the one that is most up to date and ready to go.

It almost makes more sense to me to merge RollingFunctions.jl or LocalFilters.jl with DSP.jl.

What are your thoughts? Thanks!

~TheCedarPrince

@baggepinnen
Copy link
Contributor

Yet, a concern I have is those other repositories do not seem to be regularly maintained or updated.

What makes you say this? They all seem to have great test coverage and pass their own tests. Perhaps no recent updates indicates that they are mature and well functioning?

ImageFiltering is also seeing quite some activity
https://github.com/JuliaImages/ImageFiltering.jl/pulse/monthly

In general, since this is self-contained functionality, there is no need to merge it into a larger package. People who only need this filtering will not be happier if they need to load all functionality in DSP to access it. If all you want is is the convenience of loading a single package only, you may create a super-package that reexports both DSP and one of the filtering packages and use that as an import, but I do not think you will find it worthwhile.

If you do want to contribute to this part of the ecocsystem, you might want to improve upon those existing implementations, e.g., by increasing performance etc. One issue with ImageFiltering.mapwindow is that people can't find it :/ JuliaImages/ImageFiltering.jl#42 Maybe you have a suggestion on improvements on this aspect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants