public WavesCascade(int size, ComputeShader initialSpectrumShader, ComputeShader timeDependentSpectrumShader, ComputeShader texturesMergerShader, FastFourierTransform fft, Texture2D gaussianNoise) { this.size = size; this.initialSpectrumShader = initialSpectrumShader; this.timeDependentSpectrumShader = timeDependentSpectrumShader; this.texturesMergerShader = texturesMergerShader; this.fft = fft; this.gaussianNoise = gaussianNoise; KERNEL_INITIAL_SPECTRUM = initialSpectrumShader.FindKernel("CalculateInitialSpectrum"); KERNEL_CONJUGATE_SPECTRUM = initialSpectrumShader.FindKernel("CalculateConjugatedSpectrum"); KERNEL_TIME_DEPENDENT_SPECTRUMS = timeDependentSpectrumShader.FindKernel("CalculateAmplitudes"); KERNEL_RESULT_TEXTURES = texturesMergerShader.FindKernel("FillResultTextures"); initialSpectrum = FastFourierTransform.CreateRenderTexture(size, RenderTextureFormat.ARGBFloat); precomputedData = FastFourierTransform.CreateRenderTexture(size, RenderTextureFormat.ARGBFloat); displacement = FastFourierTransform.CreateRenderTexture(size, RenderTextureFormat.ARGBFloat); derivatives = FastFourierTransform.CreateRenderTexture(size, RenderTextureFormat.ARGBFloat, true); turbulence = FastFourierTransform.CreateRenderTexture(size, RenderTextureFormat.ARGBFloat, true); paramsBuffer = new ComputeBuffer(2, 8 * sizeof(float)); buffer = FastFourierTransform.CreateRenderTexture(size); DxDz = FastFourierTransform.CreateRenderTexture(size); DyDxz = FastFourierTransform.CreateRenderTexture(size); DyxDyz = FastFourierTransform.CreateRenderTexture(size); DxxDzz = FastFourierTransform.CreateRenderTexture(size); }
private static void MusicMod(List <Tuple <double, double>[]> ret, WavFile musik, WavFile musik1, MelodyModell Mod) { for (int i = 0; i < musik.DataList.Count; i++) { musik.DataList[i] = (Int16)((musik.DataList[i] * 3) / 7); } var datM = musik1.DataList.Select(v => { return((double)v); }).ToArray(); for (int i = 1; 2 * (i * Mod.BPMd) < musik.DataList.Count - Mod.BPMd; i++) { var sp = FastFourierTransform.FFTSpectr(datM, 2 * (i * Mod.BPMd), 2, 14, Mod.BPMd); int maxCh = 1; maxCh = (int)NoteDetector.DetectNote(Mod.BPMd, datM, i, Mod.BPMd); ret.Add(sp); var Bit = Generators.MusicImmitation(1 << 14, /*(int)((1 << 14) / (220 / 2.69 * ((i % 3 + 2) * 0.1)))*/ Math.Max(maxCh, 1), 5000, 10); for (int j = 0; j < Bit.Length && musik.DataList.Count > (i * Mod.BPMd + j + Mod.StartSd) * 2; j++) { musik.DataList[2 * (i * Mod.BPMd + j)] = (Int16)((Bit[j] * 4 / 7d + musik.DataList[2 * (i * Mod.BPMd + j) /*+ Mod.StartSd*/])); musik.DataList[2 * (i * Mod.BPMd + j) + 1] = (Int16)((Bit[j] * 4 / 7d + musik.DataList[2 * (i * Mod.BPMd + j) + 1 /*+ Mod.StartSd*/])); } } }
private void Add(float value) { if (PerformFFT && FftCalculated != null) { _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.HammingWindow(_fftPos, _fftLength)); _fftBuffer[_fftPos].Y = 0; _fftPos++; if (_fftPos >= _fftBuffer.Length) { _fftPos = 0; FastFourierTransform.FFT(true, _dim, _fftBuffer); FftCalculated(this, _fftArgs); } } _maxValue = Math.Max(_maxValue, value); _minValue = Math.Min(_minValue, value); _count++; if (_count >= NotificationCount && NotificationCount > 0) { MaximumCalculated?.Invoke(this, new MaxSampleEventArgs(_minValue, _maxValue)); Reset(); } }
public void Process(IEnumerable <float> buffer) { using (var enumerator = buffer.GetEnumerator()) { for (int i = 0; i < WindowSize; ++i) { enumerator.MoveNext(); fftBuffer[i].X = enumerator.Current; fftBuffer[i].Y = 0; } } FastFourierTransform.FFT(true, exponent, fftBuffer); if (Magnitudes != null) { for (int i = 0; i < WindowSize; ++i) { float mag = (float)(fftBuffer[i].X * fftBuffer[i].X + fftBuffer[i].Y * fftBuffer[i].Y); Magnitudes[i] = mag; } } if (Phases != null) { for (int i = 0; i < WindowSize; ++i) { Phases[i] = (float)Math.Atan2(fftBuffer[i].Y, fftBuffer[i].X); } } }
public void Add(float value) { //Console.WriteLine("added"); if (FftCalculated != null) { // Remember the window function! There are many others as well. fftBufferDoubled[fftPos].X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftLength)); fftBufferDoubled[fftPos].Y = 0; // This is always zero with audio. fftPos++; if (fftPos >= fftLength && fftPos % (fftLength / timeScaleFactor) == 0) { for (int i = fftPos - fftLength, j = 0; i < fftPos; i++, j++) { fftBuffer[j] = fftBufferDoubled[i]; } FastFourierTransform.FFT(true, m, fftBuffer); FftCalculated(this, fftArgs); //fftPos = 0; } if (fftPos == 2 * fftLength) { for (int i = fftLength, j = 0; j < fftLength; i++, j++) { fftBufferDoubled[j] = fftBufferDoubled[i]; } fftPos = fftLength; } } }
private static double[] calculateFFT(short[] dataPcm) { // the PCM size to be analyzed with FFT must be a power of 2 int fftPoints = 2; while (fftPoints * 2 <= dataPcm.Length) { fftPoints *= 2; } // apply a Hamming window function as we load the FFT array then calculate the FFT Complex[] fftFull = new Complex[fftPoints]; for (int i = 0; i < fftPoints; i++) { fftFull[i].X = (float)(dataPcm[i] * FastFourierTransform.HammingWindow(i, fftPoints)); } FastFourierTransform.FFT(true, (int)Math.Log(fftPoints, 2.0), fftFull); // copy the complex values into the double array that will be plotted double[] dataFft = new double[fftPoints / 2]; for (int i = 0; i < fftPoints / 2; i++) { double fftLeft = Math.Abs(fftFull[i].X + fftFull[i].Y); double fftRight = Math.Abs(fftFull[fftPoints - i - 1].X + fftFull[fftPoints - i - 1].Y); dataFft[i] = fftLeft + fftRight; } return(dataFft); }
private void TransformMethod() { if (SelectedSignal != null) { SampledSignal signal = new SampledSignal(); signal.PointsY = SelectedSignal.PointsY; signal.Name = $"{SelectedSignal.Name} {SelectedTransform}"; Stopwatch timer = new Stopwatch(); timer.Start(); switch (SelectedTransform.Substring(1, 4)) { case "F1.1": signal.ComplexPoints = FourierTransform.Transform(signal.PointsY); break; case "F1.2": FastFourierTransform fourierTransform = new FastFourierTransform(); signal.ComplexPoints = fourierTransform.Transform(signal.PointsY); break; case "F1.3": signal.ComplexPoints = WaveletTransform.Transform(signal.PointsY); break; } timer.Stop(); Time = timer.ElapsedMilliseconds; SignalCreator.AddSignal(signal); } }
public IDisposable Subscribe(IObserver <float[]> obs) { var fftBuffer = new Complex[fftLength]; var fftPos = 0; return (source.Subscribe(wave => { for (var i = 0; i < wave.ShortBufferCount; i++) { var value = wave.ShortBuffer[i]; fftBuffer[fftPos].X = (float)(value * FastFourierTransform.HannWindow(fftPos, fftLength)); fftBuffer[fftPos].Y = 0; fftPos++; if (fftPos >= fftBuffer.Length) { fftPos = 0; FastFourierTransform.FFT(true, m, fftBuffer); var result = fftBuffer .Take(fftLength / 2) .Select(GetDb) .ToArray(); obs.OnNext(result); } } }, obs.OnError, obs.OnCompleted )); }
public void FFT_TestMethod3() { FastFourierTransform FFT = new FastFourierTransform(); // test case 2 List <float> Samples = new List <float> { 1, 3, 5, 7, 9, 11, 13, 15 }; FFT.InputTimeDomainSignal = new DSPAlgorithms.DataStructures.Signal(Samples, false); FFT.InputSamplingFrequency = 4; var FrequenciesAmplitudes = new List <float> { 64, 20.9050074380220f, 11.3137084989848f, 8.65913760233915f, 8, 8.65913760233915f, 11.3137084989848f, 20.9050074380220f }; var FrequenciesPhaseShifts = new List <float> { 0, 1.96349540849362f, 2.35619449019235f, 2.74889357189107f, -3.14159265358979f, -2.74889357189107f, -2.35619449019235f, -1.96349540849362f }; var Frequencies = new List <float> { 0, 1, 2, 3, 4, 5, 6, 7 }; FFT.Run(); Assert.IsTrue(UnitTestUtitlities.SignalsSamplesAreEqual(FrequenciesAmplitudes, FFT.OutputFreqDomainSignal.FrequenciesAmplitudes) && UnitTestUtitlities.SignalsPhaseShiftsAreEqual(FrequenciesPhaseShifts, FFT.OutputFreqDomainSignal.FrequenciesPhaseShifts)); }
public void Generate(RingBuffer <float> ringbuffer, int offsetIntoPast = 0, bool mirror = false) { if (mirror) { ringbuffer.CopyLastTo(fftTempSamples, 0, size / 2, offsetIntoPast); for (int i = 0; i < size / 2; i++) { fftTempSamples[size - i - 1] = fftTempSamples[i]; } } else { ringbuffer.CopyLastTo(fftTempSamples, 0, size, offsetIntoPast); } for (int i = 0; i < size; i++) { fftTempComplex[i].X = fftTempSamples[i] * fftWindowShape[i]; fftTempComplex[i].Y = 0f; } FastFourierTransform.FFT(true, fft_m, fftTempComplex); // generate spectrum }
void Update() { float attack = Mathf.Pow(m_attack, 2.3f) * -128; float decaySpeed = m_decay < 1.0f ? m_decay * 10 + 0.5f : 100.0f; Lasp.AudioInput.RetrieveWaveform(Lasp.FilterType.Bypass, m_rawBuffer); SetLine(m_wavLine, m_rawBuffer); FastFourierTransform.FFT(true, CONST_M, m_rawBuffer, m_rawBufferComplex); for (int i = 0; i < m_rawBuffer.Length / 2; i++) { var newVal = Mathf.Sqrt(m_rawBuffer[i] * m_rawBuffer[i] + m_rawBufferComplex[i] * m_rawBufferComplex[i]); // attack if (m_attack < 1.0f) { newVal -= (newVal - m_fftOutput[i]) * Mathf.Exp(attack * Time.deltaTime); } // decay m_fftOutput[i] = Mathf.Max(newVal, m_fftOutput[i] - Time.deltaTime * decaySpeed); m_rawBufferComplex[i] = 0; } SetLine(m_fftLine, m_fftOutput); }
private void DataAvailable(object sender, WaveInEventArgs e) { // Convert byte[] to float[]. float[] data = ConvertByteToFloat(e.Buffer, e.BytesRecorded); // For all data. Skip right channel on stereo (i += this.Format.Channels). for (int i = 0; i < data.Length; i += this.Format.Channels) { this._fftBuffer[_fftPos].X = (float)(data[i] * FastFourierTransform.HannWindow(_fftPos, _fftLength)); this._fftBuffer[_fftPos].Y = 0; this._fftPos++; if (this._fftPos >= this._fftLength) { this._fftPos = 0; // NAudio FFT implementation. FastFourierTransform.FFT(true, this._m, this._fftBuffer); // Copy to buffer. lock (this._lock) { for (int c = 0; c < this._fftLength; c++) { float amplitude = (float)Math.Sqrt(this._fftBuffer[c].X * this._fftBuffer[c].X + this._fftBuffer[c].Y * this._fftBuffer[c].Y); this._lastFftBuffer[c] = amplitude; } this._fftBufferAvailable = true; } } } }
public void Load(string path) { FourierSounds = new List <Complex[]>(); using (var reader = new WaveFileReader(path)) { SampleRate = reader.WaveFormat.SampleRate; Ts = 1.0 / SampleRate; FftLength = 4096; //FftLength = 2048; //FftLength = 1024; Time = reader.TotalTime.TotalSeconds; var channels = reader.WaveFormat.Channels; var m = (int)Math.Log(FftLength, 2.0); var readBuffer = new byte[reader.Length]; reader.Read(readBuffer, 0, readBuffer.Length); var data = ConvertByteToFloat(readBuffer, readBuffer.Length); for (var j = 0; j < data.Length / FftLength; j++) { var sampleBuffer = data.Skip(j * FftLength).Take(FftLength).ToList(); var fftBuffer = new Complex[FftLength]; var fftPos = 0; for (var i = 0; i < FftLength; i++) { fftBuffer[fftPos].X = (float)(sampleBuffer[i] * FastFourierTransform.HammingWindow(i, FftLength)); fftBuffer[fftPos].Y = 0; fftPos++; } FastFourierTransform.FFT(true, m, fftBuffer); FourierSounds.Add(fftBuffer); } } }
public void Add(float value) { if (PerformFFT && FftCalculated != null) { Complex tempcomplex; tempcomplex.X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftBuffer.Length)); tempcomplex.Y = 0; fftBufferQueue.Enqueue(tempcomplex); // fftBuffer[fftPos].X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftBuffer.Length)); // fftBuffer[fftPos].Y = 0; if (fftPos < fftBuffer.Length) { fftPos++; } else { fftBuffer = fftBufferQueue.ToArray(); FastFourierTransform.FFT(true, m, fftBuffer); FftCalculated(this, fftArgs); fftBufferQueue.Dequeue(); } } maxValue = Math.Max(maxValue, value); minValue = Math.Min(minValue, value); count++; if (count >= NotificationCount && NotificationCount > 0) { if (MaximumCalculated != null) { MaximumCalculated(this, new MaxSampleEventArgs(minValue, maxValue)); } Reset(); } }
private void Add(float value) { if (PerformFFT && FftCalculated != null) { fftBuffer[fftPos].X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftLength)); fftBuffer[fftPos].Y = 0; fftPos++; if (fftPos >= fftBuffer.Length) { fftPos = 0; // 1024 = 2^10 FastFourierTransform.FFT(true, m, fftBuffer); FftCalculated(this, fftArgs); } } maxValue = Math.Max(maxValue, value); minValue = Math.Min(minValue, value); count++; if (count >= NotificationCount && NotificationCount > 0) { MaximumCalculated?.Invoke(this, new MaxSampleEventArgs(minValue, maxValue)); Reset(); } }
public void Add(float value) { if (PerformFFT && FftCalculated != null) { fftBuffer[fftPos].X = value; fftBuffer[fftPos].Y = 0; fftPos++; if (fftPos >= fftBuffer.Length) { fftPos = 0; // 1024 = 2^10 FastFourierTransform.FFT(true, 10, fftBuffer); FftCalculated(this, fftArgs); } } maxValue = Math.Max(maxValue, value); minValue = Math.Min(minValue, value); count++; if (count >= NotificationCount && NotificationCount > 0) { if (MaximumCalculated != null) { MaximumCalculated(this, new MaxSampleEventArgs(minValue, maxValue)); } Reset(); } }
private void Add(float value) { if (PerformFFT && FftCalculated != null) { int currentFFTPos = fftPos[currentFilter]; fftBuffer[currentFilter][currentFFTPos].X = (float)(value * FastFourierTransform.HammingWindow(currentFFTPos, fftLength)); fftBuffer[currentFilter][currentFFTPos].Y = 0; currentFFTPos++; if (currentFFTPos >= fftBuffer[currentFilter].Length) { currentFFTPos = 0; // 1024 = 2^10 FastFourierTransform.FFT(true, m, fftBuffer[currentFilter]); fftArgs.currentFilterIndex = currentFilter; fftArgs.Result = fftBuffer[currentFilter]; FftCalculated(this, fftArgs); } fftPos[currentFilter] = currentFFTPos; } /*maxValue = Math.Max(maxValue, value); * minValue = Math.Min(minValue, value); * count++; * if (count >= NotificationCount && NotificationCount > 0) * { * if (MaximumCalculated != null) * { * MaximumCalculated(this, new MaxSampleEventArgs(minValue, maxValue)); * } * Reset(); * } */ }
private void Add(float value) { if (PerformFFT && FftCalculated != null) { fftBuffer[fftPos].X = /*value;//*/ (float)(value * FastFourierTransform.BlackmannHarrisWindow(fftPos, fftLength)); fftBuffer[fftPos].Y = 0; fftPos++; if (fftPos >= fftBuffer.Length) { fftPos = 0; // 1024 = 2^10 FastFourierTransform.FFT(true, m, fftBuffer); for (int i = 0; i < fftBuffer.Length; i++) { Complex c = fftBuffer[i]; double intensityDB = (4 + Math.Log(Math.Sqrt(c.X * c.X + c.Y * c.Y), 16)) / 4; //double minDB = -32; //if (intensityDB < minDB) intensityDB = minDB; //double percent =- (intensityDB - minDB) / minDB; double percent = Math.Max(0, intensityDB); _spectrum[i] = (float)Math.Max(_spectrum[i] - downspeed, percent); } FftCalculated(this, fftArgs); } } maxValue = Math.Max(maxValue, value); minValue = Math.Min(minValue, value); count++; if (count >= NotificationCount && NotificationCount > 0) { MaximumCalculated?.Invoke(this, new MaxSampleEventArgs(minValue, maxValue)); Reset(); } }
public static void AddSample(float value) { switch (Window) { case FFTWindow.BlackmannHarris: _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.BlackmannHarrisWindow(_fftPos, _fftLength)); break; case FFTWindow.Hamming: _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.HammingWindow(_fftPos, _fftLength)); break; case FFTWindow.Hann: _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.HannWindow(_fftPos, _fftLength)); break; } _fftBuffer[_fftPos].Y = 0; _fftPos++; if (_fftPos >= _fftLength) { _fftPos = 0; FastFourierTransform.FFT(true, _m, _fftBuffer); FftCalculated(); } }
private void FFTWindowChange(Complex[] channelData, FFTWindow window) { switch (window) { case FFTWindow.HannWindow: for (int i = 0; i < channelData.Length; i++) { channelData[i].X = (float)(channelData[i].X * FastFourierTransform.HannWindow(i, bufferSize)); } break; case FFTWindow.HammingWindow: for (int i = 0; i < channelData.Length; i++) { channelData[i].X = (float)(channelData[i].X * FastFourierTransform.HammingWindow(i, bufferSize)); } break; case FFTWindow.BlackmannHarrisWindow: for (int i = 0; i < channelData.Length; i++) { channelData[i].X = (float)(channelData[i].X * FastFourierTransform.BlackmannHarrisWindow(i, bufferSize)); } break; } FastFourierTransform.FFT(false, binaryExponentitation, channelData); }
private void ProcessAudioData() { while (!_cancellationTokenSource.Token.IsCancellationRequested) { var data = _dataFromAsioCollection.Take(_cancellationTokenSource.Token); var remainedElements = FftBlockSize - _collectedSoundData.Count; for (var i = 0; i < Math.Min(remainedElements, data.Length); i++) { //var multiplier = FastFourierTransform.BlackmannHarrisWindow(collectedData.Count, blockSize); //var multiplier = // Gausse(_collectedData.Count, FftBlockSize); // показывает меньше растекания в соседние гармоники //_collectedData.Add((float)(multiplier * data[i])); _collectedSoundData.Add(data[i]); } if (_collectedSoundData.Count == FftBlockSize) { var complexData = _collectedSoundData .Select(x => new Complex { X = x, Y = 0, }) .ToArray(); FastFourierTransform.FFT(true, SignalProcessor.Log2OfBinCount, complexData); _action(_collectedSoundData.ToArray(), complexData); _collectedSoundData.Clear(); } } }
private void WaveIn_DataAvailable(object sender, WaveInEventArgs e) { byte[] buffer = e.Buffer; int bytesGrabados = e.BytesRecorded; int numMuestras = bytesGrabados / 2; int exponente = 0; int numeroBits = 0; do { exponente++; numeroBits = (int)Math.Pow(2, exponente); } while (numeroBits < numMuestras); exponente -= 1; numeroBits = (int)Math.Pow(2, exponente); Complex[] muestrasComplejas = new Complex[numeroBits]; for (int i = 0; i < bytesGrabados; i += 2) { short muestra = (short)(buffer[i + 1] << 8 | buffer[i]); float muestra32bits = (float)muestra / 32768.0f; if (i / 2 < numeroBits) { muestrasComplejas[i / 2].X = muestra32bits; } } FastFourierTransform.FFT(true, exponente, muestrasComplejas); float[] valoresAbsolutos = new float[muestrasComplejas.Length]; for (int i = 0; i < muestrasComplejas.Length; i++) { valoresAbsolutos[i] = (float) Math.Sqrt( (muestrasComplejas[i].X * muestrasComplejas[i].X) + (muestrasComplejas[i].Y * muestrasComplejas[i].Y)); } int indiceValorMaximo = valoresAbsolutos.ToList().IndexOf( valoresAbsolutos.Max()); float frecuenciaFundamental = (float)(indiceValorMaximo * formato.SampleRate) / (float)valoresAbsolutos.Length; //waveIn.StopRecording(); if (frecuenciaFundamental < 700) { rotar(rotacionPersonaje.Angle - 10); } lblHertz.Text = frecuenciaFundamental.ToString("N") + " Hz"; }
public static double[] getSpectrum(AudioFile audio, long startPos) { float[] buff = new float[1024]; if (startPos + 1024 < audio.FloatSamples.Length) { for (int i = 0; i < 1024; i++) { buff[i] = audio.FloatSamples[startPos + i]; } } Complex[] data = new Complex[1024]; for (int i = 0; i < 1024; i++) { data[i].X = buff[i]; data[i].Y = 0.0f; } FastFourierTransform.FFT(true, 10, data); double[] res = new double[512]; for (int i = 0; i < 512; i++) { res[i] = Math.Sqrt(data[i].X * data[i].X + data[i].Y * data[i].Y); } return(res); }
private void Add(float r, float i) { _count++; if (FftCalculated != null) { _fftBuffer[_fftPos].X = (float)(r * FastFourierTransform.HammingWindow(_fftPos, _fftLength)); _fftBuffer[_fftPos].Y = (float)(i * FastFourierTransform.HammingWindow(_fftPos, _fftLength)); _fftPos++; if (_fftPos >= _fftBuffer.Length) { CalculateFft(); } } _maxValue = Math.Max(_maxValue, r); _minValue = Math.Min(_minValue, r); if (_count < _notificationCount || _notificationCount <= 0) { return; } if (MaximumCalculated != null) { MaximumCalculated(this, new MaxSampleEventArgs(_minValue, _maxValue)); } Reset(); }
private void Add(float value) { if (PerformFFT && FftCalculated != null) { fftBuffer[fftPos].X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftLength)); //fftBuffer[fftPos].X = (float)(value * FastFourierTransform.BlackmannHarrisWindow(fftPos, fftLength)); //fftBuffer[fftPos].X = value; fftBuffer[fftPos].Y = 0; fftPos++; if (fftPos >= fftBuffer.Length) { fftPos = 0; //var copyBuffer = new Complex[fftBuffer.Length]; //fftPos = fftBuffer.Length / 2; //for (int i = 0; i < fftBuffer.Length / 2; i++) //{ // copyBuffer[i] = fftBuffer[fftBuffer.Length / 2 + i].Clone(); //} // nadav - copy ffts var newFFT = new Complex[fftBuffer.Length]; for (int i = 0; i < fftBuffer.Length; i++) { newFFT[i] = fftBuffer[i].Clone(); } FFTDataCalculated.Add(newFFT); // 1024 = 2^10 FastFourierTransform.FFT(false, m, fftBuffer); FftCalculated(this, new FftEventArgs(fftBuffer)); //fftBuffer = copyBuffer; if (fftBuffer.Any(c => c.X != 0 || c.Y != 0)) { var firstNotNul = fftBuffer.FirstOrDefault(c => c.X != 0 || c.Y != 0); var x = firstNotNul.X; var y = firstNotNul.Y; } } } maxValue = Math.Max(maxValue, value); minValue = Math.Min(minValue, value); count++; if (count >= NotificationCount && NotificationCount > 0) { if (MaximumCalculated != null) { MaximumCalculated(this, new MaxSampleEventArgs(minValue, maxValue)); } Reset(); } }
private static void ProcessAudioData() { while (!CancellationTokenSource.Token.IsCancellationRequested) { var data = Collection.Take(CancellationTokenSource.Token); var remainedElements = FftBlockSize - CollectedData.Count; for (var i = 0; i < Math.Min(remainedElements, data.Length); i++) { //var multiplier = FastFourierTransform.BlackmannHarrisWindow(collectedData.Count, blockSize); var multiplier = Gausse(CollectedData.Count, FftBlockSize); // показывает меньше растекания в соседние гармоники CollectedData.Add((float)(multiplier * data[i])); } if (CollectedData.Count == FftBlockSize) { var complexData = CollectedData.Select(x => new Complex { X = x, Y = 0, }).ToArray(); CollectedData.Clear(); FastFourierTransform.FFT(true, Log2OfBinCount, complexData); var freqs = ComplexToFreqSpectrum(complexData, NoiseMagnitude, ToneGenerator - MaxDiscoverDelta * 2, ToneGenerator + MaxDiscoverDelta * 2); if (freqs.Any()) { var loudestFreqs = freqs.OrderByDescending(f => f.Value); #if DEBUG Console.WriteLine(topFreqs.Aggregate(">>>", (a, f) => a + ", " + (int)f.Key)); #endif if (!loudestFreqs.Any(f => Math.Abs(f.Key - ToneGenerator) <= (double)SampleRate / FftBlockSize)) { Console.WriteLine(DateTime.Now.ToLocalTime() + " WRONG SIGNAL FREQUENCY"); } else { var movementIsDiscovered = loudestFreqs.Any(f => { var delta = Math.Abs(f.Key - ToneGenerator); return(delta >= MinDiscoverDelta && delta < MaxDiscoverDelta); }); if (movementIsDiscovered) { AlarmEvent.Set(); Console.WriteLine(DateTime.Now.ToLocalTime() + " ALERT"); } } } else { Console.WriteLine(DateTime.Now.ToLocalTime() + " NO SIGNAL"); } } } CancellationTokenSource.Token.ThrowIfCancellationRequested(); }
public Complex[] RFFTProcess(Complex[] sdata) { var res = new float[fftsampleRange]; FastFourierTransform.FFT(false, (int)Math.Log(fftsampleRange, 2), sdata); return(sdata); }
public static ulong[] GetFingerprint(string filename) { using var data = new AudioFileReader(filename); ISampleProvider mono; if (data.WaveFormat.Channels == 2) { var monoProvider = new StereoToMonoSampleProvider(data) { LeftVolume = 1.0f, RightVolume = 1.0f }; mono = monoProvider.ToMono(); } else { mono = data; } var buffer = new float[ChunkSize]; var transformed = new List <Complex[]>(); var m = (int)Math.Log(ChunkSize, 2.0); while (true) { var read = mono.Read(buffer, 0, ChunkSize); if (read == 0) { break; } var complex = new Complex[ChunkSize]; for (var i = 0; i < read; i++) { complex[i].X = buffer[i]; complex[i].Y = 0; } FastFourierTransform.FFT(false, m, complex); transformed.Add(complex); } var keyPoints = new List <ulong>(); foreach (var item in transformed) { keyPoints.Add(GetKeyPoints(item)); } return(keyPoints.ToArray()); }
private void WaveIn_DataAvailable(object sender, WaveInEventArgs e) { byte[] buffer = e.Buffer; int bytesGrabados = e.BytesRecorded; float acumulador = 0.0f; double numeroMuestras = bytesGrabados / 2; int exponente = 1; int numeroMuestrasComplejas = 0; int bitsMaximos = 0; do { bitsMaximos = (int)Math.Pow(2, exponente); exponente++; } while (bitsMaximos < numeroMuestras); numeroMuestrasComplejas = bitsMaximos / 2; exponente -= 2; Complex[] señalCompleja = new Complex[numeroMuestrasComplejas]; for (int i = 0; i < bytesGrabados; i += 2) { short muestra = (short)(buffer[i + 1] << 8 | buffer[i]); float muestra32bits = (float)muestra / 32768.0f; acumulador += Math.Abs(muestra32bits); if (i / 2 < numeroMuestrasComplejas) { señalCompleja[i / 2].X = muestra32bits; } } float promedio = acumulador / (bytesGrabados / 2.0f); if (promedio > 0) { FastFourierTransform.FFT(true, exponente, señalCompleja); float[] valoresAbsolutos = new float[señalCompleja.Length]; for (int i = 0; i < señalCompleja.Length; i++) { valoresAbsolutos[i] = (float)Math.Sqrt( (señalCompleja[i].X * señalCompleja[i].X) + (señalCompleja[i].Y * señalCompleja[i].Y) ); } int indiceSeñalConMasPresencia = valoresAbsolutos.ToList().IndexOf(valoresAbsolutos.Max()); frecuenciaFundamental = (float)(indiceSeñalConMasPresencia * waveIn.WaveFormat.SampleRate) / (float)valoresAbsolutos.Length; lblHertz.Text = frecuenciaFundamental.ToString("n") + " Hz"; } }
private void GenerateFFTWindow() { for (int i = 0; i < size; i++) { fftWindowShape[i] = (float)FastFourierTransform.HammingWindow(i, size); //fftWindowShape[i] = (float)FastFourierTransform.BlackmannHarrisWindow(i, size); //fftWindowShape[i] = (float)FastFourierTransform.HannWindow(i, size); } }
static void Main(string[] args) { var len = 4096; var pow = 12; FastFourierTransform fft = new FastFourierTransform(len,len, pow); var arr = new Complex[len]; for (int i = 0; i < len; i++) { arr[i] = Math.Sin(2*Math.PI*i/len); } fft.Transform(arr, true); }
public void FastFourierTransformTest() { FastFourierTransform df = new FastFourierTransform(); int i = 1; FourierTestPair[] testToDo = new FourierTestPair[] { new FourierTestPair( new double[,] { { 1.0, 1.0 }, { 1.0, 1.0 } }, new ComplexImage(new double[,] { { 0.0, 0.0 }, { 0.0, 4.0 } } )) // 2x2 ,new FourierTestPair( new double[,] { { 2.0, 2.0 }, { 1.0, 1.0 } }, new ComplexImage(new double[,] { { 0.0, 2.0 }, { 0.0, 6.0 } } )) //2x2 //// FastFourierTransform can't handle images that do not have sizes base-2 //,new FourierTestPair( // new double[,] { { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } }, // new ComplexImage(new Complex[,] { // { new Complex(1.0, 0.0), new Complex(1.0, 1.7321), new Complex(1.0, - 1.7321) }, // { new Complex(1.0, 1.7321), new Complex(-2.0, 3.4641), new Complex(4.0, 0) }, // { new Complex(1.0, -1.7321), new Complex(4.0, 0.0), new Complex(-2.0, - 3.4641) } } )) //3x3 //,new FourierTestPair( // new double[,] { { 2.0, 2.0, 2.0 }, { 1.0, 1.0, 1.0 }, { 0.0, 0.0, 0.0 } }, // new ComplexImage(new Complex[,] { // { new Complex(1.0, 0.0), new Complex(1.0, 1.7321), new Complex(1.0, - 1.7321) }, // { new Complex(2.5, 0.8660), new Complex(1.0, 5.1962), new Complex(4.0, - 3.4641) }, // { new Complex(2.5, -0.8660), new Complex(4.0, 3.4641), new Complex(1.0, - 5.1962) } } )) //3x3 ,new FourierTestPair( new double[,] { { 3.0, 3.0, 3.0, 3.0 }, { 2.0, 2.0, 2.0, 2.0 }, { 1.0, 1.0, 1.0, 1.0 }, { 0.0, 0.0, 0.0, 0.0 } }, new ComplexImage(new Complex[,] { { new Complex( 0.0, 0.0), new Complex(0.0, 0.0), new Complex(8.0, 0.0) , new Complex(0.0, 0.0)}, { new Complex( 0.0, 0.0), new Complex(0.0, 0.0), new Complex(8.0, 8.0) , new Complex(0.0, 0.0)}, { new Complex( 0.0, 0.0), new Complex(0.0, 0.0), new Complex(24.0, 0.0) , new Complex(0.0, 0.0)}, { new Complex( 0.0, 0.0), new Complex(0.0, 0.0), new Complex(8.0, -8.0) , new Complex(0.0, 0.0)}} )) //4x4 }; /* * TEMPLATE: * new FourierTestPair( new double[,] { { 1.0, 1.0 }, { 1.0, 1.0 } }, new ComplexImage(new double[,] { { 4.0, 0.0 }, { 0.0, 0.0 } } )) */ foreach (FourierTestPair test in testToDo) { TransformTestTestData(df, i, test.Item1, test.Item2); i += 1; } }
void Awake() { _source = GetComponent<AudioSource>(); _fft = new FastFourierTransform(_source, 1024); _cubes = new List<GameObject>(); for (var i = 0; i < Count; i++) { var angle = 2 * Mathf.PI * i / Count; var position = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * Radius; var rotation = Quaternion.Euler(0, Mathf.Rad2Deg * -angle, 0); var cube = Instantiate(Prefab, position, rotation) as GameObject; _cubes.Add(cube); } }