difft

Version 0.1.1 (471 KB) by Gus Zhang
Double Interpolation FFT implementations - Accurate harmonic extraction with restricted sampling rates and sizes. Suitable for embedded syst
13 Downloads
Updated 9 May 2025
DIFFT: Double-Interpolation FFT for Accurate Harmonic Extraction
DIFFT is an open-source project that includes different implementations of the Double-Interpolation FFT method, introduced in the paper "An Improved Accuracy Harmonic Extraction Method for Microcontrollers" by Tian and Zhang (2025). The method is designed to extract harmonic amplitudes and phases with high accuracy, even on devices with limited computing resources like microcontrollers.
difft.m Usage
% Perform DIFFT extraction on the input signal
% difft.m
% [f, Amp, phi, dc] = difft(x, Fs, sigma)
% x: input signal, the length of x must be a power of 2; the signal must contain at least two cycles of the fundamental component.
% Fs: sampling frequency
% sigma: the standard deviation of the Gaussian window
% f: extracted base frequency
% Amp: amplitude of the extracted harmonics
% phi: phase of the extracted harmonics
% dc: DC component of the input signal
Note: Project in Development
This project is currently a work in progress. We are actively developing code, tests, and documentation. There may be frequent updates, changes, and incomplete features. Feedback and contributions are welcome—please feel free to report issues or suggest improvements.
Main Features
  • Provides accurate harmonic extraction for signals with unknown fundamental frequency.
  • Works well on microcontrollers with limited memory, such as STM32 and TMS320.
  • Uses fast FFT libraries that take advantage of hardware acceleration.
Background
In many monitoring and control systems, especially on low-power microcontrollers, FFT-based methods for analyzing harmonics can produce large errors. These errors happen due to spectral leakage and misalignment between frequency bins and the signal's true harmonics. For instance, using an 8K-point FFT on a square wave with a poorly matched bin size can cause the fundamental frequency amplitude to be over 20% off.
The DIFFT method addresses this problem in three steps:
  1. It estimates the actual fundamental frequency using Gaussian interpolation.
  2. It resamples the signal using sample rate conversion so that harmonic frequencies align properly with FFT bins.
  3. It performs a second FFT to extract accurate amplitude and phase information.
This approach has been tested and verified in software (GNU Octave) and hardware (STM32F412 microcontroller and TMS320 DSP chips).
Benchmark Results
Compared to older techniques like straight FFT or phase-locked loops (PLLs), DIFFT offers several advantages:
  • Less than 1% error for harmonics up to the 11th order.
  • Runs 3 to 5 times faster on STM32F4 than previous methods.
  • Uses less than 100 kilobytes of RAM for 8K samples
License
This project is released under the MIT License. See the LICENSE file for full details.
Contributing
Contributions are welcome. If you'd like to help out, please refer to the CONTRIBUTING.md file for guidelines.
Citation
If you use DIFFT in academic or professional work, please cite the following publication:
Y. Tian and C. Zhang, "An Improved Accuracy Harmonic Extraction Method for Microcontrollers," IEEE Transactions on Industrial Electronics, vol. 72, no. 5, pp. 5344-5352, May 2025. DOI: 10.1109/TIE.2024.3472288
The frequency interpolation method is from the following publication:
M. Gasior, J. L. Gonzalez, “Improving FFT Frequency Measurement Resolution by Parabolic and Gaussian Spectrum Interpolation,” AIP Conference Proceedings, vol. 732, no. 1, pp. 276–285, Nov. 2004.

Cite As

Gus Zhang (2025). difft (https://github.com/guszhang/difft/releases/tag/v0.1.1), GitHub. Retrieved .

Tian, Yidong, and Cheng Zhang. “An Improved Accuracy Harmonic Extraction Method for Microcontrollers.” IEEE Transactions on Industrial Electronics, vol. 72, no. 5, May 2025, pp. 5344–52, https://doi.org/10.1109/tie.2024.3472288.

View more styles

M. Gasior, J. L. Gonzalez, “Improving FFT Frequency Measurement Resolution by Parabolic and Gaussian Spectrum Interpolation,” AIP Conference Proceedings, vol. 732, no. 1, pp. 276–285, Nov. 2004.

MATLAB Release Compatibility
Created with R2024b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

matlab/examples

matlab/src

Version Published Release Notes
0.1.1

See release notes for this release on GitHub: https://github.com/guszhang/difft/releases/tag/v0.1.1

0.1.0

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.