diff options
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src')
6 files changed, 371 insertions, 0 deletions
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.cpp new file mode 100644 index 0000000..9198223 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.cpp @@ -0,0 +1,103 @@ +/* + * Quantization.cpp + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef QUANTIZATION_H_ + #include "Quantization.h" +#endif + +#ifndef _GLIBCXX_FSTREAM + #include <fstream> +#endif + +#ifndef _GLIBCXX_NUMERIC_LIMITS + #include <limits> +#endif + +#ifndef _GLIBCXX_CMATH + #include <cmath> +#endif + +#ifndef _GLIBCXX_IOSTREAM + #include <iostream> +#endif + +Quantization::Quantization(QuantizationParameters& params) +:mParams(params) +{ + calcQuantizationValues(); + std::ifstream inFile; + std::ofstream outFile; + inFile.open(mParams.getInputFile().c_str(), std::ios::in | std::ios::binary ); + outFile.open(mParams.getOutputFile().c_str(), std::ios::out | std::ios::binary); + int dataLenOffset = 0x28; + char header[dataLenOffset]; + inFile.read(&header[0],dataLenOffset); + outFile.write(&header[0],dataLenOffset); + int numSamples = 0; + inFile.read((char*) &numSamples,sizeof(numSamples)); + outFile.write((char*) &numSamples,sizeof(numSamples)); + short data; + short quantizationized; +// unsigned counter = 0; + for (int i=0; i < numSamples;) { + inFile.read((char*) &data, sizeof(data) ); + quantizationized = getQuantizationValue(data); + outFile.write((char*) &quantizationized, sizeof(quantizationized) ); +// std::cout << counter++ << " " << quantizationized << std::endl; + i += sizeof(short); + } + inFile.close(); + outFile.close(); + std::cout << "quantization done" << std::endl; +} + +Quantization::~Quantization() +{ + QuantizationRangesIter it = mQuantizationRanges.begin(); + for ( ;it < mQuantizationRanges.end(); ) + { + delete *it++; + } + mQuantizationRanges.clear(); +} + +short Quantization::getQuantizationValue(short val) +{ + // shift to unsigned posivitives values + unsigned short shiftedVal = val - std::numeric_limits<short>::min(); + QuantizationRangesIter it = mQuantizationRanges.begin(); + for(; it < mQuantizationRanges.end(); it++) { + QuantizationRange* quantRangePtr = *it; + if (quantRangePtr->IsInRange(shiftedVal)) { + // shift back + return quantRangePtr->mRangeValue + std::numeric_limits<short>::min(); + } + } + std::cout << val << " " << shiftedVal << " not quantizationized" << std::endl; + return -1; +} + +void Quantization::calcQuantizationValues() +{ + mNumQuantizationValues = 1 << mParams.getBits(); +// std::cout << mNumQuantizationValues << " quantization values" << std::endl; + unsigned short valueRangeWidth = std::numeric_limits<unsigned short>::max() / mNumQuantizationValues; +// std::cout << "value range width " << valueRangeWidth << std::endl; + unsigned short rangeMiddle = valueRangeWidth / 2; +// std::cout << "value range middle " << rangeMiddle << std::endl; + QuantizationRange* quantRangePtr = NULL; + for (unsigned i=0; i<mNumQuantizationValues; i++) { + quantRangePtr = new QuantizationRange; + quantRangePtr->mMinValue = i * valueRangeWidth; + quantRangePtr->mMaxValue = ((i+1) * valueRangeWidth) - 1; + quantRangePtr->mRangeValue = quantRangePtr->mMinValue + rangeMiddle; + quantRangePtr->mRangeWidth = valueRangeWidth; +// std::cout << "quantization value "<< (i+1) << ": " << quantRangePtr->mRangeValue << std::endl; +// std::cout << "Range: "<< quantRangePtr->mMinValue << " - " << quantRangePtr->mMaxValue << std::endl; + mQuantizationRanges.push_back(quantRangePtr); + } +} diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.h new file mode 100644 index 0000000..5939591 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.h @@ -0,0 +1,45 @@ +/* + * Quantization.h + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef QUANTIZATION_H_ +#define QUANTIZATION_H_ + +#ifndef QUANTIZATIONPARAMETERS_H_ + #include "QuantizationParameters.h" +#endif + +#ifndef _GLIBCXX_VECTOR + #include <vector> +#endif + +struct QuantizationRange { + unsigned short mMinValue; + unsigned short mMaxValue; + unsigned short mRangeWidth; + unsigned short mRangeValue; + bool IsInRange(unsigned short val) + { + return ((val >= mMinValue) && (val <= mMaxValue)); + } +}; + +typedef std::vector<QuantizationRange*> QuantizationRanges; +typedef std::vector<QuantizationRange*>::iterator QuantizationRangesIter; + +class Quantization { +public: + Quantization(QuantizationParameters& params); + virtual ~Quantization(); +private: + QuantizationParameters& mParams; + unsigned mNumQuantizationValues; + QuantizationRanges mQuantizationRanges; + short getQuantizationValue(short); + void calcQuantizationValues(); +}; + +#endif /* QUANTIZATION_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationController.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationController.cpp new file mode 100644 index 0000000..47196bd --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationController.cpp @@ -0,0 +1,95 @@ +/* + * QuantizationController.cpp + * + * Created on: 02.04.2011 + * Author: sven + */ +#ifndef QUANTIZATIONCONTROLLER_H_ + #include "QuantizationController.h" +#endif + +#ifndef QUANTIZATION_H_ + #include "Quantization.h" +#endif + +const char* QuantizationController::UI_FILENAME = "ui/quantization.glade"; +const double QuantizationController::QUANTIZATIONBITS_DEFAULT = 8.0; +const double QuantizationController::QUANTIZATIONBITS_MIN = 1.0; +const double QuantizationController::QUANTIZATIONBITS_MAX = 16.0; + +QuantizationController::QuantizationController() +:mQuantizationBitsAdj(QUANTIZATIONBITS_DEFAULT,QUANTIZATIONBITS_MIN,QUANTIZATIONBITS_MAX) +{ + Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(UI_FILENAME); + builder->get_widget("mainwindow",mPtrMainWin); + builder->get_widget("quantizationbits",mPtrQuantizationBits); + builder->get_widget("openinputbtn",mPtrOpenInputBtn); + builder->get_widget("openoutputbtn",mPtrOpenOutputBtn); + builder->get_widget("startbtn",mPtrStartBtn); + builder->get_widget("inputfile",mPtrInputFile); + builder->get_widget("outputfile",mPtrOutputFile); + mPtrQuantizationBits->set_adjustment(mQuantizationBitsAdj); + mPtrStartBtn->signal_clicked().connect(sigc::mem_fun(this,&QuantizationController::on_start_btn)); + mPtrOpenInputBtn->signal_clicked().connect(sigc::mem_fun(this,&QuantizationController::on_open_input_btn)); + mPtrOpenOutputBtn->signal_clicked().connect(sigc::mem_fun(this,&QuantizationController::on_open_output_btn)); + mPtrQuantizationBits->signal_value_changed().connect(sigc::mem_fun(this,&QuantizationController::on_quantizationbits_changed)); + mParams.setBits(mPtrQuantizationBits->get_value_as_int()); +} + +QuantizationController::~QuantizationController() { + // TODO Auto-generated destructor stub +} +void QuantizationController::startApp(Gtk::Main& kit) +{ + kit.run(*mPtrMainWin); +} + +void QuantizationController::on_open_input_btn() +{ + std::string filename; + choose_file(filename); + mParams.setInputFile(filename); + mPtrInputFile->set_text(mParams.getInputFile()); +} + +void QuantizationController::on_open_output_btn() +{ + std::string filename; + choose_file(filename); + mParams.setOutputFile(filename); + mPtrOutputFile->set_text(mParams.getOutputFile()); +} + +void QuantizationController::on_quantizationbits_changed() +{ + mParams.setBits(mPtrQuantizationBits->get_value_as_int()); +} + +void QuantizationController::on_start_btn() +{ + Quantization quantization(mParams); +} + +void QuantizationController::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/Quantization/src/QuantizationController.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationController.h new file mode 100644 index 0000000..ea19c64 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationController.h @@ -0,0 +1,51 @@ +/* + * QuantizationController.h + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef QUANTIZATIONCONTROLLER_H_ +#define QUANTIZATIONCONTROLLER_H_ + +#ifndef QUANTIZATIONPARAMETERS_H_ + #include "QuantizationParameters.h" +#endif + +#ifndef _GTKMM_H + #include <gtkmm.h> +#endif + +#ifndef _GLIBCXX_STRING + #include <string> +#endif + +class QuantizationController { +public: + QuantizationController(); + virtual ~QuantizationController(); + void startApp(Gtk::Main& kit); +private: + static const char* UI_FILENAME; + static const double QUANTIZATIONBITS_DEFAULT; + static const double QUANTIZATIONBITS_MIN; + static const double QUANTIZATIONBITS_MAX; + Gtk::Window* mPtrMainWin; + Gtk::Entry* mPtrInputFile; + Gtk::Entry* mPtrOutputFile; + Gtk::SpinButton* mPtrQuantizationBits; + Gtk::Button* mPtrOpenInputBtn; + Gtk::Button* mPtrOpenOutputBtn; + Gtk::Button* mPtrStartBtn; + + Gtk::Adjustment mQuantizationBitsAdj; + QuantizationParameters mParams; + + void on_open_input_btn(); + void on_open_output_btn(); + void on_start_btn(); + void on_quantizationbits_changed(); + void choose_file(std::string&); +}; + +#endif /* QUANTIZATIONCONTROLLER_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationParameters.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationParameters.h new file mode 100644 index 0000000..06be668 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/QuantizationParameters.h @@ -0,0 +1,55 @@ +/* + * QuantizationParameters.h + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef QUANTIZATIONPARAMETERS_H_ +#define QUANTIZATIONPARAMETERS_H_ + +#ifndef _GLIBCXX_STRING + #include <string> +#endif + +class QuantizationParameters +{ +public: +private: + std::string mInputFile; + std::string mOutputFile; + int mBits; +public: + int getBits() const + { + return mBits; + } + + std::string getInputFile() const + { + return mInputFile; + } + + std::string getOutputFile() const + { + return mOutputFile; + } + + void setBits(int mBits) + { + this->mBits = mBits; + } + + void setInputFile(std::string& mInputFile) + { + this->mInputFile = mInputFile; + } + + void setOutputFile(std::string& mOutputFile) + { + this->mOutputFile = mOutputFile; + } + +}; + +#endif /* QUANTIZATIONPARAMETERS_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/main.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/main.cpp new file mode 100644 index 0000000..7257265 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/main.cpp @@ -0,0 +1,22 @@ +/* + * main.cpp + * + * Created on: 02.04.2011 + * Author: sven + */ + +#ifndef QUANTIZATIONCONTROLLER_H_ + #include "QuantizationController.h" +#endif + +#ifndef _GTKMM_H + #include <gtkmm.h> +#endif + +int main(int argc, char* argv[]) +{ + Gtk::Main kit(argc,argv); + QuantizationController quantCtrl; + quantCtrl.startApp(kit); + return 0; +} |
