public static double[] Execute(double[] y, FTFunc func, WindowFunc windwfunc) { // Complex[] sign = new Complex[y.Length]; Complex[] done = new Complex[y.Length]; // 窓関数の実行 y = Fourier.Windowing(y, windwfunc); // オイラー単位円への割当 for (int i = 0; i < y.Length; i++) { sign[i] = new Complex(y[i], 0); } // 任意の変換 if (FTFunc.DFT == func) { done = Fourier.DFT(sign); } else if (FTFunc.FFT == func) { done = Fourier.FFT(sign); } for (int ii = 0; ii < y.Length; ii++) { y[ii] = done[ii].magnitude; } Seikika(ref y); // referencive functioon return(y); }
/// <summary> /// Initializes a new instance of the <see cref="FFT"/> class. /// wrapper for FFT. /// Window Power equals sum of squared window values. Default window is Hamming. /// </summary> public FFT(int windowSize, WindowFunc w) { if (!IsPowerOf2(windowSize)) { throw new ArgumentException("WindowSize must be a power of 2."); } this.WindowSize = windowSize; this.CoeffCount = (windowSize / 2) + 1; //f[0]=DC; f[256]=Nyquist //calculate the window weights and power this.WindowPower = windowSize; //the default power of a rectangular window. if (w != null) { //set up the FFT window this.WindowWeights = new double[windowSize]; for (int i = 0; i < windowSize; i++) { this.WindowWeights[i] = w(i, windowSize); } //calculate power of the FFT window double power = 0.0; for (int i = 0; i < windowSize; i++) { power += this.WindowWeights[i] * this.WindowWeights[i]; } this.WindowPower = power; } }
private void test2() { WindowFunc windowfunc = WindowFunc.None; foreach (RadioButton rdo in groupBox1.Controls) { if (rdo.Checked) { String s = rdo.Text; windowfunc = (WindowFunc)Enum.Parse(typeof(WindowFunc), s); } } y_out = Fourier.Execute(y, FTFunc.DFT, windowfunc); for (int i = 0; i < CNSTMAX; i++) { y2[i] = Convert.ToInt32(y_out[i]); } // 結果をファイル出力する String fileout = @"C:\Users\N.Ishikawa\Desktop\data\fft_out.txt"; String yout; System.IO.StreamWriter kekkaout = new System.IO.StreamWriter(fileout); for (int iii = 0; iii < CNSTMAX; iii++) { yout = y2[iii].ToString("D10"); kekkaout.WriteLine(yout); } kekkaout.Close(); }
private void button4_Click(object sender, EventArgs e) { WindowFunc windowfunc = WindowFunc.None; foreach (RadioButton rdo in groupBox1.Controls) { if (rdo.Checked) { String s = rdo.Text; windowfunc = (WindowFunc)Enum.Parse(typeof(WindowFunc), s); } } double[] y_window = Fourier.Windowing(y, windowfunc); int[] y2_window = new int[y_window.Length]; //y2_window.CopyTo(y_window, 0); for (int i = 0; i < y2_window.Length; i++) { y2_window[i] = (int)y_window[i]; } DataViewer view = new DataViewer(y2_window); view.Show(); // 結果をファイル出力する String fileout = @"C:\Users\N.Ishikawa\Desktop\data\viewer_out.txt"; String yout; System.IO.StreamWriter kekkaout = new System.IO.StreamWriter(fileout); for (int iii = 0; iii < CNSTMAX; iii++) { yout = y2_window[iii].ToString("D10"); kekkaout.WriteLine(yout); } kekkaout.Close(); }
/// <summary> /// 根据进程位数 选择SetWindowLong WinApi调用 /// </summary> public static IntPtr SetWindowLongPtr(IntPtr hwnd, int nIndex, WindowFunc dwNewLong) { if (Environment.Is64BitProcess) { return(SetWindowLong64(hwnd, nIndex, dwNewLong)); } return(SetWindowLong(hwnd, nIndex, dwNewLong)); }
protected override bool ApplyEffect() { if (String.IsNullOrWhiteSpace(this.textBox1.Text)) { return(false); } if (!Int32.TryParse(this.textBox1.Text, out int windowLength)) { return(false); } if (!Double.TryParse(this.textBox3.Text, out double lowerFreqBoundary)) { return(false); } if (!Double.TryParse(this.textBox2.Text, out double upperFreqBoundary)) { return(false); } if (upperFreqBoundary < lowerFreqBoundary) { GeneralUtilities.Swap(ref upperFreqBoundary, ref lowerFreqBoundary); } int frameLength = (int)this.comboBox2.SelectedItem; string funcName = String.Join("", (this.comboBox1.SelectedItem as string).Split(' ')); MethodInfo methodInfo = typeof(WindowFunctions).GetMethod(funcName, BindingFlags.Public | BindingFlags.Static); if (methodInfo == null) { return(false); } WindowFunc windowFunction = new WindowFunc() { Length = windowLength, Function = Delegate.CreateDelegate(typeof(Func <int, int, double>), methodInfo) as Func <int, int, double> }; var result = SoundManipulation.Filters.Bandstop( this.BufferToModify, frameLength, lowerFreqBoundary, upperFreqBoundary, windowFunction, this.FileFormat.SampleRate ); this.ModifiedBuffer = result; return(true); }
protected override bool ApplyEffect() { if (String.IsNullOrWhiteSpace(this.textBox1.Text)) { return(false); } bool parsingBoundarySuccessful = Double.TryParse(this.textBox2.Text, out double res); if (!parsingBoundarySuccessful) { return(false); } double freqBoundary = res; int frameLength = (int)this.comboBox2.SelectedItem; bool parsingSuccessful = Int32.TryParse(this.textBox1.Text, out int windowLength); if (!parsingSuccessful) { return(false); } string funcName = String.Join("", ((string)this.comboBox1.SelectedItem).Split(' ')); MethodInfo windowFunction = typeof(WindowFunctions).GetMethod(funcName, BindingFlags.Static | BindingFlags.Public); if (windowFunction == null) { return(false); } WindowFunc func = new WindowFunc() { Length = windowLength, Function = Delegate.CreateDelegate(typeof(Func <int, int, double>), windowFunction) as Func <int, int, double> }; var result = SoundManipulation.Filters.Lowpass( this.BufferToModify, frameLength, freqBoundary, func, this.FileFormat.SampleRate ); this.ModifiedBuffer = result; return(true); }
public static void ApplyWindow(ref NativeArray <float> array, WindowFunc windowFunc) { int N = array.Length; switch (windowFunc) { case WindowFunc.Hann: { for (int i = 0; i < N; ++i) { float x = (float)i / (N - 1); array[i] *= 0.5f - 0.5f * math.cos(2f * math.PI * x); } break; } case WindowFunc.BlackmanHarris: { for (int i = 0; i < N; ++i) { float x = (float)i / (N - 1); array[i] *= 0.35875f - 0.48829f * math.cos(2f * math.PI * x) + 0.14128f * math.cos(4f * math.PI * x) - 0.01168f * math.cos(6f * math.PI * x); } break; } case WindowFunc.Gaussian4_5: { for (int i = 0; i < N; ++i) { float x = (float)i / (N - 1); array[i] *= math.exp(-math.pow(x / 4.5f, 2f)); } break; } } }
public static float[] Windowing(float[] data, WindowFunc windowfunc) { int size = data.Length; float[] windata = new float[size]; for (int i = 0; i < size; i++) { double winValue = 0; // 各々の窓関数 switch (windowfunc) { case WindowFunc.Hamming: winValue = 0.54 - 0.46 * Math.Cos(2 * Math.PI * i / (size - 1)); break; case WindowFunc.Hanning: winValue = 0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (size - 1)); break; case WindowFunc.Blackman: winValue = 0.42 - 0.5 * Math.Cos(2 * Math.PI * i / (size - 1) + 0.08 * Math.Cos(4 * Math.PI * i / (size - 1))); break; case WindowFunc.Rectangular: winValue = 1.0; break; default: winValue = 1.0; break; } // 窓関数を掛け算 windata[i] = data[i] * (float)winValue; } return(windata); }
} // 窓関数 public static double[] Windowing(double[] data, WindowFunc windowFunc) { int size = data.Length; double[] windata = new double[size]; for (int i = 0; i < size; i++) { double winValue = 0; // 各々の窓関数 if (WindowFunc.Hamming == windowFunc) { winValue = 0.54 - 0.46 * Math.Cos(2 * Math.PI * i / (size - 1)); } else if (WindowFunc.Hanning == windowFunc) { winValue = 0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (size - 1)); } else if (WindowFunc.Blackman == windowFunc) { winValue = 0.42 - 0.5 * Math.Cos(2 * Math.PI * i / (size - 1)) + 0.08 * Math.Cos(4 * Math.PI * i / (size - 1)); } else if (WindowFunc.Rectangular == windowFunc) { winValue = 1.0; } else { winValue = 1.0; } // 窓関数を掛け算 windata[i] = data[i] * winValue; } return(windata); }
public static void SetWindowRefreshCallback(Window window, WindowFunc callback) { var ptr = Marshal.GetFunctionPointerForDelegate(callback); glfwSetWindowRefreshCallback(window.Ptr, ptr); }
public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, WindowFunc dwNewLong);
public static void SetWindowCloseCallback(Window window, WindowFunc callback) { var ptr = Marshal.GetFunctionPointerForDelegate(callback); glfwSetWindowCloseCallback(window.Ptr, ptr); GC.KeepAlive(callback); }