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); }
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 void FftNewD() => FastFourierTransformation.FFT(testRegion.Span);