• Stars
    star
    313
  • Rank 133,714 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created over 4 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A .NET Standard library for computing the Fast Fourier Transform (FFT) of real or complex data

FftSharp

CI/CD

FftSharp is a collection of Fast Fourier Transform (FFT) tools for .NET. FftSharp is provided under the permissive MIT license so it is suitable for use in commercial applications. FftSharp targets .NET Standard and has no dependencies so it can be easily used in cross-platform .NET Framework and .NET Core applications.

Quickstart

// Begin with an array containing sample data
double[] signal = FftSharp.SampleData.SampleAudio1();

// Shape the signal using a Hanning window
var window = new FftSharp.Windows.Hanning();
window.ApplyInPlace(signal);

// Calculate the FFT as an array of complex numbers
System.Numerics.Complex[] spectrum = FftSharp.FFT.Forward(signal);

// or get the magnitude (unitsĀ²) or power (dB) as real numbers
double[] magnitude = FftSharp.FFT.Magnitude(spectrum);
double[] power = FftSharp.FFT.Power(spectrum);
Signal Windowed Signal FFT

Sample Data

// sample audio with tones at 2, 10, and 20 kHz plus white noise
double[] signal = FftSharp.SampleData.SampleAudio1();
int sampleRate = 48_000;
double samplePeriod = sampleRate / 1000.0;

// plot the sample audio
ScottPlot.Plot plt = new();
plt.AddSignal(signal, samplePeriod);
plt.YLabel("Amplitude");
plt.SaveFig("time-series.png");

Spectral Magnitude and Power Density

Most people performing FFT operations are interested in calculating magnitude or power of their signal with respect to frequency. Magnitude units are the square of the original units, and power is in decibels.

Frequency of each point is a linear range between zero and half the sample rage (Nyquist frequency). A helper function makes it easy to get an array of frequencies (Hz units) to match the FFT that was generated.

// sample audio with tones at 2, 10, and 20 kHz plus white noise
double[] signal = FftSharp.SampleData.SampleAudio1();
int sampleRate = 48_000;

// calculate the power spectral density using FFT
System.Numerics.Complex[] spectrum = FftSharp.FFT.Forward(audio);
double[] psd = FftSharp.FFT.Power(spectrum);
double[] freq = FftSharp.FFT.FrequencyScale(psd.Length, sampleRate);

// plot the sample audio
ScottPlot.Plot plt = new ScottPlot.Plot();
plt.AddScatterLines(freq, psd);
plt.YLabel("Power (dB)");
plt.XLabel("Frequency (Hz)");
plt.SaveFig("periodogram.png");

FFT using Complex Numbers

If you are writing a performance application or just enjoy working with real and imaginary components of complex numbers, you can build your own complex array perform FFT operations on it in place:

System.Numerics.Complex[] buffer =
{
    new(real: 42, imaginary: 12),
    new(real: 96, imaginary: 34),
    new(real: 13, imaginary: 56),
    new(real: 99, imaginary: 78),
};

FftSharp.Transform.FFT(buffer);

Filtering

The FftSharp.Filter module has methods to apply low-pass, high-pass, band-pass, and band-stop filtering. This works by converting signals to the frequency domain (using FFT), zeroing-out the desired ranges, performing the inverse FFT (iFFT), and returning the result.

double[] audio = FftSharp.SampleData.SampleAudio1();
double[] filtered = FftSharp.Filter.LowPass(audio, sampleRate, maxFrequency: 2000);

Windowing

Signals are often windowed prior to FFT analysis. Windowing is essentially multiplying the waveform by a bell-shaped curve prior to analysis, improving the frequency resolution of the FFT output.

The Hanning window is the most common window function for general-purpose FFT analysis. Other window functions may have different scallop loss or spectral leakage properties. For more information review window functions on Wikipedia.

double[] signal = FftSharp.SampleData.SampleAudio1();

var window = new FftSharp.Windows.Hanning();
double[] windowed = window.Apply(signal);
Hanning Window Power Spectral Density

Windowing signals prior to calculating the FFT improves signal-to-noise ratio at lower frequencies, making power spectrum peaks easier to resolve.

No Window Power Spectral Density

Window Functions

This chart (adapted from Understanding FFT Windows) summarizes windows commonly used for FFT analysis.

Window Use Case Frequency Resolution Spectral Leakage Amplitude Accuracy
Barlett Random Good Fair Fair
Blackman Random Poor Best Good
Cosine Random Fair Fair Fair
Flat Top Sine waves Poor Good Best
Hanning Random Good Good Fair
Hamming Random Good Fair Fair
Kaiser Random Fair Good Good
Rectangular Transient Best Poor Poor
Tukey Transient Good Poor Poor
Welch Random Good Good Fair

Demo Application

A sample application is included with this project that interactively displays an audio signal next to its FFT using different windowing functions.

