// GET: DFT public ActionResult Image(HttpPostedFileBase image) { var model = new DFTImageModel(); if (image != null) { Image input = Bitmap.FromStream(image.InputStream, true, true); model.Input = FileManager.UploadPhoto(input, "input"); FastFourierTransformation fft = new FastFourierTransformation((Bitmap)input); fft.Forward(); fft.AddShift(); fft.Plot(fft.FFTShifted); //fft.Plot(); fft.Inverse(); Image mag = (Image)fft.FourierPlot; model.Mag = FileManager.UploadPhoto(mag, "mag"); Image phase = (Image)fft.PhasePlot; model.Phase = FileManager.UploadPhoto(phase, "phase"); Image inverse = (Image)fft.Image; model.Inverse = FileManager.UploadPhoto(inverse, "inverse"); } return(View(model)); }
// GET: DFT public ActionResult Calculation() { var model = new DFTCalculationModel(); model.Input = Request["values"]; model.Values = new List <double>(); model.Errors = new List <string>(); if (!String.IsNullOrEmpty(model.Input)) { foreach (var item in model.Input.Split(' ').Where(v => !String.IsNullOrEmpty(v)).ToList()) { try { model.Values.Add(Double.Parse(item.Replace(',', '.'), CultureInfo.InvariantCulture)); } catch { model.Errors.Add(item + " is and invalid input value"); } } if (!model.Errors.Any()) { FastFourierTransformation fft = new FastFourierTransformation(); model.Output = CostumComplex.Parse(fft.FFT1D(model.Values.ToArray(), 4)); } } return(View(model)); }
public CoreSpectrum(int size = 1024) { newDataRead = true; hamming = new float[size]; for (int n = 0; n < hamming.Length; n++) { hamming[n] = FastFourierTransformation.HammingWindowF(n, size); } capture = new WasapiLoopbackCapture(CAPTURE_DELAY); capture.DataAvailable += (s, a) => { if (newDataRead) { UpdateSpectrumData(a.Data); } }; capture.Stopped += (c, e) => { for (int i = 0; i < spectrumData.Length; i++) { spectrumData[i] = 0f; } }; capture.Initialize(); capture.Start(); fftProvider = new FftProvider(STEREO, FftSize.Fft1024); spectrumData = new float[size]; }
// ReSharper disable once InconsistentNaming public void CalculateFFT(float[] resultBuffer) { Complex[] input = new Complex[_storedSamples.Length]; _storedSamples.CopyTo(input, 0); FastFourierTransformation.Fft(input, Convert.ToInt32(Math.Truncate(Math.Log(_storedSamples.Length, 2)))); for (int i = 0; i <= input.Length / 2 - 1; i++) { var z = input[i]; resultBuffer[i] = (float)z.Value; } }
public override void Draw(RenderWindow window) { if (!isFirstFrame) { waitForEnter(); } else { isFirstFrame = false; } double[] samples = new double[BufferSize]; for (int i = 0; i < rendersTillStop; i++) { if (offset + BufferSize < SampleCount) { for (int counter = 0; counter < BufferSize; counter++) //one line of spectogram { samples[counter] = Samples[offset + counter]; //4k samples } offset += (int)BufferSize; //filter out high frequencies and downsample audio data var cutOffData = AudioProcessor.DownSample(samples, downSampleCoef, SampleRate); //1k samples for (int index = 0; index < BufferSize / downSampleCoef; index++) { //data[index * 2] = cutOffData[index] * hammingWindow[index]; //apply hamming window bins[index * 2] = cutOffData[index] * this.window[index]; //apply hamming window bins[index * 2 + 1] = 0d; //set 0s for Img complex values } FastFourierTransformation.FFT(bins); //apply fft //1024 = WIDTH 700=HEIGHT Render(bins, new Vector2f(100 + i * (1024 / rendersTillStop), 700), (int)BufferSize / (2 * downSampleCoef)); window.Draw(VA); } else { //Draw last frame and then close window window.Draw(instructionsText); window.Display(); waitForEnter(); window.Close(); return; } } window.Draw(instructionsText); }
private float[] ComputeFft(Complex[] samples) { float[] resultBuffer = new float[samples.Length / 2]; FastFourierTransformation.Fft(samples, mSettings.Exponent); for (int i = 0; i < samples.Length / 2; i++) { var z = samples[i]; resultBuffer[i] = (float)z.Value; } return(resultBuffer); }
/// <summary> /// Calculates the Fast Fourier Transform and stores the result in the <paramref name="fftResultBuffer" />. /// </summary> /// <param name="fftResultBuffer">The output buffer.</param> /// <returns>Returns a value which indicates whether the Fast Fourier Transform got calculated. If there have not been added any new samples since the last transform, the FFT won't be calculated. True means that the Fast Fourier Transform got calculated.</returns> public virtual bool GetFftData(Complex[] fftResultBuffer) { if (fftResultBuffer == null) { throw new ArgumentNullException(nameof(fftResultBuffer)); } Complex[] data = fftResultBuffer; Array.Copy((Array)this._storedSamples, this._currentSampleOffset, (Array)data, 0, this._storedSamples.Length - this._currentSampleOffset); Array.Copy((Array)this._storedSamples, 0, (Array)data, this._storedSamples.Length - this._currentSampleOffset, this._currentSampleOffset); FastFourierTransformation.Fft(data, this._fftSizeExponent, FftMode.Forward); bool newDataAvailable = this._newDataAvailable; this._newDataAvailable = false; return(newDataAvailable); }
public FrequenciesMode(SoundBuffer sb, int downSampleCoef) : base(sb) { VA = new VertexArray(PrimitiveType.LineStrip, (uint)(BufferSize / (2 * downSampleCoef))); font = new Font("Resources/sansation.ttf"); timeText = new Text("0", font); timeText.Position = new Vector2f(10f, 10f); timeText.CharacterSize = 30; this.downSampleCoef = downSampleCoef; window = FastFourierTransformation.GenerateHammingWindow((uint)(BufferSize / downSampleCoef)); bin = new double[(int)(BufferSize * (2d / downSampleCoef))]; Song.Loop = true; Song.Play(); }
public bool Add(float value) { if (PerformFFT && FftCalculated != null) { fftBuffer[fftPos].Real = value * 0.5f * (float)Math.Cos((2 * Math.PI) / (fftLength - 1)) * Program.SpectroAmplitude * 100; fftBuffer[fftPos].Imaginary = 0; // This is always zero with audio. fftPos++; if (fftPos >= fftLength) { fftPos = 0; FastFourierTransformation.Fft(fftBuffer, m, FftMode.Forward); FftCalculated(this, fftArgs); return(true); } } return(false); }
public override void Update() { int offset = (int)(Song.PlayingOffset.AsSeconds() * SampleRate); timeText.DisplayedString = Song.PlayingOffset.AsSeconds().ToString(); double[] samples = new double[BufferSize]; //allocate array taht will be used at downsampling Task t1 = Task.Factory.StartNew(() => SetImaginary()); if (offset + BufferSize < SampleCount) { if (ChannelCount == 2) { for (uint i = 0; i < BufferSize; i++) { samples[i] = Samples[(i + offset) * 2]; } } else { for (uint i = 0; i < BufferSize; i++) { samples[i] = Samples[(i + offset)]; } } } //Filter out frequencies and then downsamples var cutOffData = AudioProcessor.DownSample(samples, downSampleCoef, SampleRate); t1.Wait(); //t1 is working with bin array //enter complex values to the bin array for (int i = 0; i < BufferSize / downSampleCoef; i++) { bin[i * 2] = cutOffData[i] * window[i]; } FastFourierTransformation.FFT(bin); for (uint i = 0; i < BufferSize / (2 * downSampleCoef); i++) { VA[i] = new Vertex(new Vector2f((float)(i * 0.5 * downSampleCoef + 100), (float)(200 - AudioRecognitionLibrary.Tools.Arithmetics.GetComplexAbs(bin[2 * i], bin[2 * i + 1]) / 100000))); //100000 is to scale visualisation so it fits the window } }
private static void CalculateAutocorrelationUsingFFT(ReadOnlySpan <float> data, Span <float> acorr) { Span <ComplexF> dataComplex = stackalloc ComplexF[data.Length]; for (int i = 0; i < data.Length; i++) { dataComplex[i] = data[i]; //convert float into double } FastFourierTransformation.FFT(dataComplex); for (int i = 0; i < dataComplex.Length; i++) { dataComplex[i] *= ComplexF.Conjugate(dataComplex[i]); } FastFourierTransformation.FFT(dataComplex, FftMode.Backward); for (int i = 0; i < acorr.Length; i++) { acorr[i] = dataComplex[i].Real; } }
public void FFTTestFloat() { ComplexF[] array = new ComplexF[2048]; for (int i = 0; i < array.Length; i++) { array[i] = (float)Math.Sin(2.0 * Math.PI * i / array.Length); } ComplexF[] copy = new ComplexF[array.Length]; Array.Copy(array, copy, array.Length); Span <ComplexF> span = new Span <ComplexF>(array); FastFourierTransformation.FFT(span); Span <ComplexF> transformed = stackalloc ComplexF[array.Length]; span.CopyTo(transformed); FastFourierTransformation.FFT(span, FftMode.Backward); try { for (int i = 0; i < array.Length; i++) { Assert.AreEqual(copy[i].Real, array[i].Real, -1.0 / short.MinValue); Assert.AreEqual(copy[i].Imaginary, array[i].Imaginary, -1.0 / short.MinValue); } Console.WriteLine("Source,Transformed"); for (int i = 0; i < array.Length; i++) { Console.WriteLine($"{copy[i]}, {transformed[i]}"); } } catch (Exception) { Console.WriteLine("Expected,Actual"); for (int i = 0; i < array.Length; i++) { Console.WriteLine($"{copy[i]}, {array[i]}"); } throw; } }
public Spectrogram(SoundBuffer sb, int downSampleCoef) : base(sb) { //windowSize = (int)(BufferSize / downSampleCoef); window = FastFourierTransformation.GenerateHammingWindow((uint)(BufferSize / downSampleCoef)); const int secInFrame = 60; rendersTillStop = (int)(secInFrame / ((double)BufferSize / SampleRate)); this.downSampleCoef = downSampleCoef; bins = new double[(int)(BufferSize * (2d / downSampleCoef))]; //*2 because of Re+Im font = new Font("Resources/sansation.ttf"); instructionsText = new Text("Press Enter to load next frame.", font); instructionsText.Position = new Vector2f(10f, 10f); instructionsText.CharacterSize = 16; //free up space Song.Dispose(); Song = null; }
/// <summary> /// Calculates the Fast Fourier Transform and stores the result in the <paramref name="fftResultBuffer"/>. /// </summary> /// <param name="fftResultBuffer">The output buffer.</param> /// <returns>Returns a value which indicates whether the Fast Fourier Transform got calculated. If there have not been added any new samples since the last transform, the FFT won't be calculated. True means that the Fast Fourier Transform got calculated.</returns> public virtual bool GetFftData(Complex[] fftResultBuffer) { if (fftResultBuffer == null) { throw new ArgumentNullException("fftResultBuffer"); } if (fftResultBuffer.Length < (int)_fftSize) { throw new ArgumentException("Length of array must be at least as long as the specified fft size.", "fftResultBuffer"); } var input = fftResultBuffer; bool result; lock (_lockObject) { //copy from block [offset - end] to input buffer Array.Copy(_storedSamples, _currentSampleOffset, input, 0, _storedSamples.Length - _currentSampleOffset); //copy from block [0 - offset] to input buffer Array.Copy(_storedSamples, 0, input, _storedSamples.Length - _currentSampleOffset, _currentSampleOffset); for (int i = 0; i < input.Length; i++) { input[i].Real *= WindowFunction(i, input.Length); } result = _newDataAvailable; _newDataAvailable = false; } FastFourierTransformation.Fft(input, _fftSizeExponent); return(result); }
public double CalculateFFT(double[] resultBuffer, Complex[] data, int flag) { Complex[] input = new Complex[data.Length]; data.CopyTo(input, 0); double max = int.MinValue; int freq = 0; FastFourierTransformation.Fft(input, Convert.ToInt32(Math.Truncate(Math.Log(input.Length, 2)))); for (int i = 0; i <= (input.Length / 2) - 1; i++) { var z = input[i]; resultBuffer[i] = (float)z.Value; if (resultBuffer[i] >= max) { max = resultBuffer[i]; freq = i; } //Console.WriteLine(resultBuffer[i]); } if (flag == 1) { //Console.WriteLine(Math.Truncate(freq / 2 * 44100 / 16370.8381) + " Hz"); return(Math.Truncate(freq / 2 * 44100 / 16370.8381)); } else { //Console.WriteLine(Math.Truncate(freq * 44100 / 16370.8381) + " Hz"); return(Math.Truncate(freq * 44100 / 16370.8381)); } }
public AudioProcessor(Publisher publisher, string outlet = null) { WasapiCapture capture = new WasapiLoopbackCapture(CAPTURE_LATENCY); capture.Initialize(); channelNum = capture.WaveFormat.Channels; systemSampleRate = capture.WaveFormat.SampleRate; SoundInSource captureSource = new SoundInSource(capture) { FillWithZeros = false }; SimpleNotificationSource notificationSource = new SimpleNotificationSource(FluentExtensions.ToSampleSource(captureSource)) { Interval = PROCESS_WINDOW_LENGTH }; InitializeMonoBuffers(pcmBuffers, channelNum, notificationSource.BlockCount); blockBuffer = new float[notificationSource.BlockCount * channelNum]; capture.DataAvailable += (s, e) => { while (notificationSource.Read(blockBuffer, 0, notificationSource.BlockCount * channelNum) > 0) { // Extracted audio signal pcmBuffers = Deinterlacing(pcmBuffers, blockBuffer, channelNum); CSCore.Utils.Complex[] data = new CSCore.Utils.Complex[pcmBuffers[0].Length]; for (int i = 0; i < pcmBuffers.Count; i++) { data[i].Real = pcmBuffers[0][i]; data[i].Imaginary = 0; } //DetectPitch(pcmBuffers); counter++; if (counter % 30 == 0) { Console.Clear(); FastFourierTransformation.Fft(data, 8); //Console.WriteLine(data.Length); foreach (var d in data) { Console.Write(Math.Round(d.Value, 4) + " "); } Console.WriteLine(); } // TODO: Implement your model //publisher.Publish(outlet, "OUTPUT MESSAGE TO NEXT OUTLET"); } }; StartCapturingAndHold(capture); }
public void FftNewD() => FastFourierTransformation.FFT(testRegion.Span);