SpikeSpectrum.cpp
Go to the documentation of this file.00001 #include "CMtuFormat.h"
00002 #include "CSpikeStats.h"
00003 #include "CTsSpectrum.h"
00004 #include "CDespike.h"
00005 #include <iostream>
00006 #include <fstream>
00007 #include <string>
00008 #include <sstream>
00009 #include <fftw3.h>
00010 using namespace std;
00011
00012 const int iterations =20;
00013
00014 CMtuFormat TsData;
00015 ofstream diag;
00016 double samplerate;
00017 const int length = 100;
00018 const int trailratio = 4;
00019
00020 void Filter(const tcompdata &Filter, ttsdata &TimeSeries)
00021 {
00022 double *timedomain;
00023 fftw_complex *freqdomain;
00024 fftw_plan pforward, pinverse;
00025 int seglength;
00026 int nsegs;
00027 double real, imag, mean, coeff;
00028
00029 seglength = Filter.size() * 2;
00030 nsegs = TimeSeries.size()/seglength;
00031 timedomain = (double *)fftw_malloc(sizeof(double) * seglength);
00032 freqdomain = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * (seglength/2+1));
00033 pforward = fftw_plan_dft_r2c_1d(seglength,timedomain,freqdomain,FFTW_MEASURE);
00034 pinverse = fftw_plan_dft_c2r_1d(seglength,freqdomain,timedomain,FFTW_MEASURE);
00035 for (int i = 0; i < nsegs-1; ++i)
00036 {
00037 mean = 0;
00038 for (int j = 0; j < seglength; ++j)
00039 {
00040 timedomain[j] = TimeSeries.at(i*seglength +j);
00041 mean += timedomain[j];
00042 }
00043 for (int j = 0; j < seglength; ++j)
00044 {
00045 timedomain[j] -=mean/seglength;
00046 timedomain[j] *= 0.54 - 0.46 * cos(2 * PI *j /seglength);
00047 }
00048 fftw_execute(pforward);
00049 for (int j = 0; j < seglength/2; j++)
00050 {
00051 real = freqdomain[j][0];
00052 imag = freqdomain[j][1];
00053 coeff = (pow(real,2) + pow(imag,2) - norm(Filter.at(j)))/(pow(real,2) + pow(imag,2));
00054
00055
00056
00057 if (coeff < 0)
00058 coeff = 0.0001;
00059 freqdomain[j][0] = real * coeff;
00060 freqdomain[j][1] = imag * coeff;
00061 if (i == 1)
00062 diag << j << " " << coeff << " " << imag << " " << real << " " << Filter.at(j) << endl;
00063 }
00064 fftw_execute(pinverse);
00065 for (int j = 0; j < seglength; ++j)
00066 {
00067 timedomain[j] /= seglength;
00068 timedomain[j] /= 0.54 - 0.46 * cos(2 * PI *j /seglength);
00069 timedomain[j] += mean/seglength;
00070 TimeSeries.at(i*seglength +j)= timedomain[j];
00071 }
00072 }
00073 }
00074
00075 void WriteSpec(string name, const CTsSpectrum &Spectrum)
00076 {
00077 ofstream spikefreq;
00078 spikefreq.open(name.c_str());
00079 int i = 0;
00080 for (tcompdata::iterator it = Spectrum.FreqData->begin(); it != Spectrum.FreqData->end(); ++it)
00081 {
00082 spikefreq.precision(10);
00083 spikefreq << ++i << " " << (*it).real() << " " << (*it).imag() << endl;
00084 }
00085 spikefreq.close();
00086 }
00087
00088 void WritePower(string name, const CTsSpectrum &Spectrum)
00089 {
00090 ofstream spikefreq;
00091 spikefreq.open(name.c_str());
00092 int i = -1;
00093 double freqstep = 1./(samplerate * length);
00094 for (tcompdata::iterator it = Spectrum.FreqData->begin(); it != Spectrum.FreqData->end(); ++it)
00095 {
00096 spikefreq.precision(10);
00097 spikefreq << (++i)*freqstep << " " << norm(*it) << endl;
00098 }
00099 spikefreq.close();
00100 }
00101
00102 void WriteSpike(ttsdata &SpikeForm, string name)
00103 {
00104 ofstream spike;
00105 spike.open(name.c_str());
00106 int i = 0;
00107 for (ttsdata::iterator it = SpikeForm.begin(); it != SpikeForm.end(); ++it)
00108 {
00109 spike.precision(10);
00110 spike << ++i << " " << *it << endl;
00111 }
00112 spike.close();
00113 }
00114
00115 void ApplyFilter(ttsdata &Component, string name, ttsdata &Time)
00116 {
00117 double mean = 0;
00118 CSpikeStats SpikeStats;
00119 CDespike Despiker;
00120 CTsSpectrum SpikeSpec;
00121
00122 SpikeStats.Time = &(Time);
00123 SpikeStats.nValueBins = 1000;
00124 SpikeStats.nDerivBins = 1000;
00125 SpikeStats.HeightFraction = 0.01;
00126 SpikeStats.samplerate = samplerate;
00127 Despiker.Time = &(Time);
00128
00129
00130 Despiker.SpikeLength = length;
00131 Despiker.TrailPoints = length/trailratio;
00132 Despiker.samplerate = samplerate;
00133 SpikeSpec.TsData = &Despiker.SpikeForm;
00134 SpikeSpec.MultiCalc = false;
00135 SpikeSpec.FreqData = new tcompdata;
00136 SpikeStats.Data = &Component;
00137 Despiker.Data = &Component;
00138 SpikeStats.AnalyseSpikes();
00139 Despiker.HeightThreshold = SpikeStats.HeightThreshold;
00140 Despiker.StackSpikes();
00141
00142
00143
00144 for (int i = 0; i < Despiker.SpikeForm.size(); ++i)
00145 {
00146
00147 Despiker.SpikeForm.at(i) *= 0.54 - 0.46 * cos(2 * PI *i
00148 /Despiker.SpikeForm.size());
00149 }
00150 SpikeSpec.CalcSpectrum();
00151
00152
00153
00154
00155 Filter(*SpikeSpec.FreqData,Component);
00156 }
00157
00158 int main()
00159 {
00160 string infilename,temp;
00161 ostringstream converter;
00162
00163 double filterfraction, currentfraction;
00164
00165 do
00166 {
00167 cout << "Inputfile: ";
00168 cin >> infilename;
00169 }
00170 while (TsData.GetData(infilename.c_str()));
00171
00172 diag.open((infilename+".diag").c_str());
00173 samplerate = TsData.samplerate;
00174 for (int i = 0; i < iterations; ++i)
00175 {
00176 converter << i;
00177 temp = infilename + converter.str() + "ey";
00178 ApplyFilter(TsData.Ey.data,temp,TsData.t);
00179 temp = infilename + converter.str() + "ex";
00180 ApplyFilter(TsData.Ex.data,temp,TsData.t);
00181
00182
00183
00184
00185
00186 }
00187 TsData.WriteData(("opt"+infilename).c_str());
00188 diag.close();
00189 }