Microphone Demo

One of the demos included is a FFT microphone analyzer which continuously monitors a sound card input device and calculates the FFT and displays it in real time.

Spectrogram

A spectrogram is a visual representation of the spectrum of frequencies of a signal as it varies with time. Spectrograms are created by computing power spectral density of a small window of an audio signal, moving the window forward in time, and repeating until the end of the signal is reached. In a spectrogram the horizontal axis represents time, the vertical axis represents frequency, and the pixel intensity represents spectral magnitude or power.

Spectrogram is a .NET library for creating spectrograms.

I'm sorry Dave... I'm afraid I can't do that

More Repositories

1

Csharp-Data-Visualization

Resources for visualizing data using C# and the .NET platform
C#
1,009
star
2

Spectrogram

.NET library for creating spectrograms (visual representations of frequency spectrum over time)
C#
311
star
3

Python-GUI-examples

A growing collection of bare-bones GUI examples for python (PyQt4, Python 3)
Python
238
star
4

AVR-projects

A collection of standalone AVR projects written in C
C
161
star
5

pyABF

pyABF is a Python package for reading electrophysiology data from Axon Binary Format (ABF) files
Jupyter Notebook
101
star
6

diyECG-1opAmp

A surprisingly good ECG circuit using a single op-amp and some Python software
Python
98
star
7

SoundCardECG

Display ECG signals in real time using a sound card interface and an AD8232
C#
85
star
8

pyHH

A simple Python implementation of the Hodgkin-Huxley spiking neuron model
Python
36
star
9

code-notes

notes and reusable code for various programming languages
C++
34
star
10

QuickPlot

Experimental Plotting Library for .NET
C#
31
star
11

FtdiSharp

A .NET library for interfacing FTDI USB controller ICs
C#
25
star
12

SWHLab

Python tools for analysis of whole-cell patch-clamp electrophysiological data
Python
20
star
13

SeriPlot

Realtime Serial Data Plotter
C#
20
star
14

Local-LLM-csharp

Run a LLM locally to answer questions about documents using C#
HTML
19
star
15

USB-Counter

Open-source USB frequency counter
C
18
star
16

BasicTimer

A basic timer app for Windows
C#
17
star
17

pyScreenCapture

Screen capture video (with cursor) using Python
Python
17
star
18

md2html-php

A flat-file dynamic markdown-to-HTML site generator using PHP
PHP
15
star
19

pyABFauto

Automatic analysis (and graphing) of ABF files
Python
14
star
20

RasPi-Frequency-Counter

Raspberry PI RF Frequency Counter with Python Interface
Python
14
star
21

HHSharp

Interactive Hodgkin-Huxley neuron simulator
C#
13
star
22

FSKview

A high resolution spectrogram for viewing FSK signals in real time
C#
13
star
23

pyTENMA

Python interface to TENMA multimeter serial data
Python
12
star
24

QRSS-VD

QRSS Viewer for Python 2
Python
12
star
25

Mystify

A modern implementation of a classic screensaver
C#
12
star
26

SwarmFit

A .NET package for fitting curves to data using particle swarm optimization
C#
11
star
27

FTFlash

A tool for reading and writing data in SPI flash memory chips using a FT232H
C#
9
star
28

QRSSplus

QRSS Plus: live QRSS grabbers from around the world
C#
9
star
29

ADC-10-F103C

Software and Notes for 10-Channel ADC Board
C#
9
star
30

WsprSharp

.NET Standard library for encoding/decoding messages using the WSPR protocol
HTML
8
star
31

QRSS-hardware

A collection of resources related to QRSS hardware (TX and RX)
C
8
star
32

LJPcalc

Liquid Junction Potential Calculator
C#
8
star
33

ABFview

Electrophysiology data viewer for Axon Binary Format (ABF) files
C#
7
star
34

SciTIF

A .NET library for working with scientific image data in TIFF files
C#
7
star
35

CsvPlot

Interactively display data from CSV files
C#
7
star
36

NumberSpeaker

Arduino library for reading numbers using a speaker
C
5
star
37

AbfSharp

A .NET interface for Axon Binary Format (ABF) files
HTML
5
star
38

pyHamLog

ham radio logging software with a web interface (designed for school club roundup)
Python
5
star
39

repo-badge

Display GitHub repository statistics using Vanilla JS
5
star
40

swharden

Scott W Harden on GitHub
4
star
41

ScanAGator

A tool for calculating calcium levels (Ī”F/F) from two-photon line scans
C#
4
star
42

ABF-Tag-Editor

A standalone application to modify comment tags in ABF files
C#
4
star
43

QRSS-Uploader

Uploads QRSS images to a FTP server every 10 minutes
C#
4
star
44

vsABF

A modern .NET interface to files in the Axon Binary Format (ABF)
C#
4
star
45

