CroCo
07-15-2013, 08:28 AM
I'm trying to use Savitzky-Golay Filter to smooth experimental data. Savitzky-Golay computes c_n in the following equation
http://s21.postimg.org/v4lpdiqk7/Untitled.png
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.
The unsmoothed data is
http://s15.postimg.org/jsf6cckej/Untitled.png
and the smoothed data is
http://s24.postimg.org/o78oyhjrp/smoothed.png
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;
}
Any suggestions?
http://s21.postimg.org/v4lpdiqk7/Untitled.png
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.
The unsmoothed data is
http://s15.postimg.org/jsf6cckej/Untitled.png
and the smoothed data is
http://s24.postimg.org/o78oyhjrp/smoothed.png
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;
}
Any suggestions?