I'm trying to use Savitzky-Golay Filter to smooth experimental data. Savitzky-Golay computes $c_{n}$ in the following equation $$ g_{i} = \displaystyle\sum\limits_{n=-n_{L}}^{n_{R}} c_{n}f_{i}+n$$

According to Numerical Recipes book by William H. Press. I've bought the library. The next step to calculate $g_{0}$ which represents the smoothed data. To fulfil that, we need to convolute $c_{n}$ with $f_{i}+n$. Luckily, there is a function to do so namely "convlv()". I'm passing 17 points of the stream data ( nl = 7, nr = 7, m = 4 ). To compute $c_{n}$, I'm using "savgol()". The sample rate of the experimental data is 1KHz ( 0.001 second ). After smoothing the data, the sample rate decreased to 100Hz WHY?. The unsmoothed data is

enter image description here and the smoothed data is

enter image description here

This is my code

    #include <iostream>
#include <fstream>
#include <vector>
#include "nr3.h"
#include "ludcmp.h"
#include "savgol.h"
#include "fourier.h"
#include "convlv.h"

int main(void)
    std::ifstream raw_data;
    std::ofstream smoothed_data;
    std::vector<double> x;


    VecDoub_IO ans(17);
    VecDoub_IO data(17);
    VecDoub_IO respns(17);

    const Int np(17), nl(8), nr(8), ld(0), m(4);
     savgol(respns, np, nl, nr, ld,  m);
    for (int i = 0; i < 17; ++i)
    std::cout << respns[i] << " ";

    if (raw_data.fail())
        std::cout << "Error in open xdata file";
    if (smoothed_data.fail())
        std::cout << "Error in Opening xdot file";
    double line(0);
    while ( raw_data >> line )

    int xdata_counter(0);

    // void convlv(VecDoub_I &data, VecDoub_I &respns, const Int isign, VecDoub_O &ans) 

    while ( xdata_counter < (x.size() - 25) )
        for (int i = 8; i <= 17; ++i)
            data[i] = x[xdata_counter];

        convlv(data, respns, 1, ans);
        smoothed_data << ans[0] << std::endl;

        for (int i = 0; i < 8; ++i)
            data[i] = data[i+1];


    return 0;
  • $\begingroup$ Do you have a link to API documentation of that function? $\endgroup$
    – Phonon
    Oct 29, 2013 at 19:25
  • $\begingroup$ @Phonon, thanks for the reply. Which function? convlv() or savgol()? $\endgroup$
    – CroCo
    Oct 29, 2013 at 20:44
  • $\begingroup$ Well, whichever one unintentionally downsamples your signal. Also, adding some comments to your code would help, and removing irrelevant parts of it, too. It's not very easy to parse. $\endgroup$
    – Phonon
    Oct 29, 2013 at 20:54


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.