ImageJ-Python-plugin

boilerplate python plugin for ImageJ / FIJI
Python
4
star
46

Washcloth

A .NET Standard library for reading XML documentation using reflection
C#
4
star
47

Lopora

QRSS Beacon Reception Program
Python
4
star
48

FlaskABF

Python-based web application to browse and analyze electrophysiology data
HTML
4
star
49

LivePictureViewer

A picture viewer that rapidly updates when the image file on disk changes
C#
4
star
50

webinspect

Python module to allow users to inspect python objects in a web browser
Python
4
star
51

SWHarden.com

The personal website of Scott W Harden
HTML
4
star
52

ROI-Analysis-Pipeline

Tools for automated analysis of fluorescent microscopy data
Shell
3
star
53

ABF-SDK

Resources for working with Axon Binary Format (ABF) Files
C++
3
star
54

QRSSplus-Downloader

A Windows application to automatically download QRSS grabber images
C#
3
star
55

Tmoji

A taskbar app to quickly copy emoji and special symbols
C#
3
star
56

WpfPingMonitor

Windows application to display ping latency
C#
3
star
57

RasterSharp

A .NET library for working with images in memory
C#
3
star
58

ephys-projects

personal projects related to electrophysiology data analysis and visualization
C#
2
star
59

NDepend-Badges

C#
2
star
60

Statix

C# Static Site Generator
C#
2
star
61

SWHarden-User-Controls

A collection of custom user controls for .NET
C#
2
star
62

psk-experiments

Phase-shift keying (PSK) notes and resources
C#
2
star
63

Csharp-Image-Analysis

Code examples and notes for analyzing image data using C#
C#
2
star
64

StarGraph

A cloud application for generating historical graphs of GitHub stars
C#
2
star
65

Palila

Minimal, hackable, dependency-free Python static site generator
HTML
2
star
66

ABF-Scale-Fixer

An application to modify header values of ABF2 files
C#
2
star
67

iot-weather-station

Wi-Fi weather station using NodeMCU and ESP8266
HTML
2
star
68

ScottPlotStats

Azure Functions for logging and plotting ScottPlot NuGet package downloads
C#
2
star
69

memtest

Notes and code for calculating cell membrane parameters from electrophysiological recordings
Jupyter Notebook
2
star
70

JLJP

Java application for calculating liquid junction potential (LJP)
Java
2
star
71

Java-Boilerplate

Example Java desktop app with tests and CI using GitHub Actions
Java
1
star
72

swharden.com-2021

code and resources related to my personal website
PHP
1
star
73

Bootstrapped-KS2

Tools for bootstrapping the Kolmogorov-Smirnov statistic on 2 samples
C#
1
star
74

duplicate-code-finder

A simple Python script to identify duplicate lines in the code base of any programming language
Python
1
star
75

AliCalc

Aliquot Calculator for Electrophysiology
HTML
1
star
76

patch

A Student's Guide to Patch-Clamp Electrophysiology
HTML
1
star
77

QRSS-Viewer

An open source QRSS spectrograph for Windows
C#
1
star
78

Microscopy

miscellaneous notes and tools to aid the scientific microscopist
Python
1
star
79

ColorblindEnhancer

A Windows tool that enhances color perception for people with color vision deficiencies
C#
1
star
80

originpro

Source code for the originpro Python package
Python
1
star
81

Maui.Graphics

Source for https://maui.graphics
HTML
1
star
82

SWHLabPHP

Dynamically generated web platform to browse electrophysiology data
PHP
1
star
83

govee-plot

Tools for interactively plotting Govee temperature and humidity data
Jupyter Notebook
1
star
84

double-pendulum-dotnet

C# implementation of the double pendulum problem
C#
1
star
85

AbfAuto

automated analysis of electrophysiology data in ABF files
C#
1
star
86

AbfConvert

Tools for automated conversion of ABF files to ATF, CSV, and other useful formats
C#
1
star
87

MatrixWall

Falling green Unicode characters on a dark background implemented using Vanilla JavaScript
JavaScript
1
star
88

PrairieView-Quick-Loader

ImageJ plugin to quickly load multiphoton imaging data from PrairieView folders
Java
1
star
89

Exponential-Fit-CSharp

Exponential fit code examples using C#
C#
1
star
90

AbfVideo

Generate real-time video files from electrophysiology data
C#
1
star
91

qrss.org

A collection of resources for ultra-narrowband amateur radio experimentation
HTML
1
star
92

swharden.RoiSelect

A .NET Package that facilitates working with regions of interest (ROIs) for imaging data in GUI applications
C#
1
star
93

ABF-Spectrogram

Tools for generating FFTs and Spectrograms from ABF files
C#
1
star
94

GitHub-License-Checker

A web app for identifying GitHub repositories lacking license or other important files
HTML
1
star