# Problem with Smoothing experimental data (Savitzky-Golay & C++)?

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

and the smoothed data is

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;

raw_data.open("data.txt");
smoothed_data.open("smoothed.txt");

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 )
{
x.push_back(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];
++xdata_counter;

}
//four1(&data[0],data.size()/2,isign);
convlv(data, respns, 1, ans);
smoothed_data << ans[0] << std::endl;

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

raw_data.close();
smoothed_data.close();

return 0;
}

• Do you have a link to API documentation of that function? Oct 29, 2013 at 19:25
• @Phonon, thanks for the reply. Which function? convlv() or savgol()? Oct 29, 2013 at 20:44
• 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. Oct 29, 2013 at 20:54