weegreenblobbie / median_filter_benchmark

A program that times various techniques for performing a moving median filter (sometimes called rolling median, or streaming median)
GNU General Public License v3.0
10 stars 3 forks source link

The three algorithm listed could not get the true median value. #1

Open sunbingfengPI opened 5 years ago

sunbingfengPI commented 5 years ago

Hello,

I tried to test your algorithm listed using random data, but could not get the true median value after filtering.

So I am not sure if something goes wrong.

Thanks!

weegreenblobbie commented 5 years ago

Can you reproduce it and post example data?

sunbingfengPI commented 5 years ago

Hi,

  1. sample code:
// usage: ./main [window_size]
// example: ./main 100
int main(int argc, char ** argv)
{
    int v_size = atoi(argv[1]);

    using Type = int;
    auto input = get_random_vector<Type>(v_size);
    std::cout << "input";
    for (std::vector<Type>::iterator i = input.begin(); i != input.end(); ++i)
    {
        std::cout << ", " << *i;
    }
    std::cout << std::endl;

    auto sorted = input;
    std::sort(sorted.begin(), sorted.end(), std::greater<Type>());
    std::cout << "sorted";
    for (std::vector<Type>::iterator i = sorted.begin(); i != sorted.end(); ++i)
    {
        std::cout << ", " << *i;
    }
    std::cout << std::endl;

    auto fptr = std::make_shared<LowerBoundVector<Type>>(v_size);

    auto output = fptr->filter(input);
    std::cout << "output";
    for (std::vector<Type>::iterator i = output.begin(); i != output.end(); ++i)
    {
        std::cout << ", " << *i;
    }
    std::cout << std::endl;

    return 0;
}
  1. compile with g++:
g++ -o main main.cpp -std=c++11
  1. results:
Bills-MacBook-Pro:median_filter_benchmark bill$ ./main 5
input, -151605594, -1949823204, -1411098632, 1303974802, -1244110644
sorted, 1303974802, -151605594, -1244110644, -1411098632, -1949823204
output, -151605594, -151605594, -151605594, -151605594, -151605594
Bills-MacBook-Pro:median_filter_benchmark bill$ ./main 7
input, -151605594, -1949823204, -1411098632, 1303974802, -1244110644, 282161878, -416595935
sorted, 1303974802, 282161878, -151605594, -416595935, -1244110644, -1411098632, -1949823204
output, -151605594, -151605594, -151605594, -151605594, -151605594, -151605594, -151605594
Bills-MacBook-Pro:median_filter_benchmark bill$

I guess that the last value in filter output vector may be the true median, but it's not.

For your reference!