Instantly share code, notes, and snippets.

# ValdemarOrn/SimpleDFT.md

Last active November 1, 2021 20:28
Show Gist options
• Save ValdemarOrn/1646627 to your computer and use it in GitHub Desktop.
A very simple Discrete Fourier Transform algorithm (not suitable for real-time processing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleDFT { class SimpleDFT { static void Main(string[] args) { var s = new double[1024]; for(int i=0; i /// Provides the Discrete Fourier Transform for a real-valued input signal /// /// the signal to transform /// the maximum number of partials to calculate. If not value is given it defaults to input/2 /// The Cos and Sin components of the signal, respectively public static Tuple DFT(double[] input, int partials = 0) { int len = input.Length; double[] cosDFT = new double[len / 2 + 1]; double[] sinDFT = new double[len / 2 + 1]; if (partials == 0) partials = len / 2; for (int n = 0; n <= partials ; n++) { double cos = 0.0; double sin = 0.0; for (int i = 0; i < len; i++) { cos += input[i] * Math.Cos(2 * Math.PI * n / len * i); sin += input[i] * Math.Sin(2 * Math.PI * n / len * i); } cosDFT[n] = cos; sinDFT[n] = sin; } return new Tuple(cosDFT, sinDFT); } /// /// Takes the real-valued Cos and Sin components of Fourier transformed signal and reconstructs the time-domain signal /// /// Array of cos components, containing frequency components from 0 to pi. sin.Length must match cos.Length /// Array of sin components, containing frequency components from 0 to pi. sin.Length must match cos.Length /// /// The length of the output signal. /// If len < (partials-1)*2 then frequency data will be lost in the output signal. /// if no len parameter is given it defaults to (partials-1)*2 /// /// the real-valued time-domain signal public static double[] IDFT(double[] cos, double[] sin, int len = 0) { if (cos.Length != sin.Length) throw new ArgumentException("cos.Length and sin.Length bust match!"); if (len == 0) len = (cos.Length - 1) * 2; double[] output = new double[len]; int partials = sin.Length; if (partials > len / 2) partials = len / 2; for (int n = 0; n <= partials; n++) { for (int i = 0; i < len; i++) { output[i] += Math.Cos(2 * Math.PI * n / len * i) * cos[n]; output[i] += Math.Sin(2 * Math.PI * n / len * i) * sin[n]; } } return output; } } }

# Real-Value Discrete Fourier Transformation in C#

This is a very basic version of a Discrete Fourier Transformation.

Not suitable for real-time analysis