static void Main(string[] args) { test1(); CaculateTimes caculate = new CaculateTimes(); caculate.StartTime(); //4 3 2 6 7 8 9 0 int[] nums = { 4, 3, 2, 6, 7, 8, 9, 0 }; int[] nums_2 = { 1, 2, 5, 4, 8, 6, 4, 5, 2, 3, 1 }; Complex[] complices = new Complex[nums.Length]; for (int i = 0; i < nums.Length; i++) { complices[i] = new Complex(nums[i], 0); } FourierTransform.DFT(complices, FourierTransform.Direction.Backward); for (int i = 0; i < nums.Length; i++) { Console.WriteLine(complices[i].Re + " " + complices[i].Im); } Console.WriteLine(); Console.WriteLine("===================="); Console.WriteLine(); for (int i = 0; i < nums.Length; i++) { complices[i].Re = nums[i]; complices[i].Im = 0; } FourierTransform.FFT(complices, FourierTransform.Direction.Backward); for (int i = 0; i < nums.Length; i++) { Console.WriteLine(complices[i].Re + " " + complices[i].Im); } Console.WriteLine(caculate.EndTime()); Console.ReadKey(); }
//public int[] FFT(int[] y) public async Task <int[]> FFT(int[] y) { WvlLogger.Log(LogType.TraceAll, "FFT()"); var input = new AForge.Math.Complex[y.Length]; for (int i = 0; i < y.Length; i++) { input[i] = new AForge.Math.Complex(y[i], 0); } FourierTransform.FFT(input, FourierTransform.Direction.Forward); var result = new int[y.Length / 2]; // getting magnitude for (int i = 0; i < y.Length / 2 - 1; i++) { var current = Math.Sqrt(input[i].Re * input[i].Re + input[i].Im * input[i].Im); current = Math.Log10(current) * 10; result[i] = (int)current; } samplesUpdated(this, new SamplesUpdatedEventArgs(result)); //PrepareHeatmapDataSeries(result); return(result); }
public static float[] ConvertWav2FFTMag(string wavfile, string outfile) { WAVFile audioFile = new WAVFile(); String warning = audioFile.Open(wavfile, WAVFile.WAVFileMode.READ); Complex[] indata = new Complex[MaxSamples]; int IterSamples = (audioFile.NumSamples < MaxSamples) ? audioFile.NumSamples : MaxSamples; float[] indata_mag = null; if (warning == "") { //short audioSample = 0; for (int sampleNum = 0; sampleNum < IterSamples; ++sampleNum) { //audioSample = audioFile.GetNextSampleAs16Bit(); indata[sampleNum].Re = audioFile.GetNextSampleAs16Bit(); } FourierTransform.FFT(indata, FourierTransform.Direction.Forward); indata_mag = GetMagnitude(indata); if (outfile != null) { WriteData(indata_mag, outfile); } } audioFile.Close(); return(indata_mag); }
private void CopyFromDomain() { var audioBuffer = _gameBoy.APU.Buffer; var sampleCount = _gameBoy.APU.SampleCount; var complexBuffer = new Complex[_fftSize * 2]; var index = 0; var originalIndex = 0; foreach (var value in audioBuffer) { if (originalIndex % 2 == 0) { if (index >= sampleCount) { complexBuffer[index] = new Complex(0, 0); } else { complexBuffer[index] = new Complex(value, 0); } index++; if (index == _fftSize * 2) { break; } } originalIndex++; } FourierTransform.FFT(complexBuffer, FourierTransform.Direction.Forward); CopyFFTToFrameColumn(complexBuffer); Utils.TransferBytesToWriteableBitmap(_spectrogram, _spectrogramData); }
/// <summary> /// Two dimensional Fast Fourier Transform. /// </summary> /// /// <param name="data">Data to transform.</param> /// <param name="direction">Transformation direction.</param> /// /// <remarks><para><note>The method accepts <paramref name="data"/> array of 2<sup>n</sup> size /// only in each dimension, where <b>n</b> may vary in the [1, 14] range. For example, 16x16 array /// is valid, but 15x15 is not.</note></para></remarks> /// /// <exception cref="ArgumentException">Incorrect data length.</exception> /// public static void FFT2(Complex[,] data, Direction direction) { int k = data.GetLength(0); int n = data.GetLength(1); // check data size //if ( // (!Tools.IsPowerOf2(k)) || // (!Tools.IsPowerOf2(n)) || // (k < minLength) || (k > maxLength) || // (n < minLength) || (n > maxLength) // ) if ( (!Tools.IsPowerOf2(k)) || (!Tools.IsPowerOf2(n)) || (k < minLength) || (k > maxLength) || (n < minLength) || (n > maxLength) ) { throw new ArgumentException("Incorrect data length."); } // process rows Complex[] row = new Complex[n]; for (int i = 0; i < k; i++) { // copy row for (int j = 0; j < n; j++) { row[j] = data[i, j]; } // transform it FourierTransform.FFT(row, direction); // copy back for (int j = 0; j < n; j++) { data[i, j] = row[j]; } } // process columns Complex[] col = new Complex[k]; for (int j = 0; j < n; j++) { // copy column for (int i = 0; i < k; i++) { col[i] = data[i, j]; } // transform it FourierTransform.FFT(col, direction); // copy back for (int i = 0; i < k; i++) { data[i, j] = col[i]; } } }
private void OnDataReceiverEvent(int[] real, int[] imaginary, int length, rx_metadata_t md) { if (length < numberOfPoints) // Insufficient data for FFT { return; } for (int i = 0; i < numberOfPoints; i++) { data[i] = new AForge.Math.Complex(real[i], imaginary[i]); } FourierTransform.FFT(data, FourierTransform.Direction.Forward); int count = 0; for (int i = numberOfPoints / 2; i < numberOfPoints; i++) { testValues[i, 1] = data[count++].Magnitude; } for (int i = 0; i < numberOfPoints / 2; i++) { testValues[i, 1] = data[count++].Magnitude; } chart.UpdateDataSeries("Test", testValues); //chart.RangeY = new AForge.Range(0, 2048); }
public override void FFT(bool forward) { data.CopyTo(copy, 0); FourierTransform.FFT(copy, forward ? FourierTransform.Direction.Forward : FourierTransform.Direction.Backward); }
public void GetSpectrumData(float[] samples, float[] spectrumReal, float[] spectrumImg, FFTWindow fftWindow) { if (null == samples) { return; } int size = samples.Length; if (null == m_complex || m_complex.Length != size) { m_complex = new float[size]; } m_fourierTransform.FFT(samples, m_complex, spectrumReal, spectrumImg); }
public static void FastDFT(Complex[] x, int mode = 1) { var dir = FourierTransform.Direction.Forward; if (mode == -1) { dir = FourierTransform.Direction.Backward; } FourierTransform.FFT(x, dir); }
public void IFFTPasses() { var input = Enumerable.Range(0, 8) .Select(_i => new Complex(SoundUtils.CalSinWave(_i, 0.25f, 250f, 8000), 0)) .ToArray(); var fft = new FourierTransform(); var output = fft.FFT(input); var gots = fft.IFFT(output); AssertionUtils.AreEqualComplexArray(input, gots, "逆変換に失敗しています", EPSILON); }
/// <summary> /// Converts a frame to log-power-spectrum bins. /// </summary> /// private double[] frame2logspec(params Int16[] p_frame) { double[] w_preEmp = pre_emphasis(p_frame); double[] w_win = new double[m_win.Length]; for (int w_i = 0; w_i < m_win.Length; w_i++) { w_win[w_i] = m_win[w_i]; } // Each frame has to be multiplied with a hamming window in order to keep // the continuity of the first and the last points in the frame. double[] w_frame = new double[w_win.Length]; for (int w_i = 0; w_i < w_win.Length; w_i++) { w_frame[w_i] = w_preEmp[w_i] * w_win[w_i]; } Complex[] w_complexFrame = new Complex[m_nfft]; for (int w_i = 0; w_i < w_frame.Length && w_i < m_nfft; w_i++) { w_complexFrame[w_i] = w_frame[w_i]; } for (int w_i = w_frame.Length; w_i < m_nfft; w_i++) { w_complexFrame[w_i] = 0; } // Compute the one-dimensional discrete Fourier Transform for real input. FourierTransform.FFT(w_complexFrame, FourierTransform.Direction.Backward); // Square of absolute value double[] w_power = new double[m_nfft / 2 + 1]; for (int w_i = 0; w_i < m_nfft / 2 + 1; w_i++) { w_power[w_i] = w_complexFrame[w_i].Real * w_complexFrame[w_i].Real + w_complexFrame[w_i].Imaginary * w_complexFrame[w_i].Imaginary; } double[] w_dotMat = Matrix.Dot(w_power, m_filters); double[] w_ret = new double[w_dotMat.Length]; for (int w_i = 0; w_i < w_ret.Length; w_i++) { if (w_dotMat[w_i] < 1.0f / 100000.0f) { w_ret[w_i] = System.Math.Log(1.0f / 100000.0f); } else { w_ret[w_i] = System.Math.Log(w_dotMat[w_i]); } } return(w_ret); }
public double[] Equalize(double[] gains, WavData wavData) { var n = FourierHelper.GetExpandedPow2(_windowLength + _filterLength - 1); var size = wavData.Samples.Length + n - _windowLength; var result = new double[size]; var windows = new double[size / _windowHopSize][]; var windowsComplex = new Complex[size / _windowHopSize][]; for (int i = 0; i < windows.Length; i++) { windows[i] = new double[n]; windowsComplex[i] = new Complex[n]; } var windowFactors = _window.WindowFactors(_windowLength); var gainsComplex = GenerateGains(gains, wavData.FormatChunk.SampleRate, n); for (int i = 0; i < windows.Length; i++) { for (int j = 0; j < _windowLength; j++) { if (i * _windowHopSize + j < wavData.Samples.Length) { windows[i][j] = windowFactors[j] * wavData.Samples[i * _windowHopSize + j]; } else { windows[i][j] = 0; } } for (int j = _windowLength; j < n; j++) { windows[i][j] = 0; } windowsComplex[i] = FourierTransform.FFT(windows[i]); windowsComplex[i] = AdjustGain(gainsComplex, windowsComplex[i]); windows[i] = FourierTransform.IFFT(windowsComplex[i]); } for (int i = 0; i < windows.Length; i++) { for (int j = 0; j < windows[i].Length; j++) { if (i * _windowHopSize + j < wavData.Samples.Length) { result[i * _windowHopSize + j] += windows[i][j]; } } } return(result); }
public void FFTPasses() { var input = Enumerable.Range(0, 8) .Select(_i => new Complex(SoundUtils.CalSinWave(_i, 0.25f, 250f, 8000), 0)) .ToArray(); var fft = new FourierTransform(); var output = fft.FFT(input); //検証用のデータ作成 var corrects = fft.FT(input); AssertionUtils.AreEqualComplexArray(corrects, output, "", EPSILON); }
/// <summary> /// Applies backward fast Fourier transformation to the complex signal. /// </summary> /// public void BackwardFourierTransform() { if (status == ComplexSignalStatus.FourierTransformed) { for (int i = 0; i < Channels; i++) { Complex[] channel = GetChannel(i); FourierTransform.FFT(channel, FourierTransform.Direction.Backward); SetChannel(i, channel); } status = ComplexSignalStatus.Normal; } }
public void IFFT() { if (copy == null) { copy = (List <DataPoint>)Extensions.Clone(signal); } FourierTransform.FFT(fft, FourierTransform.Direction.Backward); for (var i = 0; i < signal.Count(); ++i) { signal[i].mV = fft[i].Re; } }
public static Complex[] CrossCorrelation(Complex[] ffta, Complex[] fftb) { var conj = ffta.Select(i => new Complex(i.Re, -i.Im)).ToArray(); for (int a = 0; a < conj.Length; a++) { conj[a] = Complex.Multiply(conj[a], fftb[a]); } FourierTransform.FFT(conj, FourierTransform.Direction.Backward); return(conj); }
private void loadButton_Click(object sender, EventArgs e) { var fileDialog = new OpenFileDialog(); fileDialog.Filter = "Files (wav)|*.wav"; fileDialog.ShowDialog(); loadTextBox.Text = fileDialog.FileName; listBox.Items.Clear(); pointsList.Clear(); FileStream fs = new FileStream(fileDialog.FileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); wav = new WAV(br, (int)fs.Length); setListBox(wav); br.Close(); fs.Close(); listBox.Visible = true; spectrumChart.Visible = true; spectrumChart.Series.Clear(); spectrumChart.Update(); spectrumChart.ResetAutoValues(); var tabCom = new Complex[1024]; float[] data = wav.getFloatData(); for (int i = 0; i < 1024; i++) { tabCom[i] = new Complex(data[i], 0); } FourierTransform.FFT(tabCom, FourierTransform.Direction.Forward); for (int i = 0; i < 512; i++) { pointsList.Add(new Point() { x = (wav.header.sampleRate * i) / 511, y = tabCom[i].Magnitude * 1000 }); } spectrumChart.Series.Clear(); spectrumChart.Series.Add("Spectrum"); foreach (var p in pointsList) { spectrumChart.Series["Spectrum"].Points.AddXY(p.x, p.y); } MessageBox.Show("File loaded!"); }
/* * Generates a frequency distribution for the given audio samples. * Applies PRE_GAIN to the samples before evaluating the Fourier transform. */ protected void Fourier(float[] samples, double[] fourier, Complex[] fourierComplex) { for (int i = 0; i < samples.Length; i++) { fourierComplex[i] = new Complex(samples[i] * PRE_GAIN, 0.0); } FourierTransform.FFT(fourierComplex, FourierTransform.Direction.Forward); for (int i = 0; i < fourierComplex.Length; i++) { fourier[i] = fourierComplex[i].Magnitude; } }
private void CalculateFFT(List <float> values, List <Complex> fftValues) { Complex[] data = new Complex[FFT_SAMPLE_SIZE]; for (int i = 0; i < FFT_SAMPLE_SIZE; i++) { data[i] = new Complex(values[i], 0); } FourierTransform.FFT(data, FourierTransform.Direction.Forward); fftValues.Clear(); fftValues.Capacity = data.Length; fftValues.AddRange(data); }
/// <summary> /// Applies forward fast Fourier transformation to the complex signal. /// </summary> /// public void ForwardFourierTransform() { if (status == ComplexSignalStatus.Normal || status == ComplexSignalStatus.Analytic) { for (int i = 0; i < Channels; i++) { System.Numerics.Complex[] channel = GetChannel(i); FourierTransform.FFT(channel, FourierTransform.Direction.Forward); SetChannel(i, channel); } status = ComplexSignalStatus.FourierTransformed; } }
public override double[] Spectrum(double[] input, bool scale) { var data = ToComplex(input); FourierTransform.FFT(data, FourierTransform.Direction.Forward); var spectrum = ComputeSpectrum(data); FourierTransform.FFT(data, FourierTransform.Direction.Backward); ToDouble(data, input); return(spectrum); }
public static void detectPitch(float[] data) { Complex[] complex = new Complex[data.Length]; int fftPoints = 2; while (fftPoints * 2 <= complex.Length) { fftPoints *= 2; } for (int i = 0; i < complex.Length; i++) { complex[i] = new Complex(data[i] * Gausse(i, data.Length), 0.0); } ; FourierTransform.FFT(complex, FourierTransform.Direction.Forward); double[] fftresult = new double[complex.Length / 2]; for (int i = 0; i < fftresult.Length; i++) { fftresult[i] = complex[i].Magnitude; } Note note; float fftStep = sampleRate / bufferLength; for (int i = 0; i < fftresult.Length; i++) { note = new Note((int)(i * fftStep)); if (!note.name.Equals("note not found")) { if (notes.ContainsKey(note.ToString())) { notes[note.ToString()].magnitude += fftresult[i]; } else { note.magnitude += fftresult[i]; notes.Add(note.name, note); } } ; } }
public static Complex[] BandPassFilterFactors(double lowPassFreq, double highPassFreq, double sampleFreq, int filterLength, int n) { var window = new HanningWindow(); var windowFilterFactors = window.WindowFactors(filterLength); var low = LowPassFilterFactors(lowPassFreq, sampleFreq, filterLength); var high = HighPassFilterFactors(highPassFreq, sampleFreq, filterLength); var lowWindowed = new double[n]; var highWindowed = new double[n]; for (int i = 0; i < filterLength; i++) { lowWindowed[i] = low[i] * windowFilterFactors[i]; highWindowed[i] = high[i] * windowFilterFactors[i]; } for (int i = filterLength; i < n; i++) { lowWindowed[i] = 0; highWindowed[i] = 0; } var shiftNumberFilter = (filterLength - 1) / 2; var lowShiftedFilter = lowWindowed.Take(shiftNumberFilter); var lowFilteredTemp = lowWindowed.Skip(shiftNumberFilter).ToList(); lowFilteredTemp.AddRange(lowShiftedFilter); lowWindowed = lowFilteredTemp.ToArray(); var highShiftedFilter = highWindowed.Take(shiftNumberFilter); var highFilteredTemp = highWindowed.Skip(shiftNumberFilter).ToList(); highFilteredTemp.AddRange(highShiftedFilter); highWindowed = highFilteredTemp.ToArray(); var lowComplex = FourierTransform.FFT(lowWindowed); var highComplex = FourierTransform.FFT(highWindowed); var bandPass = new Complex[n]; for (int i = 0; i < n; i++) { bandPass[i] = lowComplex[i] * highComplex[i]; } return(bandPass); }
public static double[] FFT(double[] data) { double[] fft = new double[data.Length]; Complex[] fftComplex = new Complex[data.Length]; for (int i = 0; i < data.Length; i++) { fftComplex[i] = new System.Numerics.Complex(data[i], 0.0); } FourierTransform.FFT(fftComplex, FourierTransform.Direction.Forward); for (int i = 0; i < data.Length; i++) { fft[i] = fftComplex[i].Magnitude; } return(fft); }
public override void Draw() { Graphics.SetColor(1, 1, 1); if (buffer == null) { Graphics.Print("No buffer available"); return; } int len = buffer.FloatBuffer.Length / 8; float pad = (float)len / WindowWidth; // samples per pixels for (int x = 0; x < WindowWidth; x++) { // current sample int i = (int)Math.Round(x * pad); float y = buffer.FloatBuffer[i]; // previous sample int x1 = x - 1; int i1 = (int)Math.Round((x - 1) * pad); float y1 = buffer.FloatBuffer[Math.Max(i1, 0)]; // render Graphics.SetColor(Math.Abs(y), 1f - Math.Abs(y), Math.Abs(y), 1f); Graphics.Line(x1, WindowHeight / 2 + y1 * (WindowHeight / (Intensity * 2)), x, WindowHeight / 2 + y * (WindowHeight / (Intensity * 2))); } // fft Complex[] values = new Complex[Size]; for (int i = 0; i < values.Length; i++) { values[i] = new Complex(buffer.FloatBuffer[i], 0.0); } FourierTransform.FFT(values, FourierTransform.Direction.Forward); for (int i = 0; i < Size; i++) { float v = (float)(values[i].Magnitude); //Graphics.Print(v.ToString(), 0, (i + 1) * 16); Graphics.SetColor(Math.Abs(v), 1f - Math.Abs(v), 1f - Math.Abs(v), 1f); Graphics.Rectangle(DrawMode.Fill, i * 16, WindowHeight, 16, -v * 10 * WindowHeight - 1); /*int j = Math.Max(i - 1, 0); * float w = (float)(values[j].Magnitude); * Graphics.Line(j, w * WindowHeight, i, v * WindowHeight);*/ } }
/// <summary> /// 得到频谱数值 /// </summary> /// <param name="complices"></param> /// <returns></returns> private double[] GetSpectrum(Complex [] complices) { if (complices == null) { Console.WriteLine("ConsoleMorphVOXPro.WaveDeal.Spectrogram->GetSpectrum"); return(null); } double[] spectrumMap = new double[complices.Length]; FourierTransform.FFT(complices, FourierTransform.Direction.Backward); for (int i = 0; i < spectrumMap.Length; i++) { spectrumMap[i] = complices[i].Magnitude; } return(spectrumMap); }
static double[] FFT(double[] data) { for (int i = 0; i < SAMPLE_COUNT; i++) { fftComplex[i] = new Complex(data[i], 0.0); } FourierTransform.FFT(fftComplex, FourierTransform.Direction.Forward); for (int i = 0; i < SAMPLE_COUNT; i++) { fft[i] = fftComplex[i].Magnitude; } return(fft); }
internal static List <TrainingValue> GetTrainingValues(List <EMGPacket> packets, bool enableSkip) { List <TrainingValue> values = new List <TrainingValue>(EMGProcessor.FFT_SAMPLE_SIZE); int skipsRemaining = 0; for (int i = 0; i < packets.Count / EMGProcessor.FFT_SAMPLE_SIZE; i++) { if (enableSkip && skipsRemaining > 0) { skipsRemaining--; continue; } Complex[] data = new Complex[EMGProcessor.FFT_SAMPLE_SIZE]; int start = i * EMGProcessor.FFT_SAMPLE_SIZE; int end = start + EMGProcessor.FFT_SAMPLE_SIZE; MuscleState startMuscleState = packets[start].muscleStateHint; for (int j = start; j < end; j++) { EMGPacket packet = packets[j]; if (packet.muscleStateHint != startMuscleState) { skipsRemaining += EMGProcessor.SKIPS_AFTER_TRANSITION; break; } data[j - start] = new Complex(EMGProcessor.ValueFromPacket(packet), 0); } if (enableSkip && skipsRemaining > 0) { continue; } FourierTransform.FFT(data, FourierTransform.Direction.Forward); List <Complex> fftResults = new List <Complex>(data); TrainingValue trainingValue = new TrainingValue((int)packets[start].muscleStateHint, EMGProcessor.FEATURE_COUNT); EMGProcessor.FillTrainingValue(ref trainingValue, fftResults); values.Add(trainingValue); } return(values); }
// Get fft result after index, use 0.25 second. public double[] GetFFTResult(int index) { Complex[] fftData = new Complex[this.fftLength]; double[] result = new double[this.fftLength]; for (int i = 0; i < this.fftLength; ++i) { fftData[i] = new Complex(this.data[index + i], 0); } FourierTransform.FFT(fftData, FourierTransform.Direction.Forward); for (int i = 0; i < this.fftLength / 2; ++i) { result[i] = fftData[i].Re * fftData[i].Re + fftData[i].Im * fftData[i].Im; } return(result); }
public List <Sound> Calculate(WavData wavData) { var frequencies = new List <Sound>(); for (int i = 0; i < wavData.ChunkedSamples.Length; i++) { var samples = wavData.ChunkedSamples[i]; var reducedSamples = FourierHelper.ReduceToPow2(samples); reducedSamples = FourierHelper.PreEmphasis(reducedSamples); reducedSamples = _fourierWindow.Windowing(reducedSamples); var complexSamples = FourierTransform.FFT(reducedSamples); complexSamples = complexSamples.Take(complexSamples.Length / 2).ToArray(); var threshold = complexSamples.Max(c => c.Magnitude) / 10; var localMax = 0; for (int j = 1; j < complexSamples.Length; j++) { if (complexSamples[j].Magnitude > threshold && complexSamples[j].Magnitude > complexSamples[j - 1].Magnitude && complexSamples[j].Magnitude > complexSamples[j + 1].Magnitude) { localMax = j; break; } } var frequency = (wavData.FormatChunk.SampleRate / (complexSamples.Length * 2)) * localMax; if (frequencies.Any() && frequencies.Last().Frequency == frequency) { frequencies.Last().EndTime += 50; } else { frequencies.Add(new Sound { StartTime = i * 50, EndTime = i * 50 + 50, Frequency = frequency, Result = complexSamples.Select(c => c.Magnitude).ToArray() }); } } return(frequencies); }