Skip to content

Commit

Permalink
Merge branch 'release/v0.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
howeverforever committed Sep 15, 2017
2 parents 92c1576 + b2a0555 commit 3fc4c4f
Show file tree
Hide file tree
Showing 177 changed files with 106,030 additions and 15,159 deletions.
138 changes: 72 additions & 66 deletions C_programming/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#include <functional>
#include <numeric>
#include <deque>
#include <iostream>

using namespace std;

Model::Model(const char* filename) {
this->_original_data = Open(filename);
Open(filename);
}

/** @brief: Calculate gaps between peaks and valleys.
Expand Down Expand Up @@ -37,17 +38,15 @@ double Model::FindGaps(vector <double> peaks, vector <double> valleys) {
* Create the file and write down.
* @param index: the axis which we should consider
* @param mean: the average of gaps
* @param std: the stardard deviation of gaps
* @return:
**/
void Model::WriteToFile(int index, double mean, double std) {
void Model::WriteToFile(double mean, double std) {
FILE* fp = fopen(Model::TRAINING_MODEL_FILE, "w");
fprintf(fp, "%d\n", index);
fprintf(fp, "%f\n", mean);
fprintf(fp, "%f\n", std);
fprintf(fp, "%lf\n", mean);
fprintf(fp, "%lf\n", std);
fclose(fp);
}

Expand All @@ -61,69 +60,78 @@ void Model::WriteToFile(int index, double mean, double std) {
**/
void Model::Run(int time_interval) {
// get the end mark
int end_pos = min(int(_original_data.size()), time_interval * SAMPLE_RATE);
int n = min(int(_original_data.size()), time_interval * SAMPLE_RATE);

Vector3d mu(DIM);
mu.setZero();

for (int i = 0; i < n; i++) for (int j = 0; j < DIM; j++)
mu(j) += _original_data[i](j) / n;

MatrixXd cov(DIM, DIM);
cov.setZero();
for (int i = 0; i < DIM; i++) for (int j = 0; j < DIM; j++) for (int k = 0; k < n; k++)
cov(i, j) += (_original_data[k](i) - mu(i)) * (_original_data[k](j) - mu(j)) / n;

// use power method to compute dominant eigenvector
Vector3d v(1, 0, 0);
for (int i = 0; i < ITERATION; i++) {
v = cov * v;
v.normalize();
}

// initialize
double now_max_gap = 0.0;
int now_max_gap_index = -1;
vector <double> now_gaps = vector <double>();

// loop for X, Y, Z axes.
for (int axis_index = 0; axis_index < 3; axis_index++) {
// slice the particular axis data
vector <double> buffer;
for (int j = 0; j < end_pos; j++)
buffer.emplace_back(_original_data[j](axis_index));

vector <double> valleys = vector <double>();
vector <double> peaks = vector <double>();

// generate needed information for the first "PAGE_SIZE" data
assert(_original_data.size() > PAGE_SIZE);
for (int j = 1; j < PAGE_SIZE - 1; j++) {
if (buffer[j] > buffer[j - 1] && buffer[j] > buffer[j + 1])
peaks.emplace_back(buffer[j]);
if (buffer[j] < buffer[j - 1] && buffer[j] < buffer[j + 1])
valleys.emplace_back(buffer[j]);
}
sort(valleys.begin(), valleys.end());
sort(peaks.begin(), peaks.end());
_components = v;

vector <double> gaps;
gaps.emplace_back(FindGaps(peaks, valleys));
cout << mu << endl;
cout << _components << endl;

// simulate the sliding window on "buffer"
for (int j = PAGE_SIZE; j < end_pos; j++) {
int s = j - PAGE_SIZE + 1;
if (buffer[s] > buffer[s - 1] && buffer[s] > buffer[s + 1])
peaks.erase(find(peaks.begin(), peaks.end(), buffer[s]));
if (buffer[s] < buffer[s - 1] && buffer[s] < buffer[s + 1])
valleys.erase(find(valleys.begin(), valleys.end(), buffer[s]));

int e = j - 1;
if (buffer[e] > buffer[e - 1] && buffer[e] > buffer[e + 1])
peaks.insert(lower_bound(peaks.begin(), peaks.end(), buffer[e]), buffer[e]);
if (buffer[e] < buffer[e - 1] && buffer[e] < buffer[e + 1])
valleys.insert(lower_bound(valleys.begin(), valleys.end(), buffer[e]), buffer[e]);
gaps.emplace_back(FindGaps(peaks, valleys));
}
// initialize
vector <double> buffer;
for (int j = 0; j < n; j++) {
double pca = 0.0;
for (int k = 0; k < DIM; k++)
pca += _original_data[j](k) * _components(k);
buffer.emplace_back(pca);
}
vector <double> valleys;
vector <double> peaks;

// generate needed information for the first "PAGE_SIZE" data
assert(_original_data.size() > PAGE_SIZE);
for (int j = 1; j < PAGE_SIZE - 1; j++) {
if (buffer[j] > buffer[j - 1] && buffer[j] > buffer[j + 1])
peaks.emplace_back(buffer[j]);
if (buffer[j] < buffer[j - 1] && buffer[j] < buffer[j + 1])
valleys.emplace_back(buffer[j]);
}
sort(valleys.begin(), valleys.end());
sort(peaks.begin(), peaks.end());

vector <double> gaps;
gaps.emplace_back(FindGaps(peaks, valleys));

// simulate the sliding window on "buffer"
for (int j = PAGE_SIZE; j < n; j++) {
int s = j - PAGE_SIZE + 1;
if (buffer[s] > buffer[s - 1] && buffer[s] > buffer[s + 1])
peaks.erase(find(peaks.begin(), peaks.end(), buffer[s]));
if (buffer[s] < buffer[s - 1] && buffer[s] < buffer[s + 1])
valleys.erase(find(valleys.begin(), valleys.end(), buffer[s]));

int e = j - 1;
if (buffer[e] > buffer[e - 1] && buffer[e] > buffer[e + 1])
peaks.insert(lower_bound(peaks.begin(), peaks.end(), buffer[e]), buffer[e]);
if (buffer[e] < buffer[e - 1] && buffer[e] < buffer[e + 1])
valleys.insert(lower_bound(valleys.begin(), valleys.end(), buffer[e]), buffer[e]);

// update feature if we get useful one
double gap = GetMean(gaps);
printf("%lf\n", gap);
if (gap > now_max_gap) {
now_max_gap = gap;
now_max_gap_index = axis_index;
now_gaps = gaps;
}
gaps.emplace_back(FindGaps(peaks, valleys));
}
// output important features to the file.
vector <double> gaps = now_gaps;
double mean = GetMean(gaps);
printf("!! %lf\n", mean);
printf(">> mean = %lf\n", mean);
double std = GetStd(gaps);
WriteToFile(now_max_gap_index, mean, std);
printf("!!!!!!!! %d !!!!!!!!!!!!\n", now_max_gap_index);
printf(">> std = %lf\n", std);
WriteToFile(mean, std);
}

/** @brief: Calculate average of a list.
Expand Down Expand Up @@ -159,13 +167,12 @@ double Model::GetStd(vector <double> xs) {
* @return: tuple (X,Y,Z) list
**/
vector <RowVector3d> Model::Open(const char* filename) {
void Model::Open(const char* filename) {
FILE* fp;
errno_t err = fopen_s(&fp, filename, "r");
if (err != 0)
return vector <RowVector3d>();
return;

vector <RowVector3d> V;
char* line = new char[MAX_COUNT];
while ((fgets(line, MAX_COUNT, fp)) != 0) {
char* token = strtok(line, ",");
Expand All @@ -174,7 +181,6 @@ vector <RowVector3d> Model::Open(const char* filename) {
token = strtok(NULL, ",");
arr[i] = atoi(token);
}
V.emplace_back(RowVector3d(arr[0], arr[1], arr[2]));
_original_data.emplace_back(RowVector3d(arr[0], arr[1], arr[2]));
}
return V;
}
9 changes: 6 additions & 3 deletions C_programming/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ class Model {
const static int TOP_PEAK_PERCENT = 10;
const static int PAGE_SIZE = 100;
const static int SAMPLE_RATE = 20;
const static int DIM = 3;
const static int ITERATION = 1000;
const char* TRAINING_MODEL_FILE = "motorcycle.txt";

public:
Model(const char* filename);
double FindGaps(vector <double> peaks, vector <double> valleys);
void Run(int time_interval);
void WriteToFile(int index, double mean, double std);

protected:
double GetMean(vector <double> xs);
double GetStd(vector <double> xs);
vector <RowVector3d> Open(const char* filename);
void Open(const char* filename);
void WriteToFile(double mean, double std);
double FindGaps(vector <double> peaks, vector <double> valleys);

private:
vector <RowVector3d> _original_data;
RowVector3d _components;
};
2 changes: 1 addition & 1 deletion C_programming/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main() {
double before_time = clock();

// construct a model
Model model = Model("E:\\Python\\SuperMotor\\recorded_original_data\\motor_0808_1_fan1.csv");
Model model = Model("E:\\Python\\SuperMotor\\recorded_original_data\\motor_0504_4Y7M_2_TOP_on.csv");
model.Run(60);

// the states of memory and clock after constructing model
Expand Down
Binary file modified WindowsFormsApp1/.vs/WindowsFormsApp1/v15/.suo
Binary file not shown.
Binary file not shown.
Binary file modified __pycache__/lib.cpython-36.pyc
Binary file not shown.
Binary file added __pycache__/lib2.cpython-36.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions fan_0911.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PMModel
100
0.593349739925,-0.668943604003,0.447717032057
66.6230736343
1.466598138965636
Loading

0 comments on commit 3fc4c4f

Please sign in to comment.