Skip to content
Snippets Groups Projects
Commit 38161e81 authored by Kristoffer Tondel's avatar Kristoffer Tondel
Browse files

Merge branch 'DFT' into 'main'

Dft

See merge request !1
parents 5d060b47 6e506fda
No related branches found
No related tags found
1 merge request!1Dft
#include <iomanip>
#include "Header files/AdaptivKvadratur.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <string>
using namespace std::complex_literals;
int main()
{
// PI
const double PI{std::acos(-1)};
// filnamnet
std::string filename{"sinusKoefficenter.txt"};
// obj för att komma åt funktioner, output är raderna som skrivs i filenamne
AdaptivKvadratur obj;
std::complex<double> output{};
// Antal datapunkter
int numDataPoints{30};
// Perioden för funktionen
double T{2*PI};
// Undre och övere intergration
double a{0};
double b{a+T};
// Tolerans och omega
double epsilon{0.067};
double omega{2.0*PI/T};
// Öppnar eller skapar filen
std::ofstream dataFile;
dataFile.open (filename);
// sätter önskad antal decimaler
dataFile << std::fixed << std::setprecision(10);
for (int n = 0; n<numDataPoints;n++)
{
output = obj.integrate(a,b,omega,n,epsilon);
dataFile << output*1.0/T << '\n';
}
dataFile.close();
std::cout << "done"<<'\n';
return 0;
}
AdaptivKvadratur::AdaptivKvadratur()
{
}
std::complex<double> AdaptivKvadratur::function(double a,double omega, int n)
{
std::complex<double> functionValue{};
functionValue = std::sin(a) * std::exp(-1i*a*omega*static_cast<double>(n));
return functionValue;
}
std::complex<double> AdaptivKvadratur::integrate(double a, double b, double omega,
int n, double epsilon)
{
std::complex<double> I1{};
std::complex<double> I2{};
std::complex<double> I3{};
double m{(a+b)/2.0};
double h{b-a};
I1 = h/6.0 * (function(a,omega,n)+4.0*function(m,omega,n)
+function(b,omega,n));
I2 = h/12.0*(function(a,omega,n) + 4.0 * function(h+1/4.0,omega,n)
+ 2.0 * function(m,omega,n) + 4.0 * function(b-h/4.0,omega,n)
+ function(b,omega,n));
I3 = I2+(I2-I1)/15.0;
if (std::abs((I3-I2)/I2) > epsilon)
{
I1 = integrate(a, m, omega, n, epsilon/2);
I2 = integrate(m, b, omega, n, epsilon/2);
I3 = I1 + I2;
}
return I3;
}
DFT.cc 0 → 100644
#include <complex>
#include <cmath>
#include <iomanip>
#include "Header files/DFT.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std::complex_literals;
int main()
{
// PI
const double PI{std::acos(-1)};
// inFile innehåller uHatt data och outFile är resulterande filen
std::string inFile{"sinusKoefficenter.txt"};
std::string outFile{"sinusFHat.txt"};
// Perioden för funktionen
double T{2*PI};
// Definerar omega,my och n. (n is the size of the vectors,matrix)
double omega{2.0*PI/T};
std::complex<double> my{std::exp(1i * omega)};
int const n{20};
std::complex<double> F[n][n];
std::complex<double> fHat[n];
std::complex<double> uHat[n];
std::string dataLine;
std::ifstream uHatData (inFile);
// while-loop variabel samt element i uHat
int loop = 0;
std::complex<double> uHatElement;
if (uHatData.is_open())
{
while (std::getline (uHatData,dataLine) )
{
std::istringstream is(dataLine);
is >> uHatElement;
uHat[loop] = uHatElement;
loop++;
}
uHatData.close();
}
// Behövs för att skriva få rätt format på raderna i filename2
std::ostringstream strConverter;
std::string output;
std::ofstream resultFile;
resultFile.open(outFile);
// Sätter önskad antal decimaler
resultFile << std::fixed << std::setprecision(10);
for(int i = 0; i<n; i++)
{
for(int j = 0; j<n; j++)
{
std::complex<double> exponent = static_cast<double> (i*j);
F[j][i] = std::pow(my,exponent);
fHat[i] = fHat[i] + F[j][i]*uHat[i];
// Skriver ut F matrisen
std::cout<<F[j][i]<<" ";
}
std::cout <<'\n';
// Tar bort paranteserna från komplexa talet. Då det blir lättare för PlotData.py
strConverter.str("");
strConverter << fHat[i];
output = strConverter.str().substr(1, strConverter.str().size() - 2);
// Skriver in fHat in i resultFile
resultFile << output << '\n';
}
resultFile.close();
std::cout << "done"<<'\n';
return 0;
}
DFT::DFT()
{
}
import math
# Genererar sinus värden för att jämföra med DFT
# välj filnamn
filename = 'sinus.txt'
# välj antal datapunkter
numDataPoints = 20
file = open(filename, 'w')
# file.write(omega+'\n')
for i in range(numDataPoints):
file.write(str(math.sin(i))+'\n')
pass
file.close()
print('done')
#include <complex>
#include <cmath>
class AdaptivKvadratur
{
public:
AdaptivKvadratur();
std::complex<double> integrate(double a, double b, double omega,
int n, double epsilon);
std::complex<double> function(double a,double omega, int n);
};
class DFT
{
public:
DFT();
private:
};
import cmath
# sinus
def integrate(a, b, omega, n, epsilon):
m = (a+b)/2
h = b-a
# e^(-i*t*n*omega) faktorerna
ae = cmath.exp(-a*omega*n*1j)
me = cmath.exp(-m*omega*n*1j)
be = cmath.exp(-b*omega*n*1j)
aeh4 = cmath.exp(-(a+h/4)*omega*n*1j)
beh4 = cmath.exp(-(b-h/4)*omega*n*1j)
I1 = h/3*(cmath.sin(a)*ae+4*cmath.sin(m)*me+cmath.sin(b)*be)
I2 = h/6*(cmath.sin(a)*ae+4*cmath.sin(a+h/4)*aeh4+2*cmath.sin(m)*me+4*cmath.sin(b-h/4)*beh4+cmath.sin(b)*be)
I3 = I2+(I2-I1)/15
if abs((I3-I2)/I2) > epsilon:
I1 = integrate(a, m, omega, n, epsilon/2)
I2 = integrate(m, b, omega, n, epsilon/2)
I3 = I1 + I2
pass
return I3
filename = 'sinusKoefficenter'
# välj antal datapunkter
numDataPoints = 20;
# välj period
T = 2*cmath.pi
# väl undre och övre integration
a = 0
b = T + a
# välj tolerans och omega
epsilon = 0.1
omega = 2*cmath.pi/T
file=open(filename,'w')
for i in range(numDataPoints):
file.write(str(i)+';'+str(integrate(a, b, omega, i, epsilon))+'\n')
pass
file.close()
print('done')
import matplotlib.pyplot as plt
import numpy as np
filename1 = 'sinus.txt'
filename2 = 'sinusFHat.txt'
# Tar fram data och plotar datan i filename1 mot x=1...len()
Y = np.loadtxt(filename1, unpack=True)
X = list(range(0,len((Y))))
plt.scatter(X, Y)
plt.plot(X, Y)
# Tar fram data och plotar datan i filename2 mot xHat=1...len()
YHat,Imaginary = np.loadtxt(filename2, delimiter =',',unpack=True)
XHat = list(range(0,len((YHat))))
plt.plot(XHat, YHat)
plt.scatter(XHat, YHat)
plt.title(filename1)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
print('done')
a.out 0 → 100755
File added
0.0
0.8414709848078965
0.9092974268256817
0.1411200080598672
-0.7568024953079282
-0.9589242746631385
-0.27941549819892586
0.6569865987187891
0.9893582466233818
0.4121184852417566
-0.5440211108893698
-0.9999902065507035
-0.5365729180004349
0.4201670368266409
0.9906073556948704
0.6502878401571168
-0.2879033166650653
-0.9613974918795568
-0.750987246771676
0.14987720966295234
-5.35179,0
0.128618,-0.419678
0.471065,0.0251305
-0.00337691,0.299632
-0.223093,-0.132737
0.0820078,-0.174555
-0.785837,-0.17028
-0.328153,0.495687
-0.365077,-0.378147
0.203529,-0.333906
0.128914,0.0825593
0.018315,-0.00991071
0.549578,0.737994
1.02951,-1.14958
0.161698,0.376965
-0.303066,0.0827178
-0.0466722,-0.0801996
-0.110187,0.0509128
0.022323,-0.675769
-5.82281,0.874292
(-0.2675897034,0.0000000000)
(0.0852312069,-0.3773186557)
(0.4327528535,-0.0421730761)
(0.0643636349,0.2955946212)
(-0.3080274028,-0.0740207123)
(0.0277376003,-0.4390337963)
(0.3617780138,-0.0877454965)
(-0.0156795662,0.2689983932)
(-0.3921622667,-0.0799871230)
(-0.0552578280,-0.4239994274)
(0.2850822747,-0.0526451122)
(-0.0813070464,0.3219824211)
(-0.4426410892,-0.0124137418)
(-0.0874494641,-0.3460380783)
(0.2731793450,0.0308569456)
(-0.0721812031,0.4058334340)
(-0.4130538737,0.0665727764)
(-0.0392404662,-0.2768270540)
(0.3370127234,0.0859892698)
(0.0033077054,0.4434591973)
(-0.3306029697,0.0843525910)
(0.0450460354,-0.2799986509)
(0.4178942962,0.0620634560)
(0.0757555249,0.4002667578)
(-0.2710933581,0.0245790224)
(0.0879174198,-0.3526369175)
(0.4414619183,-0.0189232131)
(0.0785540641,0.3159670451)
(-0.2892379013,-0.0577923860)
(0.0499579339,-0.4279585665)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment