arntanguy / gram_savitzky_golay

C++ Implementation of Savitzky-Golay filtering based on Gram polynomials
BSD 2-Clause "Simplified" License
102 stars 29 forks source link

following example, doesn't work #10

Closed journeytosilius closed 4 months ago

journeytosilius commented 3 years ago

Hi, thank you for your work. This is a really interesting project since there are not savgol implementations for C++ that don't require a ton of dependencies. Unfortunately, I am testing smoothing a vector of length 300 ( so m is set to 151 ) and I get this error when I run it :

signal: /home/xavier/cpp_test/lib/gram_savitzky_golay/include/gram_savitzky_golay/gram_savitzky_golay.h:161: typename ContainerT::value_type gram_sg::SavitzkyGolayFilter::filter(const ContainerT&) const [with ContainerT = std::vector<float>; typename ContainerT::value_type = float]: Assertion `v.size() == weights_.size() && v.size() > 0' failed.
Aborted (core dumped)

This is my code

    static void savgol(vector<double> &result)
    {

        cout << rsi_result.data() << endl;

        // Window size is 2*m+1

        const size_t m = 151;
        // Polynomial Order
        const size_t n = 6;
        // Initial Point Smoothing (ie evaluate polynomial at first point in the window)
        // Points are defined in range [-m;m]
        const size_t t = m;
        // Derivation order? 0: no derivation, 1: first derivative, 2: second derivative...
        const int d = 0;

        // Real-time filter (filtering at latest data point)
        gram_sg::SavitzkyGolayFilter filter(m, t, n, d);
        // Filter some data
        double result = filter.filter(result);

Additionally, I have realized in your example that this implementation only returns the last datapoint, I am right ? Is there a way to get the entire vector smoothed from the start of the window ?

Is there something I am missing ? Thank you

An-u-rag commented 5 months ago

Hey, did you figure this out? I am struggling with the same problem. What does the result even mean? What can I do to get the entire smoothed vector? Do I need to run this in a loop for each point and a window of 2*m+1 around it?

journeytosilius commented 5 months ago

hi, I have completely forgotten. I believe I ended up using another implementation instead of this one

arntanguy commented 4 months ago

Sorry for never getting back to you. This is because your convolution size and buffer size do not match. If m = 151 one expects a vector of size 2*m+1 = 303. Unfortunately as-is the library only supports vector of odd size, if I remember well this was a limitation of the original paper from which this implementation is derived. There was an assertion in the code to display an error, but this would only show up in debug. I changed it to an exception and added a unit test for this particular case.