You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

299 lines
8.6 KiB

//g++ abgabe6.cpp -std=c++14 -o abgabe6
// ./abgabe6
// Pauline Maas, Ariane Ufer
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <complex>
#include <vector>
using namespace std;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
typedef struct {
long n;
double b, a, beta, alpha;
double tmin, tmax,h,tl,signalLenght;
int m;
int sign;
} fft_param;
//Pseudo-Zufallszahlen-Generator
//Part of a C-program for MT19937, with initialization improved 2002/1/26.
//Coded by Takuji Nishimura and Makoto Matsumoto.
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
static unsigned long mt[N]; /* the array for the state vector */
static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
/* initializes mt[N] with a seed */
void init_genrand(unsigned long s)
{
mt[0]= s & 0xffffffffUL;
for (mti=1; mti<N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt[mti] &= 0xffffffffUL;
/* for >32 bit machines */
}
}
/* initialize by an array with array-length */
/* init_key is the array for initializing keys */
/* key_length is its length */
/* slight change for C++, 2004/2/26 */
void init_by_array(unsigned long init_key[], int key_length)
{
int i, j, k;
init_genrand(19650218UL);
i=1; j=0;
k = (N>key_length ? N : key_length);
for (; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j; /* non linear */
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++; j++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
if (j>=key_length) j=0;
}
for (k=N-1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i; /* non linear */
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
}
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}
/* generates a random number on [0,0xffffffff]-interval */
unsigned long genrand_int32(void)
{
unsigned long y;
static unsigned long mag01[2]={0x0UL, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= N) { /* generate N words at one time */
int kk;
if (mti == N+1) /* if init_genrand() has not been called, */
init_genrand(5489UL); /* a default initial seed is used */
for (kk=0;kk<N-M;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
for (;kk<N-1;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
mti = 0;
}
y = mt[mti++];
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
}
/* generates a random number on [0,1) with 53-bit resolution*/
double genrand_res53(void)
{
unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
return(a*67108864.0+b)*(1.0/9007199254740992.0);
}
// End of C-program for MT19937
complex<double> expo(fft_param *fp,int k) {
int sign = fp->sign;
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) {
long n = x.size(); //Länge des x vektors
int sign=fp->sign;
vector<complex<double>> gerPkt(n / 2); //Vektoren um x in gerade und ungerade Teile aufzuteilen
vector<complex<double>> ungerPkt(n / 2); //Haben dann Länge n/2
if (n == 1) {
return x;
} else {
//Aufteilen
for (int k = 0; k < n / 2; k++) {
gerPkt[k] = x[2 * k];
ungerPkt[k] = x[2 * k + 1];
}
vector<complex<double>> ftger = fourierTrafo(fp, gerPkt); //rekursives Aufrufen der Funktion um weiter zu Teilen
vector<complex<double>> ftunger = fourierTrafo(fp, ungerPkt);
vector<complex<double>> out(n);
for (int k = 0; k < n / 2; k++) {
//In out ergebnis nach FFT routine Speichern
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;
}
}
vector<complex<double>> makeS(fft_param *fp){
double h=fp->h; //Variabeln aus struct holen
double tmax=fp->tmax;
double tmin=fp->tmin;;
double t;
double tl=fp->tl;
double alpha=fp->alpha;
int n=fp->n;
vector<complex<double>> s(n); //Vektor zum speichern von s
for(int k=0;k<n;k++){
t=tmin+k*h;
//TODO s ist nicht geschiftet
if (t >= 0 && t <= fp->signalLenght){
s[k]=sin(2*M_PI*alpha*(t-tl)); //s berechnen
}
else{
s[k]=0;
}
}
return s;
}
vector<complex<double>> makeE(fft_param *fp){
double h=fp->h; //Variabeln aus struct holen
double tmax=fp->tmax;
double tmin=fp->tmin;
double t;
double beta=fp->beta;
double alpha=fp->alpha;
double tl=fp->tl;
double a=fp->a;
double b=fp->b;
int n=fp->n;
vector<complex<double>> e(n); //Vektor zum speichern von e
//vector<complex<double>> s=makeS(fp); //Vektor s wird benötigt
//FILE *fp3 = fopen("H10_2_30.txt" ,"w");
for(int k=0;k<n;k++){
t=tmin+k*h;
e[k]=b*sin(2*M_PI*beta*t)+ 2*a*(genrand_res53()-0.5); //e berechnen
if (t-tl >= 0 && t-tl <= fp->signalLenght){
e[k]+=sin(2*M_PI*alpha*(t-tl)); //s berechnen
}
//fprintf(fp3,"%f\t%f\n",t,pow(e[k].real(),2));
//printf("%f\t%f\n",t,pow(e[k].real(),2));
//printf("%f\t%f\t%f\n",t,e[k].real(),e[k].imag());
}
//fclose(fp3);
return e;
}
vector<complex<double>> aufgabe3 (fft_param *fp){
double h=fp->h; //Varabeln aus Struct holen
double tmin=fp->tmin;
double tmax=fp->tmax;
double t=tmin;
int n=fp->n;
vector<complex<double>> e=makeE(fp); //e und s berechnen
vector<complex<double>> s=makeS(fp);
vector<complex<double>> Fe=fourierTrafo(fp,e); //e Fouriertransformieren
vector<complex<double>> Fs=fourierTrafo(fp,s); //s Fouriertransformieren
vector<complex<double>> FFes(n); //Vektor machen zum speichern
for(int k=0;k<n;k++){
//Hier Fouriertransformation von e mal Fouriertransformation von s komplex konjugiert berechnen
FFes[k]=Fe[k]*conj(Fs[k]);
}
fft_param fps=*fp; //Sign im struct ändern
fps.sign=-1;
vector<complex<double>> FFFes=fourierTrafo(&fps, FFes); //Rücktransformation des Produkts
FILE *fpPlot =fopen("esPlot.csv","w");
for(int k = 0; k<n; k++){
fprintf(fpPlot, "%f\t%f\t%f\n", tmin+k*h, s[k].real(), e[k].real());
}
return FFFes;
//init_genrand((long)seed);
}
void plotAufgabe3(fft_param *fp){
int n = fp->n;
double tmin=fp->tmin;
double h=fp->h;
vector<complex<double>> es = aufgabe3(fp);
FILE *fp4 =fopen("es4.csv","w");
for (int k=0;k<n;k++){
//printf("%f\n", norm(es[k]));
fprintf(fp4,"%f\t%f\n", tmin+k*h, norm(es[k]/(double)N));
}
}
int main(){
long seed = 57291; // beliebigen Wert gewählt
init_genrand(seed); //rufe am Anfang mit einem festen Wert auf für reproduzierbare Ergebnise
fft_param fp;
fp.n= pow(2,13);
fp.tmin=0;
fp.tmax = 100;
fp.h=(fp.tmax-fp.tmin)/fp.n;
fp.sign=1;
fp.alpha=10;
fp.beta=1;
fp.a=0.5;
fp.b=0.50;
fp.tl=50;
fp.signalLenght = 5;
//test(&fp);
plotAufgabe3(&fp);
//printf("%f\n",genrand_res53());
//H10.2
fp.h=0.001; //Schrittweite der Zeit für Plot
fp.n=100/0.001; // plotte e(t) bis für die Zeit tmin bis tmin + 100
//makeE(&fp);
}