//TODO: add timer void run(int size, double coupling, IsingModel<dimensions>::Algorithms algo, int sweeps){ QElapsedTimer timer; timer.start(); QVector<int> energies(nModels); QVector<int> absoluteMagnetism(nModels); QFile file(QString("./%3d_%0_%1_%2.dat").arg(size).arg(coupling).arg(algo).arg(dimensions)); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){ std::cout << "Couldn't open file " << file.fileName().toStdString() << std::endl; return; } QTextStream out(&file); std::cout << "Starting with " << file.fileName().toStdString() << std::endl; IsingModel<dimensions> ising[nModels]; QVector<double> magnetization, magnetizationVariance; QVector<double> energy, energyVariance; for(int k = 0; k < nModels; k++){ ising[k].setSize(size); ising[k].setAlgorithm(algo); ising[k].setCoupling(coupling); } out << dimensions << " " << size << " " << coupling << " " << nModels << "\n\n"; out << ising[0].getMagnetization() << " " << sqrt(0) << " " << ising[0].getEnergy() << " " << sqrt(0) << "\n"; for(int i = 0; i < sweeps;i++){ for(int k = 0; k < nModels;k++){ ising[k].sweep(); absoluteMagnetism[k] = abs(ising[k].getMagnetization()); energies[k] = ising[k].getEnergy(); } double magneticMean = calculateMean(absoluteMagnetism); magnetization.append(magneticMean); magnetizationVariance.append(calculateVariance(absoluteMagnetism, magneticMean)); double energyMean = calculateMean(energies); energy.append(energyMean); energyVariance.append(calculateVariance(energies, energyMean)); } for(int i = 0; i < magnetization.size();i++) out << magnetization[i] << " " << magnetizationVariance[i] << " " << energy[i] << " " << energyVariance[i] << "\n"; std::cout << "Finished with " << file.fileName().toStdString() << " in " << timer.elapsed() << "ms" << std::endl; }
void Statistics::calculate(ssi_stream_t &stream_in) { ssi_real_t * in_ptr = ssi_pcast(ssi_real_t, stream_in.ptr); for (ssi_size_t d_i = 0; d_i < _dim; d_i++) { for (ssi_size_t num_i = 0; num_i < stream_in.num; num_i++){ _tmp_arr[d_i][num_i] = in_ptr[d_i*stream_in.num + num_i]; } } calculate_running_stats(stream_in.num); std::vector<stat_fn> stat_fns = _options.getSelection(); for (ssi_size_t d_i = 0; d_i < _dim; d_i++) { for (ssi_size_t f_i = 0; f_i < stat_fns.size(); f_i++){ ssi_size_t r_idx = d_i * ssi_size_t (stat_fns.size()) + f_i; switch (stat_fns[f_i]) { case STAT_KURTOSIS: _res[r_idx] = calculateKurtosis(d_i); break; case STAT_SKEWNESS: _res[r_idx] = calculateSkewness(d_i); break; case STAT_MEAN: _res[r_idx] = calculateMean(d_i); break; case STAT_STDDEV: _res[r_idx] = calculateStandardDeviation(d_i); break; case STAT_VARIANCE: _res[r_idx] = calculateVariance(d_i); break; case STAT_NUMBER_VALS: _res[r_idx] = calulateNumberVals(d_i); break; default: ssi_err("stat_fn not implemented"); } } } }
int main (void) { FILE *inputStream = NULL, /* File pointer for input file */ *outputStream = NULL; /* File pointer for output file */ int studentId1 = 0, studentId2 = 0, studentId3 = 0, studentId4 = 0, studentId5 = 0; /* 5 student IDs */ int studentClassStanding1 = 0, studentClassStanding2 = 0, studentClassStanding3 = 0, studentClassStanding4 = 0, studentClassStanding5 = 0; /* 5 student class standings */ double studentGpa1 = 0.0, studentGpa2 = 0.0, studentGpa3 = 0.0, studentGpa4 = 0.0, studentGpa5 = 0.0; /* 5 student GPAs */ double studentAge1 = 0.0, studentAge2 = 0.0, studentAge3 = 0.0, studentAge4 = 0.0, studentAge5 = 0.0; /* 5 student ages */ double sumGpa = 0.0, /* sum of the 5 student's GPA */ sumClassStanding = 0.0, /* sum of the 5 student's class standing */ sumAge = 0.0; /* sum of the 5 student's ages */ double meanGpa = 0.0, /* mean of the 5 student's GPA */ meanClassStanding = 0.0, /* mean of the 5 student's class standing */ meanAge = 0.0; /* mean of the 5 student's ages */ double deviationGpa1 = 0.0, deviationGpa2 = 0.0, deviationGpa3 = 0.0, deviationGpa4 = 0.0, deviationGpa5 = 0.0; /* deviation of each student's GPA */ double varianceGpa = 0.0, /* variance of the 5 student's GPA */ standardDeviationGpa = 0.0, /* standard variance of the 5 student's GPA */ maxStudentGpa = 0.0, /* maximum number out of the 5 student's GPA */ minStudentGpa = 0.0; /* minimum number out of the 5 student's GPA */ /* Opens an input file "input.dat" for reading; */ inputStream = openFileStream (INPUT_FILE, INPUT_FILE_MODE); /* Opens an output file "output.dat" for writing; */ outputStream = openFileStream (OUTPUT_FILE, OUTPUT_FILE_MODE); /* Checks the condition if files were successfully opened. * If either file didn't open, then an error message is prompted, * otherwise program will continue to execute other operations. */ if (inputStream == NULL || outputStream == NULL) { /* Files were not successfully opened and prompts user of the error. * Program will not continue to execute the rest of the program. */ printf ("Error: Not able to open files!\n"); } else { /* Reads five records from the input file (input.dat); */ /* Student 1 */ studentId1 = readInteger (inputStream); studentGpa1 = readDouble (inputStream); studentClassStanding1 = readInteger (inputStream); studentAge1 = readDouble (inputStream); /* Student 2 */ studentId2 = readInteger (inputStream); studentGpa2 = readDouble (inputStream); studentClassStanding2 = readInteger (inputStream); studentAge2 = readDouble (inputStream); /* Student 3 */ studentId3 = readInteger (inputStream); studentGpa3 = readDouble (inputStream); studentClassStanding3 = readInteger (inputStream); studentAge3 = readDouble (inputStream); /* Student 4 */ studentId4 = readInteger (inputStream); studentGpa4 = readDouble (inputStream); studentClassStanding4 = readInteger (inputStream); studentAge4 = readDouble (inputStream); /* Student 5 */ studentId5 = readInteger (inputStream); studentGpa5 = readDouble (inputStream); studentClassStanding5 = readInteger (inputStream); studentAge5 = readDouble (inputStream); /* Calculates the sum of the GPAs; */ sumGpa = calculateSum (studentGpa1, studentGpa2, studentGpa3, studentGpa4, studentGpa5); /* Calculates the sum of the class standings; */ sumClassStanding = calculateSum (studentClassStanding1, studentClassStanding2, studentClassStanding3, studentClassStanding4, studentClassStanding5); /* Calculates the sum of the ages; */ sumAge = calculateSum (studentAge1, studentAge2, studentAge3, studentAge4, studentAge5); /* Calculates the mean of the GPAs; */ meanGpa = calculateMean (sumGpa, NUMBER_OF_STUDENTS); /* Calculates the mean of the class standings; */ meanClassStanding = calculateMean (sumClassStanding, NUMBER_OF_STUDENTS); /* Calculates the mean of the ages; */ meanAge = calculateMean (sumAge, NUMBER_OF_STUDENTS); /* Calculates the deviation of each GPA from the mean */ deviationGpa1 = calculateDeviation (studentGpa1, meanGpa); deviationGpa2 = calculateDeviation (studentGpa2, meanGpa); deviationGpa3 = calculateDeviation (studentGpa3, meanGpa); deviationGpa4 = calculateDeviation (studentGpa4, meanGpa); deviationGpa5 = calculateDeviation (studentGpa5, meanGpa); /* Calculates the variance of the GPAs */ varianceGpa = calculateVariance (deviationGpa1, deviationGpa2, deviationGpa3, deviationGpa4, deviationGpa5, NUMBER_OF_STUDENTS); /* Calculates the standard deviation of the GPAs; */ standardDeviationGpa = calculateStandardDeviation (varianceGpa); /* Determines the min of the GPAs; */ minStudentGpa = findMin (studentGpa1, studentGpa2, studentGpa3, studentGpa4, studentGpa5); /* Determines the max of the GPAs; */ maxStudentGpa = findMax (studentGpa1, studentGpa2, studentGpa3, studentGpa4, studentGpa5); /* Writing the result to the output file (output.dat) */ printDouble (outputStream, meanGpa); printDouble (outputStream, meanClassStanding); printDouble (outputStream, meanAge); printDouble (outputStream, standardDeviationGpa); printDouble (outputStream, minStudentGpa); printDouble (outputStream, maxStudentGpa); /* Closes the input and output files (i.e. input.dat and output.dat) */ closeFileStream (inputStream); closeFileStream (outputStream); } return 0; }
double Comparator::calculateStdDeviation(double *values, int sizeOfArray){ double variance = calculateVariance(values,sizeOfArray); return sqrt(variance); }
ssi_real_t Statistics::calculateStandardDeviation(ssi_size_t dim_idx) { return sqrt(calculateVariance(dim_idx)); }
float Measures::calculateDeviation() { return qSqrt(calculateVariance()); }
Frame CameraWrapper::record() { // clear buffer rs_wait_for_frames(dev, &e); check_error(); std::vector<cv::Mat> depthstack; std::vector<cv::Mat> colorstack; std::vector<cv::Mat> irstack; // record a stack of frames for(int frame = 0; frame < framesToRecord; frame++) { // wait for frames rs_wait_for_frames(dev, &e); check_error(); // get data const uint16_t * depth_image = (const uint16_t *)rs_get_frame_data(dev, RS_STREAM_DEPTH, &e); check_error(); const uint8_t * color_image = (const uint8_t *)rs_get_frame_data(dev, RS_STREAM_COLOR, &e); check_error(); const uint8_t * ir_image = (const uint8_t *)rs_get_frame_data(dev, RS_STREAM_INFRARED, &e); check_error(); // convert depth to meters and float cv::Mat depthframe = cv::Mat(depth_intrin.height, depth_intrin.width, CV_32F); int dx, dy; for(dy=0; dy<depth_intrin.height; ++dy) { for(dx=0; dx<depth_intrin.width; ++dx) { /* Retrieve the 16-bit depth value and map it into a depth in meters */ uint16_t depth_value = depth_image[dy * depth_intrin.width + dx]; float depth_in_meters = depth_value * depthScale; depthframe.at<float>(dy, dx) = depth_in_meters; } } depthstack.push_back(depthframe); // color cv::Mat colorframeRGB = cv::Mat(color_intrin.height, color_intrin.width, CV_8UC3, cv::Scalar(0)); memcpy(colorframeRGB.ptr(),color_image,color_intrin.height*color_intrin.width*3); // f200 gives RGB image, openCV uses BGR - so convert cv::Mat colorframeBGR; cv::cvtColor(colorframeRGB, colorframeBGR, CV_RGB2BGR); colorstack.push_back(colorframeBGR); // ir cv::Mat irframe = cv::Mat(ir_intrin.height, ir_intrin.width, CV_8UC1, cv::Scalar(0)); memcpy(irframe.ptr(),ir_image,ir_intrin.height*ir_intrin.width); irstack.push_back(irframe); } // create Frame object Frame frame1; convertIntrinsicToOpenCV(depth_intrin, frame1.cameraMatrix_depth, frame1.coefficients_depth); convertIntrinsicToOpenCV(color_intrin, frame1.cameraMatrix_color, frame1.coefficients_color); convertIntrinsicToOpenCV(ir_intrin, frame1.cameraMatrix_ir, frame1.coefficients_ir); Eigen::Matrix3f R(depth_to_color.rotation); Eigen::Vector3f T(depth_to_color.translation); Eigen::Matrix4f m1 = Eigen::Affine3f(Eigen::Translation3f(T)).matrix(); Eigen::Matrix4f m2 = Eigen::Affine3f(R).matrix(); Eigen::Matrix4f dtc = m1*m2; Eigen::Affine3f mpose (dtc); frame1.depth_to_color_transform = mpose; // call averager // DESIGN add interface // limit to 4 frames // average RGB images int framesToRecordRGB; if (framesToRecord > 4) framesToRecordRGB = 4; else framesToRecordRGB = framesToRecord; frame1.processedImageRGB = cv::Mat::zeros(color_intrin.height, color_intrin.width, CV_8UC3); for (int frame = 0; frame < framesToRecordRGB; frame++) { frame1.processedImageRGB = frame1.processedImageRGB + (1.0/framesToRecordRGB)*colorstack[frame]; } // average IR frame1.processedImageIR = cv::Mat::zeros(ir_intrin.height, ir_intrin.width, CV_8UC1); for (int frame = 0; frame < framesToRecordRGB; frame++) { frame1.processedImageIR = frame1.processedImageIR + (1.0/framesToRecordRGB)*irstack[frame]; } // average depth, calculate variance and zeroes cv::Mat belief(depth_intrin.height, depth_intrin.width, CV_8U, cv::Scalar::all(0)); cv::Mat processedImageDepth(depth_intrin.height, depth_intrin.width, CV_32F, cv::Scalar::all(0)); for (int y = 0; y < depth_intrin.height; y++) { for (int x = 0; x < depth_intrin.width; x++) { double tempresult_depth = 0; int depth_zeroes = 0; std::vector<float> var_values; for(int frame = 0; frame < depthstack.size(); frame++) { if(depthstack[frame].at<float>(y,x) == 0.0) { depth_zeroes++; }else { tempresult_depth += (float)depthstack[frame].at<float>(y,x); // increase values by big factor because of value range var_values.push_back(depthstack[frame].at<float>(y,x)*10000); } } // calculate average double var = calculateVariance(var_values); // limit variance for display reasons if (var > 255.0) var = 255.0; // reduce belief if pixel has zero value i.e. no data int zeroPenaltyFactor = 255/depthstack.size(); int beliefPx = 255-((int)var + (depth_zeroes * zeroPenaltyFactor)); if (beliefPx < 0) beliefPx = 0; processedImageDepth.at<float>(y,x) = tempresult_depth/(depthstack.size()-depth_zeroes); // zeroes: (255-((255/stack.size())*depth_zeroes) belief.at<uchar>(y,x) = beliefPx; } } frame1.belief = belief; frame1.processedImageDepth = processedImageDepth; //frame1.processedImageDepth = depthstack[0]; return frame1; }