summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src')
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.cpp129
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppController.h40
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTAppParameters.h38
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.cpp167
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/DFTProcessor.h62
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/Util.h89
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/DFTApp/src/main.cpp21
7 files changed, 546 insertions, 0 deletions
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 <libglademm/xml.h>
+#endif
+
+#include <iostream>
+
+// because of static
+const char* DFTAppController::UI_FILENAME = "ui/dftapp.glade";
+
+DFTAppController::DFTAppController() {
+ try {
+ Glib::RefPtr<Gtk::Builder> 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<double>& inputData = dftProc.getInputData();
+ const std::vector<double>& Re = dftProc.getRe();
+ const std::vector<double>& Im = dftProc.getIm();
+ std::vector<double>::const_iterator inIter = inputData.begin();
+ std::vector<double>::const_iterator reIter = Re.begin();
+ std::vector<double>::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<Gtk::Builder> 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 <gtkmm.h>
+#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<Gtk::TextBuffer> 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 <string>
+#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 <iostream>
+#endif
+
+#ifndef _GLIBCXX_FSTREAM
+ #include <fstream>
+#endif
+
+#include <cmath>
+#include <string.h>
+#include <stdlib.h>
+#include <iomanip>
+
+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<mParams.getNumSamples();i++)
+ {
+ inFile.read((char*) &data,sizeof(short));
+ mInputData.at(i) = (double) data;
+ origFile.write((char*) &data, sizeof(short));
+ if (i < 5) {
+ if (i != 0) {
+ std::cout << std::endl;
+ }
+ std::cout << mInputData.at(i);
+ }
+ }
+ inFile.close();
+ origFile.close();
+
+ double nd,id,Nd,xn;
+ Nd = (double) mParams.getNumSamples();
+ for (unsigned i = 0; i < mNumDftSamples; i++) {
+ id = (double) i;
+ mRe.at(i) = 0.0;
+ mIm.at(i) = 0.0;
+ for (unsigned n=0; n < mParams.getNumSamples(); n++) {
+ nd = (double) n;
+ xn = mInputData.at(n);
+ mRe.at(i) += xn * std::cos(2.0 * PI * nd * id / Nd );
+ mIm.at(i) += xn * std::sin(2.0 * PI * nd * id / Nd );
+ }
+ mIm.at(i) = -mIm.at(i);
+ }
+ std::cout << std::endl << "dft done" << std::endl;
+
+ // idft
+ double rex,imx,fd;
+ std::cout << "idft file:" << std::endl;
+ for (unsigned n=0 ; n<mParams.getNumSamples() ; n++)
+ {
+ nd = (double) n;
+ double reSum = 0.0;
+ double imSum = 0.0;
+ for (unsigned f=0; f < mNumDftSamples; f++)
+ {
+ fd = (double) f;
+ if (f == 0) {
+ rex = mRe.at(0) / Nd;
+ } else if (f == mNumDftSamples) {
+ rex = mRe.at(mNumDftSamples) / Nd;
+ } else {
+ rex = mRe.at(f) / (Nd / 2.0);
+ }
+ imx = - mIm.at(f) / (Nd / 2.0);
+ reSum += rex * std::cos(2.0 * PI * nd * fd / Nd);
+ imSum += imx * std::sin(2.0 * PI * nd * fd / Nd);
+ }
+ mIdft.at(n) = reSum + imSum;
+ }
+ for (unsigned n=0 ; n<mParams.getNumSamples(); n++) {
+ data = (short) round(mIdft.at(n));
+ idftFile.write((char*) &data, sizeof(short));
+ if (n < 5) {
+ if (n != 0) {
+ std::cout << std::endl;
+ }
+ double dout = mIdft.at(n);
+ std::cout << std::setprecision(16) << dout;
+ }
+ }
+ idftFile.close();
+ std::cout << std::endl << "idft done" << std::endl;
+
+ // amplitude spectrum
+ double A1, A2;
+ unsigned maxTimeDomFreq = mSampleRate / 2;
+ unsigned freqIntervalWidth = maxTimeDomFreq / mNumDftSamples;
+ for (unsigned f = 0 ; f < mNumDftSamples ; f++) {
+ A1 = mIm.at(f);
+ A2 = mRe.at(f);
+ SpectralData actSpecData;
+ actSpecData.mAmplitude = std::sqrt( (A1*A1) + (A2*A2) );
+ actSpecData.mPhase = std::atan(A2/A1);
+ actSpecData.mFreq = f * freqIntervalWidth;
+ mSpectrum.push_back(actSpecData);
+ }
+ writeSpectrumToCSV();
+ startGnuPlot();
+}
+
+DFTProcessor::~DFTProcessor() {
+ // TODO Auto-generated destructor stub
+}
+
+void DFTProcessor::writeSpectrumToCSV()
+{
+ std::ofstream csvFile;
+ csvFile.open("spectrum.csv", std::ios::out);
+ std::vector<SpectralData>::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 <vector>
+
+struct SpectralData
+{
+ unsigned mFreq;
+ double mAmplitude;
+ double mPhase;
+};
+
+class DFTProcessor {
+public:
+ DFTProcessor(DFTAppParameters&);
+ virtual ~DFTProcessor();
+ const std::vector<double>& getInputData() const
+ {
+ return mInputData;
+ }
+ const std::vector<double>& getRe() const
+ {
+ return mRe;
+ }
+ const std::vector<double>& getIm() const
+ {
+ return mIm;
+ }
+ const std::vector<double>& getIdft() const
+ {
+ return mIdft;
+ }
+ const std::vector<SpectralData>& getSpectrum() const
+ {
+ return mSpectrum;
+ }
+private:
+ DFTAppParameters& mParams;
+ unsigned mNumDftSamples;
+ unsigned mSampleRate;
+ std::vector<double> mInputData;
+ std::vector<double> mRe;
+ std::vector<double> mIm;
+ std::vector<double> mIdft;
+ std::vector<SpectralData> 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 <sstream>
+#endif
+
+#include <iomanip>
+
+#ifndef _GLIBMM_USTRING_H
+ #include <glibmm/ustring.h>
+#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 <gtkmm.h>
+#endif
+
+int main(int argc,char* argv[])
+{
+ Gtk::Main kit(argc,argv);
+ DFTAppController dftAppController;
+ dftAppController.startApp(kit);
+ return 0;
+}