Browse Source

Klappt wohl

master
pauline 5 years ago
parent
commit
798f1fde0a
1 changed files with 28 additions and 20 deletions
  1. 48
      abgabe6nochmal.cpp

48
abgabe6nochmal.cpp

@ -4,7 +4,6 @@
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
#include <complex> #include <complex>
>
#include<vector> #include<vector>
using namespace std; using namespace std;
#ifndef M_PI #ifndef M_PI
@ -13,14 +12,16 @@ using namespace std;
typedef struct { typedef struct {
long N; long N;
double b, a, beta, alpha; double b, a, beta, alpha;
double tmin, tmax,h;
double tmin, tmax,h,tl;
int m; int m;
int sign; int sign;
} fft_param; } fft_param;
complex<double> expo(fft_param *fp,int k) { complex<double> expo(fft_param *fp,int k) {
int sign = fp->sign; int sign = fp->sign;
return exp(sign*2 * M_PI * 1i / ((double) k));
int N=fp->N;
return exp(sign*2.0 * M_PI * 1i* ((double)(k/N)));
} }
vector<complex<double>> fourierTrafo(fft_param *fp, vector<complex<double>> x) { vector<complex<double>> fourierTrafo(fft_param *fp, vector<complex<double>> x) {
@ -38,13 +39,19 @@ vector<complex<double>> fourierTrafo(fft_param *fp, vector<complex<double>> x) {
ungerPkt[k] = x[2 * k + 1]; ungerPkt[k] = x[2 * k + 1];
} }
vector<complex<double>> ftger(N / 2) = fourierTrafo(fp, gerPkt); //???????????????????????
vector<complex<double>> ftunger(N / 2) = fourierTrafo(fp, ungerPkt); //???????????????????
vector<complex<double>> ftger = fourierTrafo(fp, gerPkt);
vector<complex<double>> ftunger = fourierTrafo(fp, ungerPkt);
vector<complex<double>> out(N); vector<complex<double>> out(N);
for (int k = 0; k < N / 2; k++) { for (int k = 0; k < N / 2; k++) {
out[k] = (ftger[k] + ftunger[k] * expo(fp,k))/(double)N;
out[k + N / 2] = (ftger[k] - ftunger[k] *expo(fp,k))/(double)N;
if(sign==-1){
out[k] = (ftger[k] + ftunger[k] * expo(fp,k));
out[k + N / 2] = (ftger[k] - ftunger[k] *expo(fp,k));
}
else{
out[k] = (ftger[k] + ftunger[k] * expo(fp,k));
out[k + N / 2] = (ftger[k] - ftunger[k] *expo(fp,k));
}
} }
return out; return out;
@ -60,15 +67,15 @@ vector<complex<double>> test(fft_param *fp){
vector<complex<double>> x(N); vector<complex<double>> x(N);
for(int k=0;k<N;k++){ for(int k=0;k<N;k++){
x(k)=sin(tmin+k*h);
x[k]=sin(tmin+k*h);
} }
vector<complex<double>> out = fourierTrafo(fp, x); vector<complex<double>> out = fourierTrafo(fp, x);
fft_param n= *fp; fft_param n= *fp;
n.sign=-1; n.sign=-1;
vector<complex<double>> doppelout = fourierTrafo(&n, out); vector<complex<double>> doppelout = fourierTrafo(&n, out);
FILE *fp2 = fopen("testFFT.csv" ,'w');
FILE *fp2 = fopen("testFFT.csv" ,"w");
for(int k=0;k<N;k++){ for(int k=0;k<N;k++){
fprintf(fp2,"%f\t%f\t%f\n",x[k], doppelout[k],(tmin+k*h));
fprintf(fp2,"%f\t%f\t%f\n",(tmin+k*h),x[k].real(), doppelout[k].real()/(double)N);
} }
return x; return x;
@ -79,7 +86,7 @@ vector<complex<double>> makeS(fft_param *fp){
double tmin=0; double tmin=0;
double t; double t;
double tl=fp->tl; double tl=fp->tl;
double alpha=f->alpha;
double alpha=fp->alpha;
int N=fp->N; int N=fp->N;
vector<complex<double>> s(N); vector<complex<double>> s(N);
for(int k=0;k<N;k++){ for(int k=0;k<N;k++){
@ -93,7 +100,7 @@ vector<complex<double>> makeE(fft_param *fp){
double tmax=fp->tmax; double tmax=fp->tmax;
double tmin=0; double tmin=0;
double t; double t;
double beta=f->beta;
double beta=fp->beta;
double tl=fp->tl; double tl=fp->tl;
double a=fp->a; double a=fp->a;
double b=fp->b; double b=fp->b;
@ -102,7 +109,8 @@ vector<complex<double>> makeE(fft_param *fp){
vector<complex<double>> s=makeS(fp); vector<complex<double>> s=makeS(fp);
for(int k=0;k<N;k++){ for(int k=0;k<N;k++){
t=tmin+k*h; t=tmin+k*h;
e[k]=b*sin(2*M_PI*beta*t)+a*rand(1)+s[k];
e[k]=b*sin(2*M_PI*beta*t)+ 2*a*(rand()-0.5)+s[k];
//e[k]=b*sin(2*M_PI*beta*t)+s[k];
} }
return e; return e;
} }
@ -112,23 +120,23 @@ vector<complex<double>> aufgabe3 (fft_param *fp){
double tmax=fp->tmax; double tmax=fp->tmax;
double t=tmin; double t=tmin;
int N=fp->N; int N=fp->N;
vector<complex<double>> e(N)=makeE;
vector<complex<double>> s(N)=makeS(fp);
vector<comlex<double>> Fe=fourierTrafo(fp,e);
vector<complex<double>> e=makeE(fp);
vector<complex<double>> s=makeS(fp);
vector<complex<double>> Fe=fourierTrafo(fp,e);
fft_param fps=*fp; fft_param fps=*fp;
fps.sign=-1; fps.sign=-1;
vector<comlex<double>> Fs=fourierTrafo(&fps,s);
vector<comlex<double>> FFes(N);
vector<complex<double>> Fs=fourierTrafo(&fps,s);
vector<complex<double>> FFes(N);
for(int k=0;k<N;k++){ for(int k=0;k<N;k++){
FFes[k]=Fe[k]*conj(Fs[k]); FFes[k]=Fe[k]*conj(Fs[k]);
} }
vector<comlex<double>> FFFes(N)=fourierTrafo(fp, FFes);
vector<complex<double>> FFFes=fourierTrafo(fp, FFes);
return FFFes; return FFFes;
} }
double main(){
int main(){
fft_param fp; fft_param fp;
fp.N= pow(2,13); fp.N= pow(2,13);
fp.tmin=0; fp.tmin=0;

Loading…
Cancel
Save