diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src')
6 files changed, 426 insertions, 0 deletions
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/Util.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/Util.h new file mode 100644 index 0000000..a906e55 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/Util.h @@ -0,0 +1,32 @@ +/* + * Util.h + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +#ifndef _GLIBCXX_STRING + #include <string> +#endif + +#ifndef _GLIBCXX_SSTREAM + #include <sstream> +#endif + +class Util +{ +public: + static std::string ToString(const long double& val) + { + std::ostringstream ssIn; + ssIn.precision(12); + ssIn << val; + std::string res = ssIn.str(); + return res; + } +}; + +#endif /* UTIL_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzer.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzer.cpp new file mode 100644 index 0000000..abe296f --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzer.cpp @@ -0,0 +1,158 @@ +/* + * WaveAnalyzer.cpp + * + * Created on: 02.04.2011 + * Author: sven + */ +#ifndef WAVEANALYZER_H_ + #include "WaveAnalyzer.h" +#endif + +#ifndef _GLIBCXX_FSTREAM + #include <fstream> +#endif + +#ifndef _GLIBCXX_IOSTREAM + #include <iostream> +#endif + +#ifndef _GLIBCXX_CMATH + #include <cmath> +#endif + +#ifndef _GLIBCXX_MAP + #include <map> +#endif + +#ifndef _GLIBCXX_NUMERIC_LIMITS + #include <limits> +#endif + +WaveAnalyzer::WaveAnalyzer() { + // TODO Auto-generated constructor stub + +} + +WaveAnalyzer::~WaveAnalyzer() { + // TODO Auto-generated destructor stub +} + +long double WaveAnalyzer::calcSNR(std::string& fullQuantFilename, std::string& lowQuantFilename) +{ + long double result = 0.0; + double sumXnSquare = 0.0; + double sumDiffSquare = 0.0; + std::ifstream fullQuantFile; + std::ifstream lowQuantFile; + fullQuantFile.open(fullQuantFilename.c_str(), std::ios::in|std::ios::binary); + lowQuantFile.open(lowQuantFilename.c_str(), std::ios::in|std::ios::binary); + int dataLenOffset = 0x28; + fullQuantFile.seekg(dataLenOffset,std::ios_base::cur); + int numSamples = 0; + fullQuantFile.read((char*) &numSamples, sizeof(numSamples)); + lowQuantFile.seekg(dataLenOffset+sizeof(numSamples),std::ios_base::cur); + double fullVal = 0.0; + double lowVal = 0.0; + short data = 0; + int diff = 0; + for(int i=0; i < numSamples; ) { + fullQuantFile.read((char*) &data, sizeof(data)); + fullVal = (double)(data); + //std::cout << "full: " << data << " "<< fullVal << std::endl; + lowQuantFile.read((char*) &data, sizeof(data)); + lowVal = (double)(data); + //std::cout << "low: " << data << " "<< lowVal << std::endl; + sumXnSquare += (fullVal * fullVal); + //std::cout << "Sum Xn Square: " << sumXnSquare << std::endl; + diff = lowVal - fullVal; + sumDiffSquare += (diff * diff); + //std::cout << "Sum Diff Square: " << sumDiffSquare << std::endl; + i += sizeof(data); + } + if (sumDiffSquare != 0.0) { + result = 10.0 * std::log10(sumXnSquare / sumDiffSquare); + } else { + result = std::numeric_limits<long double>::infinity(); + } + fullQuantFile.close(); + lowQuantFile.close(); + return result; +} + +long double WaveAnalyzer::calcArithAvg(std::string& wavefilename) +{ + long double result = 0.0; + int sum = 0; + std::ifstream waveFile; + waveFile.open(wavefilename.c_str(), std::ios::in|std::ios::binary); + int dataLenOffset = 0x28; + waveFile.seekg(dataLenOffset,std::ios_base::cur); + int numSamples = 0; + waveFile.read((char*) &numSamples, sizeof(numSamples)); + short data = 0; + for(int i=0 ; i < numSamples; ) { + waveFile.read((char*) &data, sizeof(data)); + sum += data; + i+=sizeof(data); + } + result = ((long double) sum)/((long double)numSamples); + waveFile.close(); + return result; +} + +long double WaveAnalyzer::calcVariance(std::string& wavefilename) +{ + long double variance = 0.0; + std::map<short,statVal*> statMap; + std::ifstream waveFile; + waveFile.open(wavefilename.c_str(), std::ios::in|std::ios::binary); + UInt32 dataLenOffset = 0x28; + waveFile.seekg(dataLenOffset,std::ios_base::cur); + UInt32 numBytes = 0; + waveFile.read((char*) &numBytes, sizeof(numBytes)); + UInt32 numSamples = numBytes / sizeof(short); + for(UInt32 i=0 ; i < numSamples; i++) { + short data = 0; + waveFile.read((char*) &data, sizeof(data)); + std::map<short,statVal*>::iterator findIt = statMap.find(data); + if (findIt == statMap.end()) { + statVal* vPtr = new statVal; + vPtr->count = 1UL; + vPtr->p = 0.0; + statMap.insert(std::make_pair(data,vPtr)); + } else { + findIt->second->count++; + } + } + waveFile.close(); + long double expectation = 0.0; + std::map<short,statVal*>::iterator it; + long double pSum = 0.0; + for(it = statMap.begin() ; it != statMap.end(); it++) + { + long double x = (long double) it->first; + it->second->p = (long double) it->second->count / (long double) numSamples; + long double actExp = (x * it->second->p); + expectation += actExp; + pSum += it->second->p; + } + std::cout << "expectation: " << expectation << " pSum: " << pSum << std::endl; + for(it = statMap.begin() ; it != statMap.end(); it++) + { + long double x = (long double) it->first; + long double diff = x - expectation; + long double diffQuad = (diff*diff); + variance += diffQuad * it->second->p; + } + for (it = statMap.begin() ; it != statMap.end();) { + delete it->second; + it++; + } + statMap.clear(); + return variance; +} + +long double WaveAnalyzer::calcStdDeviation(std::string& wavefilename) +{ + return std::sqrt(calcVariance(wavefilename)); +} diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzer.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzer.h new file mode 100644 index 0000000..11b5b1f --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzer.h @@ -0,0 +1,33 @@ +/* + * WaveAnalyzer.h + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef WAVEANALYZER_H_ +#define WAVEANALYZER_H_ + +#ifndef _GLIBCXX_STRING + #include <string> +#endif + +typedef unsigned long UInt32; + +struct statVal +{ + UInt32 count; + long double p; +}; + +class WaveAnalyzer { +public: + WaveAnalyzer(); + virtual ~WaveAnalyzer(); + long double calcSNR(std::string&, std::string&); + long double calcArithAvg(std::string&); + long double calcVariance(std::string&); + long double calcStdDeviation(std::string&); +}; + +#endif /* WAVEANALYZER_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzerController.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzerController.cpp new file mode 100644 index 0000000..da87dbb --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzerController.cpp @@ -0,0 +1,128 @@ +/* + * WaveAnalyzerController.cpp + * + * Created on: 02.04.2011 + * Author: sven + */ +#ifndef WAVEANALYZERCONTROLLER_H_ + #include "WaveAnalyzerController.h" +#endif +#ifndef WAVEANALYZER_H_ + #include "WaveAnalyzer.h" +#endif +#ifndef UTIL_H_ + #include "Util.h" +#endif + +const char* WaveAnalyzerController::UI_FILENAME = "ui/waveanalyzer.glade"; + +WaveAnalyzerController::WaveAnalyzerController() +{ + Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(UI_FILENAME); + builder->get_widget("mainwindow",mPtrMainWin); + + builder->get_widget("fullquantfile",mPtrFullQuantFile); + builder->get_widget("lowquantfile",mPtrLowQuantFile); + builder->get_widget("snrvalue",mPtrSNRValue); + builder->get_widget("avgvalue",mPtrArithAvgValue); + builder->get_widget("variancevalue",mPtrVarianceValue); + builder->get_widget("deviationvalue",mPtrStdDevValue); + + builder->get_widget("openfullquantbtn",mPtrOpenFullQuantBtn); + builder->get_widget("openlowquantbtn",mPtrOpenLowQuantBtn); + builder->get_widget("calcsnrbtn",mPtrCalcSNRBtn); + builder->get_widget("calc_avg_btn",mPtrCalcArithAvgBtn); + builder->get_widget("calc_variance_btn",mPtrCalcVarianceBtn); + builder->get_widget("calc_deviation_btn",mPtrCalcStdDevBtn); + + mPtrOpenFullQuantBtn->signal_clicked().connect(sigc::mem_fun(this,&WaveAnalyzerController::on_open_fullquant_btn)); + mPtrOpenLowQuantBtn->signal_clicked().connect(sigc::mem_fun(this,&WaveAnalyzerController::on_open_lowquant_btn)); + mPtrCalcSNRBtn->signal_clicked().connect(sigc::mem_fun(this,&WaveAnalyzerController::on_calcsnr_btn)); + mPtrCalcArithAvgBtn->signal_clicked().connect(sigc::mem_fun(this,&WaveAnalyzerController::on_calcarithavg_btn)); + mPtrCalcVarianceBtn->signal_clicked().connect(sigc::mem_fun(this,&WaveAnalyzerController::on_calcvariance_btn)); + mPtrCalcStdDevBtn->signal_clicked().connect(sigc::mem_fun(this,&WaveAnalyzerController::on_calcstddev_btn)); +} + +WaveAnalyzerController::~WaveAnalyzerController() { + // TODO Auto-generated destructor stub +} +void WaveAnalyzerController::startApp(Gtk::Main& kit) +{ + kit.run(*mPtrMainWin); +} + +void WaveAnalyzerController::on_open_fullquant_btn() +{ + choose_file(mFullQuantFilename); + mPtrFullQuantFile->set_text(mFullQuantFilename); +} + +void WaveAnalyzerController::on_open_lowquant_btn() +{ + choose_file(mLowQuantFilename); + mPtrLowQuantFile->set_text(mLowQuantFilename); +} + +void WaveAnalyzerController::on_calcsnr_btn() +{ + if (mFullQuantFilename.empty() || mLowQuantFilename.empty()) { + return; + } + WaveAnalyzer waveAnalyzer; + double snr = waveAnalyzer.calcSNR(mFullQuantFilename,mLowQuantFilename); + mPtrSNRValue->set_text(Util::ToString(snr)); +} + +void WaveAnalyzerController::on_calcarithavg_btn() +{ + if (mFullQuantFilename.empty()) { + return; + } + WaveAnalyzer waveAnalyzer; + double arithAvg = waveAnalyzer.calcArithAvg(mFullQuantFilename); + mPtrArithAvgValue->set_text(Util::ToString(arithAvg)); +} + +void WaveAnalyzerController::on_calcvariance_btn() +{ + if (mFullQuantFilename.empty()) { + return; + } + WaveAnalyzer waveAnalyzer; + double variance = waveAnalyzer.calcVariance(mFullQuantFilename); + mPtrVarianceValue->set_text(Util::ToString(variance)); +} + +void WaveAnalyzerController::on_calcstddev_btn() +{ + if (mFullQuantFilename.empty()) { + return; + } + WaveAnalyzer waveAnalyzer; + double stdDev = waveAnalyzer.calcStdDeviation(mFullQuantFilename); + mPtrStdDevValue->set_text(Util::ToString(stdDev)); +} + +void WaveAnalyzerController::choose_file(std::string& filename) +{ + Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(UI_FILENAME); + Gtk::FileChooserDialog* ptrFilechooser; + builder->get_widget("filechooserdialog",ptrFilechooser); + Gtk::FileFilter waveFilter; + waveFilter.set_name("Wave files"); + waveFilter.add_pattern("*.wav"); + ptrFilechooser->add_filter(waveFilter); + ptrFilechooser->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + ptrFilechooser->add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + ptrFilechooser->set_transient_for(*mPtrMainWin); + int result = ptrFilechooser->run(); + switch (result) + { + case Gtk::RESPONSE_OK: + filename = ptrFilechooser->get_filename(); + break; + default: + break; + } + delete ptrFilechooser; +} diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzerController.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzerController.h new file mode 100644 index 0000000..e6c55a1 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/WaveAnalyzerController.h @@ -0,0 +1,53 @@ +/* + * WaveAnalyzerController.h + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef WAVEANALYZERCONTROLLER_H_ +#define WAVEANALYZERCONTROLLER_H_ + +#ifndef _GTKMM_H + #include <gtkmm.h> +#endif + +#ifndef _GLIBCXX_STRING + #include <string> +#endif + +class WaveAnalyzerController { +public: + WaveAnalyzerController(); + virtual ~WaveAnalyzerController(); + void startApp(Gtk::Main& kit); +private: + static const char* UI_FILENAME; + std::string mFullQuantFilename; + std::string mLowQuantFilename; + + Gtk::Window* mPtrMainWin; + Gtk::Button* mPtrOpenFullQuantBtn; + Gtk::Button* mPtrOpenLowQuantBtn; + Gtk::Button* mPtrCalcSNRBtn; + Gtk::Button* mPtrCalcArithAvgBtn; + Gtk::Button* mPtrCalcVarianceBtn; + Gtk::Button* mPtrCalcStdDevBtn; + + Gtk::Entry* mPtrFullQuantFile; + Gtk::Entry* mPtrLowQuantFile; + Gtk::Entry* mPtrSNRValue; + Gtk::Entry* mPtrArithAvgValue; + Gtk::Entry* mPtrVarianceValue; + Gtk::Entry* mPtrStdDevValue; + + void on_open_fullquant_btn(); + void on_open_lowquant_btn(); + void on_calcsnr_btn(); + void on_calcarithavg_btn(); + void on_calcvariance_btn(); + void on_calcstddev_btn(); + void choose_file(std::string&); +}; + +#endif /* WAVEANALYZERCONTROLLER_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/main.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/main.cpp new file mode 100644 index 0000000..755e724 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/WaveAnalyzer/src/main.cpp @@ -0,0 +1,22 @@ +/* + * main.cpp + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef WAVEANALYZERCONTROLLER_H_ + #include "WaveAnalyzerController.h" +#endif + +#ifndef _GTKMM_H + #include <gtkmm.h> +#endif + +int main(int argc, char* argv[]) +{ + Gtk::Main kit(argc,argv); + WaveAnalyzerController controller; + controller.startApp(kit); + return 0; +} |
