From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../Aufgabenblatt1/DFTApp/src/DFTAppController.cpp | 129 ++++++++++++++++ .../Aufgabenblatt1/DFTApp/src/DFTAppController.h | 40 +++++ .../Aufgabenblatt1/DFTApp/src/DFTAppParameters.h | 38 +++++ .../Aufgabenblatt1/DFTApp/src/DFTProcessor.cpp | 167 +++++++++++++++++++++ .../Aufgabenblatt1/DFTApp/src/DFTProcessor.h | 62 ++++++++ .../Aufgabenblatt1/DFTApp/src/Util.h | 89 +++++++++++ .../Aufgabenblatt1/DFTApp/src/main.cpp | 21 +++ 7 files changed, 546 insertions(+) create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.cpp create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppParameters.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.cpp create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/Util.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/main.cpp (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src') diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.cpp new file mode 100644 index 0000000..b66c987 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.cpp @@ -0,0 +1,129 @@ +/* + * DFTAppController.cpp + * + * Created on: 29.04.2011 + * Author: sven + */ +#ifndef DFTAPPCONTROLLER_H_ + #include "DFTAppController.h" +#endif + +#ifndef DFTPROCESSOR_H_ + #include "DFTProcessor.h" +#endif + +#ifndef UTIL_H_ + #include "Util.h" +#endif + +#ifndef _LIBGLADEMM_XML_H + #include +#endif + +#include + +// because of static +const char* DFTAppController::UI_FILENAME = "ui/dftapp.glade"; + +DFTAppController::DFTAppController() { + try { + Glib::RefPtr builder = Gtk::Builder::create_from_file(UI_FILENAME); + builder->get_widget("mainwindow",mPtrMainWin); + builder->get_widget("inputfileentry",mPtrInputFileEntry); + builder->get_widget("numsamplesentry",mPtrNumSamplesEntry); + builder->get_widget("startbtn",mPtrStartBtn); + builder->get_widget("openinputfilebtn",mPtrOpenBtn); + builder->get_widget("textview1",mPtrTextView); + } catch (Gtk::BuilderError err) { + std::cout << "Builder Error: " << err.code() << std::endl; + } + mRefPtrTextBuffer = Gtk::TextBuffer::create(); + mPtrTextView->set_buffer(mRefPtrTextBuffer); + mPtrMainWin->signal_hide().connect(sigc::ptr_fun(&Gtk::Main::quit)); + mPtrOpenBtn->signal_clicked().connect(sigc::mem_fun(this,&DFTAppController::on_button_open)); + mPtrStartBtn->signal_clicked().connect(sigc::mem_fun(this,&DFTAppController::on_button_start)); + mPtrNumSamplesEntry->signal_changed().connect(sigc::mem_fun(this,&DFTAppController::on_numsamples_changed)); + on_numsamples_changed(); +} + +DFTAppController::~DFTAppController() { + // TODO Auto-generated destructor stub +} + +void DFTAppController::startApp(Gtk::Main& kit) +{ + kit.run(*mPtrMainWin); + return; +} + +void DFTAppController::on_button_start() +{ + DFTProcessor dftProc(mParams); + std::string text = "Idx\tInput\tRe\tIm\n"; + const std::vector& inputData = dftProc.getInputData(); + const std::vector& Re = dftProc.getRe(); + const std::vector& Im = dftProc.getIm(); + std::vector::const_iterator inIter = inputData.begin(); + std::vector::const_iterator reIter = Re.begin(); + std::vector::const_iterator imIter = Im.begin(); + unsigned idx = 0; + for (;inIter != inputData.end(); inIter++) + { + if (inIter != inputData.begin()) { + text += "\n"; + } + text += Util::ToUString(idx); + text += "\t"; + text += Util::ToUString((int) *inIter); + if (idx < mParams.getNumSamples() / 2) { + text += "\t"; + text += Util::ToUString(*reIter); + text += "\t"; + text += Util::ToUString(*imIter); + } + reIter++; + imIter++; + idx++; + } + mRefPtrTextBuffer->set_text(text); + std::cout << mRefPtrTextBuffer->get_line_count() << " lines" << std::endl; +} + +void DFTAppController::on_button_open() +{ + std::string filename; + Gtk::FileChooserDialog* ptrFilechooser; + try { + Glib::RefPtr builder = Gtk::Builder::create_from_file(UI_FILENAME); + builder->get_widget("filechooserdlg",ptrFilechooser); + } catch (Gtk::BuilderError err) { + std::cout << "Builder Error: " << err.code() << std::endl; + } + 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(); + mParams.setInputFilename(filename); + mPtrInputFileEntry->set_text(mParams.getInputFilename()); + break; + default: + break; + } + delete ptrFilechooser; +} + +void DFTAppController::on_numsamples_changed() +{ + unsigned numSamples; + Glib::ustring numSamplesStr = mPtrNumSamplesEntry->get_text(); + numSamples = Util::ToUint(numSamplesStr); + mParams.setNumSamples(numSamples); +} diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.h new file mode 100644 index 0000000..0b2f22e --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.h @@ -0,0 +1,40 @@ +/* + * DFTAppController.h + * + * Created on: 29.04.2011 + * Author: sven + */ + +#ifndef DFTAPPCONTROLLER_H_ +#define DFTAPPCONTROLLER_H_ + +#ifndef _GTKMM_H + #include +#endif + +#ifndef DFTAPP_PARAMETERS_H_ + #include "DFTAppParameters.h" +#endif + +class DFTAppController { +public: + DFTAppController(); + virtual ~DFTAppController(); + void startApp(Gtk::Main&); +private: + static const char* UI_FILENAME; + DFTAppParameters mParams; + Gtk::Window* mPtrMainWin; + Gtk::Button* mPtrStartBtn; + Gtk::Button* mPtrOpenBtn; + Gtk::Entry* mPtrInputFileEntry; + Gtk::Entry* mPtrNumSamplesEntry; + Gtk::TextView* mPtrTextView; + Glib::RefPtr mRefPtrTextBuffer; + + void on_button_start(); + void on_button_open(); + void on_numsamples_changed(); +}; + +#endif /* DFTAPPCONTROLLER_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppParameters.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppParameters.h new file mode 100644 index 0000000..32c68fe --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppParameters.h @@ -0,0 +1,38 @@ +#ifndef DFTAPP_PARAMETERS_H_ +#define DFTAPP_PARAMETERS_H_ + +#ifndef _GLIBCXX_STRING + #include +#endif + +class DFTAppParameters +{ +public: + +private: + std::string mInputFilename; + unsigned mNumSamples; +public: + const std::string& getInputFilename() const + { + return mInputFilename; + } + + unsigned getNumSamples() const + { + return mNumSamples; + } + + void setInputFilename(std::string& mInputFilename) + { + this->mInputFilename = mInputFilename; + } + + void setNumSamples(unsigned mNumSamples) + { + this->mNumSamples = mNumSamples; + } + +}; + +#endif //DFTAPP_PARAMETERS_H_ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.cpp new file mode 100644 index 0000000..97571f9 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.cpp @@ -0,0 +1,167 @@ +/* + * DFTProcessor.cpp + * + * Created on: 29.04.2011 + * Author: sven + */ +#ifndef DFTPROCESSOR_H_ + #include "DFTProcessor.h" +#endif + +#ifndef _GLIBCXX_IOSTREAM + #include +#endif + +#ifndef _GLIBCXX_FSTREAM + #include +#endif + +#include +#include +#include +#include + +const double PI = 3.14159265358979323846264338327950288419716939; + +const char* ORIG_FILENAME = "orig.wav"; +const char* IDFT_FILENAME = "idft.wav"; + +DFTProcessor::DFTProcessor(DFTAppParameters& params) +:mParams(params) +,mNumDftSamples(mParams.getNumSamples() / 2) +,mInputData(mParams.getNumSamples()) +,mRe(mNumDftSamples) +,mIm(mNumDftSamples) +,mIdft(mParams.getNumSamples()) +{ + std::cout << "DFT processing " << mParams.getNumSamples() << + " samples of" << mParams.getInputFilename() << std::endl; + std::ifstream inFile; + std::ofstream origFile; + std::ofstream idftFile; + inFile.open(mParams.getInputFilename().c_str(), std::ios::in | std::ios::binary); + origFile.open(ORIG_FILENAME, std::ios::out | std::ios::binary); + idftFile.open(IDFT_FILENAME, std::ios::out | std::ios::binary); + int dataStartOffset = 0x2C; + char header[dataStartOffset]; + inFile.read(&header[0],dataStartOffset); + memcpy(&mSampleRate,((&header[0])+0x18),4); + origFile.write((char*) &header[0],dataStartOffset); + idftFile.write((char*) &header[0],dataStartOffset); + short data; + std::cout << "Original file:" << std::endl; + for (unsigned i=0;i::const_iterator specIter; + for(specIter = mSpectrum.begin() ; specIter != mSpectrum.end() ; specIter++) { + const SpectralData& actSpec = *specIter; + if (specIter != mSpectrum.begin()) { + csvFile << std::endl; + } + csvFile << actSpec.mFreq << ";"; + csvFile << std::setprecision (12) << std::fixed << actSpec.mAmplitude; + csvFile << ";"; + csvFile << std::setprecision (12) << std::fixed << actSpec.mPhase; + } + csvFile.close(); +} + +void DFTProcessor::startGnuPlot() +{ + int i = system("gnuplot spectrum.plt -p"); + if (i != 0) { + std::cerr << "Error starting gnuplot: " << i << std::endl; + } +} diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.h new file mode 100644 index 0000000..96ef46e --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.h @@ -0,0 +1,62 @@ +/* + * DFTProcessor.h + * + * Created on: 29.04.2011 + * Author: sven + */ + +#ifndef DFTPROCESSOR_H_ +#define DFTPROCESSOR_H_ + +#ifndef DFTAPP_PARAMETERS_H_ + #include "DFTAppParameters.h" +#endif + +#include + +struct SpectralData +{ + unsigned mFreq; + double mAmplitude; + double mPhase; +}; + +class DFTProcessor { +public: + DFTProcessor(DFTAppParameters&); + virtual ~DFTProcessor(); + const std::vector& getInputData() const + { + return mInputData; + } + const std::vector& getRe() const + { + return mRe; + } + const std::vector& getIm() const + { + return mIm; + } + const std::vector& getIdft() const + { + return mIdft; + } + const std::vector& getSpectrum() const + { + return mSpectrum; + } +private: + DFTAppParameters& mParams; + unsigned mNumDftSamples; + unsigned mSampleRate; + std::vector mInputData; + std::vector mRe; + std::vector mIm; + std::vector mIdft; + std::vector mSpectrum; + + void writeSpectrumToCSV(); + void startGnuPlot(); +}; + +#endif /* DFTPROCESSOR_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/Util.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/Util.h new file mode 100644 index 0000000..c1f7130 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/Util.h @@ -0,0 +1,89 @@ +/* + * Util.h + * + * Created on: 31.03.2011 + * Author: sven + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +#ifndef _GLIBCXX_SSTREAM + #include +#endif + +#include + +#ifndef _GLIBMM_USTRING_H + #include +#endif + +class Util +{ +public: + static Glib::ustring ToUString(unsigned short val) + { + std::ostringstream ssIn; + ssIn << val; + Glib::ustring res = ssIn.str(); + return res; + } + + static Glib::ustring ToUString(short val) + { + std::ostringstream ssIn; + ssIn << val; + Glib::ustring res = ssIn.str(); + return res; + } + + static Glib::ustring ToUString(unsigned val) + { + std::ostringstream ssIn; + ssIn << val; + Glib::ustring res = ssIn.str(); + return res; + } + + static Glib::ustring ToUString(int val) + { + std::ostringstream ssIn; + ssIn << val; + Glib::ustring res = ssIn.str(); + return res; + } + + static Glib::ustring ToUString(double val) + { + std::ostringstream ssIn; + ssIn << std::setprecision(12) << std::fixed << val; + Glib::ustring res = ssIn.str(); + return res; + } + + static unsigned short ToUShort(Glib::ustring& val) + { + std::istringstream buffer(val.raw()); + unsigned short res; + buffer >> res; + return res; + } + + static unsigned ToUint(Glib::ustring& val) + { + std::istringstream buffer(val.raw()); + unsigned res; + buffer >> res; + return res; + } + + static short ToShort(Glib::ustring& val) + { + std::istringstream buffer(val.raw()); + short res; + buffer >> res; + return res; + } +}; + +#endif /* UTIL_H_ */ diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/main.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/main.cpp new file mode 100644 index 0000000..0791767 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/main.cpp @@ -0,0 +1,21 @@ +/* + * main.cpp + * + * Created on: 29.04.2011 + * Author: sven + */ +#ifndef DFTAPPCONTROLLER_H_ + #include "DFTAppController.h" +#endif + +#ifndef _GTKMM_H + #include +#endif + +int main(int argc,char* argv[]) +{ + Gtk::Main kit(argc,argv); + DFTAppController dftAppController; + dftAppController.startApp(kit); + return 0; +} -- cgit v1.2.3