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

Merge branch 'FFTW' into 'main'

Städade upp lite

See merge request !4
parents 86646480 09d8a4ce
No related branches found
No related tags found
1 merge request!4Städade upp lite
......@@ -7,46 +7,16 @@
#include <string>
#include <bits/stdc++.h>
using namespace std::complex_literals;
// PI
const double PI{std::acos(-1)};
int main()
// Alg start----------------------------------------------------------------------------------------------------
std::vector<std::complex<double>> fft(std::vector<std::complex<double>> inputVector,std::vector<std::complex<double>> returnVector,int n,double flag)
{
// PI
const double PI{std::acos(-1)};
// inFile innehåller uHatt data och outFile är resulterande filen
std::string inFile{"sinus.txt"};
std::string outFile{"sinusFFT.txt"};
// Perioden för funktionen
double T{2*PI};
int const n{16};
std::vector<std::complex<double>> inputVector;
std::complex<double> funcTransf[n];
// Hämtar data och sätter den på en vector
int loop = 0;
std::string dataLine;
std::ifstream functionData (inFile);
std::complex<double> element;
if (functionData.is_open())
{
while ((std::getline (functionData,dataLine)) && (loop < n) )
{
std::istringstream is(dataLine);
is >> element;
inputVector.push_back(element);
loop++;
}
functionData.close();
}
// alg start -------------------------------------------------------------
std::complex<double> my[n];
for(int i=0;i<n;i++)
{
my[i]=std::exp(-1i*T*static_cast<double> (i)/static_cast<double> (n));
my[i]=std::exp(flag*1i*2.0*PI*static_cast<double> (i)/static_cast<double> (n));
}
......@@ -58,8 +28,8 @@ int main()
// F2 mult.
for(int i=0;i<n;i+=2)
{
funcTransf[i]=inputVector[i]+inputVector[i+1];
funcTransf[i+1]=inputVector[i]-inputVector[i+1];
returnVector[i]=inputVector[i]+inputVector[i+1];
returnVector[i+1]=inputVector[i]-inputVector[i+1];
}
// I and D mult.
......@@ -70,29 +40,91 @@ int main()
{
for(int j=0;j<n;j+=4)
{
s = s + funcTransf[j] + std::pow(my[m],m) * funcTransf[j+2];
s = s + returnVector[j] + std::pow(my[m],m) * returnVector[j+2];
}
}
else
{
for(int j=1;j<n;j+=4)
{
s = s + funcTransf[j] + std::pow(my[m],m) * funcTransf[j+2];
s = s + returnVector[j] + std::pow(my[m],m) * returnVector[j+2];
}
}
funcTransf[m]= s / static_cast<double> (std::sqrt(n));
returnVector[m]= s / static_cast<double> (std::sqrt(n));
}
return returnVector;
}
// Alg slut----------------------------------------------------------------------------------------------------
int main()
{
FFT call;
int const n{16};
// inFile innehåller uHatt data och outFile är resulterande filen
std::string inFile{"sinus.txt"};
std::string outFile{"inversSinusFFT.txt"};
std::vector<std::complex<double>> inputVector = call.getInput(inFile,n);
std::complex<double> funcTransf[n];
// Skapar två vektorer av storlek n med nollor i sig
std::vector<std::complex<double>> returnVector1;
std::vector<std::complex<double>> returnVector2;
for(int i=0;i<n;i++)
{
returnVector1.push_back(0);
returnVector2.push_back(0);
}
// Anroppar alg -------------------------------------------------------------
std::vector<std::complex<double>> transf = fft(inputVector,returnVector1,n,-1);
std::vector<std::complex<double>> invTransf = fft(transf,returnVector2,n,1);
// ---------------------------------------------------------------------------
call.printToFile(invTransf,outFile,n);
std::cout << "FFT done"<<'\n';
return 0;
}
// Skriva/läsa-----------------------------------------------------------------------------
std::vector<std::complex<double>> FFT::getInput(std::string inFile, int n)
{
std::vector<std::complex<double>> inputVector;
// Hämtar data och sätter den på en vector
int loop = 0;
std::string dataLine;
std::ifstream functionData (inFile);
std::complex<double> element;
if (functionData.is_open())
{
while ((std::getline (functionData,dataLine)) && (loop < n) )
{
std::istringstream is(dataLine);
is >> element;
inputVector.push_back(element);
loop++;
}
functionData.close();
}
// Alg end ----------------------------------------------------------------
return inputVector;
}
void FFT::printToFile(std::vector<std::complex<double>> printVector, std::string fileName, int n)
{
// Skriver ut inversFuncTransf till en fil
std::ofstream resultFile;
resultFile.open(outFile);
resultFile.open(fileName);
// Sätter önskad antal decimaler
resultFile << std::fixed << std::setprecision(10);
for (int i=0;i<n;i++)
{
resultFile << funcTransf[i].real() << "," << funcTransf[i].imag() << '\n';
resultFile << printVector[i].real() << "," << printVector[i].imag() << '\n';
}
resultFile.close();
std::cout << "FFT done"<<'\n';
return 0;
}
\ No newline at end of file
......@@ -13,7 +13,7 @@
int main()
{
// Antal punkter som ska transformeras/inverstransformeras
int const n=20;
int const n=16;
// Input funktions data
fftw_complex functionData[n];
......
#include <complex>
#include <vector>
#include <string>
class FFT
{
public:
void printToFile(std::vector<std::complex<double>> printVector, std::string fileName, int n);
std::vector<std::complex<double>> getInput(std::string inFile, int n);
private:
};
File moved
......@@ -26,7 +26,7 @@ std::vector<std::complex<double>> fftrec(std::vector<std::complex<double>> f,int
{
std::vector<std::complex<double>> a1;
std::vector<std::complex<double>> b1;
for(int i=0;i<=N;i++)
for(int i=0;i<=N-1;i++)
{
if(i%2==0)
{
......
No preview for this file type
2.6144470127,-0.2600135238
-0.2468330940,0.4963457911
1.8887400240,1.2449675942
-0.4467231138,0.0521004861
3.2597044536,0.0986068136
-0.2983092174,0.2022103797
2.0395450203,1.5938019973
-0.7032953600,0.4366120951
3.9948384450,0.8602987942
-0.9557205768,0.3536651993
2.0439507792,1.9582034439
-0.0042324440,0.1903385069
4.7053747961,1.4958153924
0.2336226209,0.1877846232
2.3712891621,2.3812651524
-0.9055292843,0.3578415425
0,0
0.841471,1.67914e-19
0.909297,-6.86156e-18
0.14112,-1.42613e-17
0.841471,-7.51373e-17
0.909297,-2.45327e-18
0.14112,-1.96262e-17
-0.756802,0
-0.958924,1.11022e-17
-0.279415,1.79638e-17
0.656987,-3.70247e-18
-0.958924,1.96262e-17
-0.279415,2.45327e-18
0.656987,-3.5885e-17
0.989358,0
0.412118,6.69364e-18
-0.544021,-2.22045e-17
-0.99999,1.67914e-19
0.412118,3.5885e-17
-0.544021,2.45327e-18
-0.99999,-1.96262e-17
-0.536573,0
0.420167,-1.42613e-17
0.990607,1.79638e-17
0.650288,1.11022e-17
-0.287903,0
-0.961397,-3.70247e-18
-0.750987,-6.86156e-18
0.149877,6.69364e-18
0.420167,1.96262e-17
0.990607,-2.45327e-18
0.650288,7.51373e-17
9.1398206044,-5.4536844553
9.6809334767,-2.9346356508
-4.8533132756,5.9696298177
3.1631893166,-3.1673031952
-1.0681141449,-3.9889275745
2.5085516570,4.2659503988
-2.0451485834,-2.2295090922
-0.9188801268,0.4380280064
-0.9681317764,-0.3772564925
-9.1997883841,3.8848288233
-1.0035716440,-21.6139703680
10.2159678974,8.3013102465
0.3726256441,0.3068522515
-12.6332527731,5.2140573888
-3.2499819989,-13.7342402161
4.0199196309,-4.8413742979
-0.6806533169,-2.1294354386
-0.1269205667,-0.7429836014
-0.6806533169,-0.2032087431
-0.1269205667,-0.5181073468
-0.6806533169,0.3420809383
-0.1269205667,-0.5126149384
-0.6806533169,1.0322190424
-0.1269205667,-0.7996013102
-0.6806533169,3.9315113747
-6.8586884451,-3.2520124213
6.5937260977,-4.5170493085
-1.2558806312,8.7942247812
-6.7350732794,-5.8063964610
7.5444736306,-1.0391267346
-2.9159773025,7.3502785957
-5.3828133564,-1.9297784284
......@@ -14,7 +14,3 @@
0.4201670368266409
0.9906073556948704
0.6502878401571168
-0.2879033166650653
-0.9613974918795568
-0.750987246771676
0.14987720966295234
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