ts-aggregator/project_template/Median.cpp
2022-12-13 12:36:06 +04:00

65 lines
991 B
C++

#include "StdAfx.h"
#include "Median.h"
#include <vector>
#include <algorithm>
Median::Median() {
}
Median::~Median() {
}
double Median::getValue(int, vector<double>, vector<double>){
return 0;
}
double Median::getValue(vector<double>, vector<double>){
return 0;
}
double Median::getValue(vector<double> ts){
double median;
int size = ts.size();
sort(ts.begin(), ts.end());
if (size % 2 == 0)
{
median = (ts[size % 2 - 1] + ts[size % 2 + 1]) / 2;
}
else
{
median = ts[size / 2];
}
return median;
}
vector<double> Median::getValue(vector<double> ts, int period){
int size = ts.size();
int amtPeriod = size / period;
int shift = size - amtPeriod * period;
vector <double> median(amtPeriod);
vector <double> temp(period);
int begin;
int end;
for (int i = 0; i < amtPeriod; i++)
{
begin = shift + i * period;
end = begin + period;
std::copy(ts.begin() + begin, ts.begin() + end, temp.begin());
median[i] = getValue(temp);
}
return median;
}