public static complex[] FFT(complex[] x) { int N = x.Length; complex[] X = new complex[N]; complex[] d, D, e, E; if (N == 1) { X[0] = x[0]; return(X); } int k; e = new complex[N / 2]; d = new complex[N / 2]; for (k = 0; k < N / 2; k++) { e[k] = x[2 * k]; d[k] = x[2 * k + 1]; } D = FFT(d); E = FFT(e); for (k = 0; k < N / 2; k++) { complex temp = complex.from_polar(1, -2 * Math.PI * k / N); D[k] *= temp; } for (k = 0; k < N / 2; k++) { X[k] = E[k] + D[k]; X[k + N / 2] = E[k] - D[k]; } return(X); }
public static complex[] DFT(complex[] x) { int N = x.Length; complex[] X = new complex[N]; for (int k = 0; k < N; k++) { X[k] = new complex(0, 0); for (int n = 0; n < N; n++) { complex temp = complex.from_polar(1, -2 * Math.PI * n * k / N); temp *= x[n]; X[k] += temp; } } return(X); }
/* * public class data * { * public Double time = 0; * public complex volts = new complex(0, 0); * }*/ #endregion // Function that reads the time data froma files and stores in in the variables private void PopulateData(String path, ref List <complex> datalist) { string line; StreamReader file = new StreamReader(path); bool time = true; while ((line = file.ReadLine()) != null) { complex data = new complex(); if (time) { try { Time.Add(Double.Parse(line)); } catch { time = false; continue; } } if (!time) { data.real = Double.Parse(line); Data.Add(data); } } /* * while ((line = file.ReadLine()) != null) * { * complex data = new complex(); * string[] split = line.Split(Convert.ToChar(9)); * Time.Add(Double.Parse(split[0])); * data.real = Double.Parse(split[1]); * datalist.Add(data); * } */ }
//Override subtraction operator public static complex operator -(complex a, complex b) { complex data = new complex(a.real - b.real, a.imag - b.imag); return(data); }
//Convert from polar to rectangular public static complex from_polar(double r, double radians) { complex data = new complex(r * Math.Cos(radians), r * Math.Sin(radians)); return(data); }
public static complex operator -(complex a, complex b) { complex temp = new complex(a.real - b.real, a.imag - b.imag); return(temp); }
public static complex FromPolar(double r, double radians) { complex temp = new complex(r * Math.Cos(radians), r * Math.Sin(radians)); return(temp); }
public static complex operator *(complex a, complex b) { complex temp = new complex((a.real * b.real) - (a.imag * b.imag), (a.real * b.imag + (a.imag * b.real))); return(temp); }