public InverseSTFT(IAudioWriterStream stream, int windowSize, int hopSize, int fftSize, WindowType windowType, float windowNormalizationFactor) : base(stream, windowSize, hopSize) { if (fftSize < windowSize) { throw new ArgumentOutOfRangeException("fftSize must be >= windowSize"); } frameBuffer = new float[fftSize]; fft = FFTFactory.CreateInstance(fftSize); synthesisWindow = WindowUtil.GetFunction(windowType, windowSize, windowNormalizationFactor); }
/// <summary> /// Initializes a new STFT for the specified stream with the specified window, hop, and FFT size. /// An FFT size larger than the window size can be used to increase frequency resolution. Window will be right-padded with zeros for FFT. /// </summary> /// <param name="stream">the stream to read the audio data to process from</param> /// <param name="windowSize">the window size in the dimension of samples</param> /// <param name="hopSize">the hop size in the dimension of samples</param> /// <param name="fftSize">the FFT size, must be >= windowSize</param> /// <param name="windowType">the type of the window function to apply</param> /// <param name="outputFormat">format of the output data, e.g. raw FFT complex numbers or dB spectrum</param> public STFT(IAudioStream stream, int windowSize, int hopSize, int fftSize, WindowType windowType, OutputFormat outputFormat, int bufferSize = DEFAULT_STREAM_INPUT_BUFFER_SIZE) : base(stream, windowSize, hopSize, windowType, bufferSize) { if (fftSize < windowSize) { throw new ArgumentOutOfRangeException("fftSize must be >= windowSize"); } frameBuffer = new float[fftSize]; fftBuffer = new float[fftSize]; Array.Clear(frameBuffer, 0, frameBuffer.Length); // init with zeros (assure zero padding) fft = FFTFactory.CreateInstance(fftSize); this.outputFormat = outputFormat; }
public void ComputeTest_Impulse() { Complex[] data = new Complex[4]; data[0] = new Complex(0.25, 0); data[1] = new Complex(0.25, 0); data[2] = new Complex(0.25, 0); data[3] = new Complex(0.25, 0); var output = IFFT.Compute(data); Assert.AreEqual(1, output[0].Real); Assert.AreEqual(0, output[0].Imaginary); for (int i = 1; i < data.Length; i++) { Assert.AreEqual(0, output[i].Real); Assert.AreEqual(0, output[i].Imaginary); } }
public void ComputeTest_Impulse_Nyq_Bounded() { Complex[] data = new Complex[3]; data[0] = new Complex(0.25, 0); data[1] = new Complex(0.25, 0); data[2] = new Complex(0.25, 0); var output = IFFT.Compute(data, true, true); Assert.AreEqual(1, output[0].Real); Assert.AreEqual(0, output[0].Imaginary); for (int i = 1; i < output.Length; i++) { Assert.AreEqual(0, output[i].Real); Assert.AreEqual(0, output[i].Imaginary); } Assert.AreEqual(4, output.Length); //not scaled data[0] = new Complex(1, 0); data[1] = new Complex(1, 0); data[2] = new Complex(1, 0); output = IFFT.Compute(data, false, true); Assert.AreEqual(1, output[0].Real); Assert.AreEqual(0, output[0].Imaginary); for (int i = 1; i < output.Length; i++) { Assert.AreEqual(0, output[i].Real); Assert.AreEqual(0, output[i].Imaginary); } Assert.AreEqual(4, output.Length); }
public FFTAnalyzer(int windowSize) { WindowSize = windowSize; fft = FFTFactory.CreateInstance(windowSize); windowFunctionNormalizationDecibelOffset = 0; }
private static void FrequencyDistributionBlockProcess(float[] buffer, double[] target, IFFT fft) { windowFunction.Apply(buffer); fft.Forward(buffer); for (int i = 0; i < buffer.Length; i += 2) { buffer[i / 2] = FFTUtil.CalculateMagnitude(buffer[i], buffer[i + 1]) / buffer.Length * 2; } for (int i = 0; i < target.Length; i++) { target[i] += buffer[i]; } }