public ComplexArray(ComplexArray.None dummy, double[] wave) { len = wave.Length; size = len * 2; data = new double[size]; for (int i = 0; i < len; i++) { data[i * 2 + 1] = wave[i]; } }
/// <summary> /// NFFT: Number of data for FFT (= 2^n > 4tap + num_disp, >1024) /// </summary> private void update_nfft() { int val = 1024; while (val < num_disp + filter.tap * 4) val *= 2; if (nfft != val) { // update nfft = val; lock (fftw) { over = new WaveData(nfft * over_sample_); ans = new WaveData(nfft); factors = new WaveData(nfft); wave = new WaveData(nfft); raw_wave = new WaveData(nfft); } extracted_raw_wave = new double[nfft]; // update Omega double fs = 1.0 / dt; double df = fs / nfft; lock (fftw) { omega = new ComplexArray(nfft / 2 + 1); omega2 = new ComplexArray(nfft / 2 + 1); } double df0 = -1.0 / (df*2*Math.PI); double v; for (int i = 1; i < nfft/2; i++) { v = df0 / i; omega[i].Imag = v; omega2[i].Real = - v * v; } // update freqs freqs = Enumerable.Range(0, nfft / 2 + 1).Select(i => df * i).ToArray(); } }
public ComplexArray(ComplexArray ca) { len = ca.len; size = ca.size; data = ca.data.ToArray(); }
public static ComplexArray real(double[] wave) { int len = wave.Length; ComplexArray ans = new ComplexArray(len); for (int i = 0; i < len; i++) { ans.data[i * 2] = wave[i]; } return ans; }
// operation /// <summary> /// DFT (Forword). /// Not normalized. /// </summary> /// <returns>Forward DFTed ComplexArray</returns> public ComplexArray dft(fftw_direction dir = fftw_direction.Forward) { ComplexArray ans = new ComplexArray(len); try { pin = fftw.malloc(sizeof(double) * size); pout = fftw.malloc(sizeof(double) * size); plan = fftw.dft_1d(len, pin, pout, dir, fftw_flags.Estimate); Marshal.Copy(data, 0, pin, size); fftw.execute(plan); Marshal.Copy(pout, ans.data, 0, size); } finally { fftw.free(pin); fftw.free(pout); fftw.destroy_plan(plan); } return ans; }
public static ComplexArray operator /(ComplexArray lhs, ComplexArray rhs) { if (lhs.len != rhs.len) throw new ArgumentException("Array size must be same"); ComplexArray ans = new ComplexArray(lhs.len); for (int i = 0; i < lhs.len; i++) { ans[i] = lhs[i] / rhs[i]; } return ans; }
/// <summary> /// Constract from DFT result of with real wave. /// </summary> /// <param name="wave">real wave</param> /// <returns>Constructed ComplexArray (Spectrum)</returns> public static ComplexArray by_fft(double[] wave) { throw new NotImplementedException(); IntPtr pin = IntPtr.Zero; IntPtr pout = IntPtr.Zero; IntPtr plan = IntPtr.Zero; int len = wave.Length; int size = len + 2; var res = new ComplexArray(size/2); try { pin = fftw.malloc(sizeof(double) * len); pout = fftw.malloc(sizeof(double) * size); plan = fftw.dft_r2c_1d(len, pin, pout, fftw_flags.Estimate); Marshal.Copy(wave, 0, pin, len); fftw.execute(plan); Marshal.Copy(pout, res.data, 0, size); } finally { fftw.free(pin); fftw.free(pout); fftw.destroy_plan(plan); } return res; }
public static ComplexArray operator /(ComplexArray lhs, Complex c) { ComplexArray ans = new ComplexArray(lhs.len); for (int i = 0; i < lhs.len; i++) { ans[i] = lhs[i] / c; } return ans; }
public static ComplexArray operator -(ComplexArray lhs, double d) { ComplexArray ans = new ComplexArray(lhs.len); for (int i = 0; i < lhs.len; i++) { ans.data[i * 2] = lhs.data[2 * i] - d; } return ans; }
public static ComplexArray operator *(Complex c, ComplexArray rhs) { ComplexArray ans = new ComplexArray(rhs.len); for (int i = 0; i < rhs.len; i++) { var x = rhs[i] * c; } return ans; }
public static ComplexArray operator *(double d, ComplexArray rhs) { ComplexArray ans = new ComplexArray(rhs.len); for (int i = 0; i < rhs.size; i++) { ans.data[i] = rhs.data[i] * d; } return ans; }
// operators public static ComplexArray operator *(ComplexArray lhs, double d) { ComplexArray ans = new ComplexArray(lhs.len); for (int i = 0; i < lhs.size; i++) { ans.data[i] = lhs.data[i] * d; } return ans; }