Dear all,
Recently, I was involved to deal with this job, re-organize the tsv data.
Original data is like this:
First line: time_1.1 signal_1.1 time_2.1 signal_2.1 ...
time_4096.1 signal_4096.1 (total 4096 pairs).
Second line: time_1.2 signal_1.2 time_2.2 signal_2.2 ...
time_4096.2 signal_4096.2(total 4096 pairs).
.......
last line(totally 2048 lines): time_1.2048 signal_1.2048 time_2.2048
signal_2.2048 ... time_4096.2048 signal_4096.2048 (total 4096 pairs).
What shall I do is,
Step 0, all of the time_n.* should subtract to the time_n.1. That is to
say,
time_1.1, time_1.2, ... time_1.2048 should subtract time_1.1.
time_2.1, time_2.2, ... time_2.2048 should subtract time_2.1.
....
time_4096.1, time_4096.2, ... time_4096.2048 should subtract
time_4096.1.
Step 1, make all of the time_k.* and signal_k.* in each line collected
together and save in files, let's say, file_k.tsv .
Namely, all of the time_1.1 , signal_1.1, time_1.2, signal_1.2 ......
time_1.2048, signal_1.2048 should save in file_1.tsv. And the first line
is time_1.1 signal_1.1; the second line is time_1.2, signal_1.2 ......
the last line is time_1.2048, signal_1.2048.
All of the time_2.1 , signal_2.1, time_2.2, signal_2.2 ......
time_2.2048, signal_1.2048 should save in file_2.tsv. And the first line
is time_2.1 signal_2.1; the second line is time_2.2, signal_2.2 ......
the last line is time_2.2048, signal_2.2048.
......
All of the time_4096.1 , signal_4096.1, time_4096.2, signal_4096.2
...... time_4096.2048, signal_4096.2048 should save in file_4096.tsv.
And the first line is time_4096.1 signal_4096.1; the second line is
time_4096.2, signal_4096.2 ...... the last line is time_4096.2048,
signal_4096.2048.
Finally, I developed a script like following, but it took around 3 hours.
My target(wish) is less than 30 minutes :-).
Any body have suggests to make it speed up ?
Many thanks in advance !
Junhui
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>#include <cstring>
#include <iomanip>
using namespace std;
int main ()
{
//ifstream inFile ("../test_7_10lines.tsv");
ifstream inFile ("../two_ch.tsv");
if (!inFile.good() || !inFile.is_open() )
{
std::cout << "\nWARNING! Cannot read from file: " <<" ../two_ch.tsv"<< std::endl;
}
string line; int linenum = 0;
vector <double> time;
vector <double> time_pick;
vector <double> vol;
vector <double> vol_pick;
double time_base[4096];
while (getline (inFile, line))
{
linenum++;
int itemnum = 0;
istringstream linestream(line);
if(linenum >= 22)
{
string item;
itemnum = 0;
while (getline (linestream, item, ' '))
{
double time_, vol_, time_origin;
itemnum ++;
if ( (itemnum % 2) != 0)
{
istringstream(item) >>time_ ;
//cout << "time_ = " <<setprecision(15) << scientific <<time_<<endl;
time.push_back(time_) ;
}
else
{
istringstream(item) >> vol_ ;
//cout << "vol_ = " <<setprecision(5) << scientific <<vol_<<endl;
vol.push_back(vol_);
}
}//end of while(getline ()) loop
}//end of if(linenum >=22)
}//end of while (inFile, line)
inFile.close();
for(int i = 0; i<4096; i++)
{
time_base[i] = time[i];
//cout << "time_base = "<<setprecision(15) << scientific<<time_base[i]<<endl;
}
//re-organize the vector,make each signal(1024 points) connects to another.
int i = 0, j = 4095, k =4096, n = 0, p = 0, q = 0, r = 1024, t = 0;
for (i = 0; i <= j; i++)//j means the index of file name. This loop to obtain 1024 files.
{
for (int m = 0; m < time.size()/2; m++)//This loop to reorganize channel 1 in vector.
{
n = m%k; //cout << "n= " <<n<<endl;
if (i == n)//k means each 4096 items compose a "group"
{
time_pick.push_back(time[m]);
vol_pick.push_back(vol[m]);
//cout << "time["<<p<<"] ="<<setprecision(15) << scientific<<time_pick[p] << "\tvol["<<p<<"] ="<<setprecision(5) << scientific<<vol_pick[p]<<endl;
//p++;
}
}
for (int m = time.size()/2; m < time.size(); m++)//This to reorganize channel 2 in vector.
{
n = m%k; //cout << "n= " <<n<<endl;
if (i == n)//this to get all points of all channel 2's signals.
{
time_pick.push_back(time[m]);
vol_pick.push_back(vol[m]);
}
}
}
//for(int p = 0; p < time.size(); p++)
//{
// cout << "time["<<p<<"] ="<<setprecision(15) << scientific<<time_pick[p] << "\tvol["<<p<<"] ="<<setprecision(3) << scientific<<vol_pick[p]<<endl;
//}
//cout<<"time.size()= "<<time.size()<<endl;
//Begin to save files
for (int m = 0; m < time.size(); m++)
{
q = m/(2*r) ; //r == 1024;
if ((m >= q*2*r)&&(m < (q+1)*2*r))
{
stringstream sstrm;
sstrm <<"vector_ch1ch2_"<< q << ".tsv";
ofstream outFile(sstrm.str().c_str(),ios_base::app);
outFile << setprecision(15) << scientific << time_pick[m] - time_base[q] <<" " << setprecision(2) << scientific<<vol_pick[m] << endl;
}
//cout << "q1 = "<<q<<endl;
}
outFile.close();
return 0;
}//End of main()
|
PS : During the period of debugging, a few guys presented their suggestions and
advices. Thanks a lot for their help again.