/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include "dft.hpp"
using namespace std;
int data[] = {4274, 4290, 2117, 5094, 5574, 4928, 7283, 7460, 7624, 9404, 8888, 8174, 8703, 7536, 7115, 9027, 7873, 7858, 7255, 6641, 6457, 5935, 6604, 6579, 6278, 8127, 8022, 8719, 10545, 10286, 9602, 7867, 6295, 6601, 8225, 9103, 10536, 10467, 8674, 9501, 9700, 9758, 10265, 10339, 8858, 8797, 9142, 7179, 8745, 7202, 7989, 8391, 6933, 8756, 7343, 6645, 7124, 5815, 6192, 6154, 6567, 6743, 7688, 8569, 8503, 9496, 7979, 7053, 6304, 4128, 4253, 5080, 5700, 7655, 8630, 7816, 7206, 7216, 6519, 8317, 8550, 8338, 7347, 7083, 7797, 7571, 7700, 7261, 7046, 7903, 8527, 9243, 10049, 9570, 10630, 11404, 12399, 13941, 12923, 10695, 7258, 4756, 4866, 4565, 6351, 5533, 6564, 8714, 8686, 11090, 12194, 10268, 8856, 6671, 4957, 4722, 3970, 3164, 2814, 4030, 5386, 7058, 5996, 5582, 6785, 8637, 9710, 11513, 11400, 11276, 13376, 12320, 11902, 9423, 8645, 7687, 5406, 4298, 1317, 195, 195, 2116, 2116, 3699, 6496, 6003, 9481, 10427, 11052, 13044, 11733, 12253, 12482, 11542, 10722, 8959, 6451, 4539, 5059, 5695, 4563, 4180, 2311, 194, 684, 2870, 3958, 6870, 7513, 7067, 8773, 7950, 10066, 12232, 11239, 11125, 10672, 8915, 9153, 8300, 6072, 4878, 4899, 5442, 6302, 8998, 9849, 12003, 13558, 10892, 10039, 7453, 6304, 6812, 5932, 6062, 7721, 7862, 8667, 10135, 8912, 9323, 8343, 7481, 5200, 4358, 4947, 5756, 9071, 10187, 10540, 9713, 9670, 9722, 9636, 8460, 5189, 3885, 3768, 5517, 8526, 9113, 9805, 10378, 10146, 9757, 9122, 6426, 5135, 4879, 4981, 6112, 6985, 9418, 11432, 13343, 13456, 11674, 7967, 4524, 3749, 2133, 2977, 4788, 4610, 7419, 9041, 9712, 9744, 8208, 7712, 7132, 5580, 6050, 5863, 5456, 7278, 7680, 7837, 8793, 10379, 9478, 8859, 7059, 4675, 4758, 3381, 3394, 3647, 3717, 6751, 9475, 11135, 12325, 13234, 13909, 13805, 12657, 10126, 7290, 5939, 4811, 3588, 3193, 1828, 1949, 3346, 3724, 3780, 4901, 5414, 7774, 10786, 12358, 13355, 11047, 8470, 6478, 3585, 3976, 4211, 3844, 5403, 6965, 10219, 12292, 11702, 11174, 7699, 6056, 4732, 3437, 3970, 4491, 6993, 7382, 8287, 8771, 9557, 9509, 8944, 8509, 5368, 4761, 3552, 2911, 4772, 6712, 10211, 12904, 13055, 11001, 9745, 6480, 5866, 6913, 5384, 4603, 3059, 1076, 1575, 1812, 4196, 4826, 5761, 8003, 8180, 10834, 10770, 11256, 12179, 11650, 12922, 11938, 9401, 7773, 6101, 4373, 4858, 5352, 3384, 3131, 3256, 3617, 5439, 6168, 8027, 7562, 8920, 12047, 11352, 13224, 13651, 12324, 12153, 11359, 9901, 9318, 7883, 5707, 3999, 3714, 3650, 4018, 4819, 4256, 5579, 6393, 6846, 8188, 9500, 11565, 11757, 11652, 9890, 9352, 10236, 8918, 9890, 7952, 7352, 7254, 6022, 6417, 5540, 6248, 4583, 3056, 2775, 2069, 4715, 6245, 8936, 10365, 10618, 10842, 9369, 9700, 10316, 10752, 9222, 8024, 5448, 4023, 4781, 4963, 5199, 4594, 3135, 3620, 3570, 6238, 8234, 8816, 11581, 12339, 13059, 12831, 12832, 12923, 11929, 11078, 8846, 6326, 5452, 4646, 4260, 5488, 7840, 10583, 11589, 11459, 11542, 10568, 9638, 8875, 7193, 4700, 5038, 5716, 7317, 9445, 9746, 10793, 10032, 10078, 10190, 7720, 6823, 5502, 4600, 4294, 2752, 2433, 1632, 2020, 2020, 3252, 4992, 5993, 7613, 8774, 8639, 9790, 10964, 11355, 11266, 10370, 8474, 6960, 6861, 4638, 5073, 4657, 6126, 8685, 11092, 12603, 12287, 12137, 9878, 6858, 3890, 2504, 2776, 5404, 6866, 8362, 10704, 9835, 10881, 10437, 8066, 6308, 3800, 2030, 666, 1670, 5099, 8454, 10535, 11933, 9902, 6942, 5543, 4244, 4289, 4890, 4883, 6149, 7943, 10066, 12235, 12223, 10954, 9822, 7183, 6761, 4793, 2975, 3642, 3977, 5995, 8297, 11160, 10960, 10913, 9442, 7496, 6116, 4368, 3452, 3699, 5088, 7476, 9254, 9794, 10294, 10903, 10191, 8818, 6415, 3849, 2928, 1366, 3580, 6511, 8154, 12055, 13432, 12397, 12334, 9915, 7781, 5310, 4204, 5105, 5547, 7258, 9556, 10329, 10740, 10191, 9920, 7167, 5482, 4241, 1205, 3056, 5202, 7021, 9685, 10938, 10977, 10430, 10804, 9751, 7306, 6210, 4415, 2711, 3226, 4085, 3787, 4395, 5759, 7226, 8146, 10033, 10263, 10427, 11198, 11127, 10589, 11025, 11606, 11026, 10180, 7869, 6633, 4060, 2806, 4385, 3749, 5576, 7678, 8843, 11595, 13439, 13644, 12513, 8578, 4968, 3596, 1245, 3776, 5861, 7050, 10013, 9662, 9531, 8698, 6665, 4854, 2814, 2151, 1786, 3610, 5284, 6899, 10294, 10494, 12508, 13219, 11333, 10109, 6666, 5292, 3435, 2908, 5824, 7292, 9211, 10321, 9969, 9000, 8777, 9433, 9784, 10806, 9709, 9493, 6871, 3761, 3994, 2465, 2247, 4212, 3806, 4837, 7337, 8153, 10148, 11383, 12573, 13887, 15292, 16093, 14219, 13845, 11899, 10234, 10818, 9488, 7918, 5690, 3739, 1349, 1885, 2440, 3028, 3695, 3170, 5729, 6981, 8187, 11462, 11640, 12076, 13647, 13156, 12102, 10086, 8653, 8039, 7093, 7618, 7378, 5274, 4896, 3341, 1571, 3292, 2632, 4031, 5918, 6619, 8188, 9691, 10909, 11496, 12552, 12630, 12805, 11091, 10537, 9559, 7232, 5869, 4060, 3527, 4157, 3567, 4472, 3329, 3787, 6021, 5818, 8302, 9317, 9832, 12106, 12239, 12980, 13940, 13333};
int main()
{
const int windowLen = 22;
int dataLen = sizeof(data)/sizeof(data[0]);
static SlidingDFT<double, windowLen> dft;
int cnt = 0;
for (int n=0; n<dataLen; n++){
//printf("%i, %i\n", n, data[n]);
printf("\n");
dft.update(data[n]);
if(cnt++ == windowLen){
std::complex<double> DC_bin = dft.dft[21];
std::cout << std::abs(DC_bin) << '\n';
cnt = 0;
}
}
return 0;
}
/**
Sliding discrete Fourier transform (C++)
====
This code efficiently computes discrete Fourier transforms (DFTs) from a
continuous sequence of input values. It is a recursive algorithm that updates
the DFT when each new time-domain measurement arrives, effectively applying a
sliding window over the last *N* samples. This implementation applies the
Hanning window in order to minimise spectral leakage.
The update step has computational complexity *O(N)*. If a new DFT is required
every *M* samples, and *M* < log2(*N*), then this approach is more efficient
that recalculating the DFT from scratch each time.
This is a header-only C++ library. Simply copy sliding_dft.hpp into your
project, and use it as follows:
// Use double precision arithmetic and a 512-length DFT
static SlidingDFT<double, 512> dft;
// avoid allocating on the stack because the object is large
// When a new time sample arrives, update the DFT with:
dft.update(x);
// After at least 512 samples have been processed:
std::complex<double> DC_bin = dft.dft[0];
Your application should call update() as each time domain sample arrives. Output
data is an array of `std::complex` values in the `dft` field. The length of this
array is the length of the DFT.
The output data is not valid until at least *N* samples have been processed. You
can detect this using the `is_data_valid()` method, or by storing the return
value of the `update()` method.
This is a header-only C++ library. Simply copy sliding_dft.hpp into your
project. The included CMakeLists.txt is for building the testbench.
Implementation details
----
See references [1, 2] for an overview of sliding DFT algorithms. A damping
factor is used to improve stability in the face of numerical rounding errors. If
you experience stability issues, reduce `dft.damping_factor`. It should be
slightly less than one.
Windowing is done using a Hanning window, computed in the frequency domain [1].
[1] E. Jacobsen and R. Lyons, “The Sliding DFT,” IEEE Signal Process. Mag., vol. 20, no. 2, pp. 74–80, Mar. 2003.
[2] E. Jacobsen and R. Lyons, “An Update to the Sliding DFT,” IEEE Signal Process. Mag., vol. 21, no. 1, pp. 110-111, 2004.
MIT License
----
Copyright (c) 2016 Bronson Philippa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#define _USE_MATH_DEFINES
#include <complex>
#include <math.h>
template <class NumberFormat, size_t DFT_Length>
class SlidingDFT
{
private:
/// Are the frequency domain values valid? (i.e. have at elast DFT_Length data
/// points been seen?)
bool data_valid = false;
/// Time domain samples are stored in this circular buffer.
NumberFormat x[DFT_Length] = { 0 };
/// Index of the next item in the buffer to be used. Equivalently, the number
/// of samples that have been seen so far modulo DFT_Length.
size_t x_index = 0;
/// Twiddle factors for the update algorithm
std::complex<NumberFormat> twiddle[DFT_Length];
/// Frequency domain values (unwindowed!)
std::complex<NumberFormat> S[DFT_Length];
public:
/// Frequency domain values (windowed)
std::complex<NumberFormat> dft[DFT_Length];
/// A damping factor introduced into the recursive DFT algorithm to guarantee
/// stability.
NumberFormat damping_factor = std::nexttoward((NumberFormat)1, (NumberFormat)0);
/// Constructor
SlidingDFT()
{
const std::complex<NumberFormat> j(0.0, 1.0);
const NumberFormat N = DFT_Length;
// Compute the twiddle factors, and zero the x and S arrays
for (size_t k = 0; k < DFT_Length; k++) {
NumberFormat factor = (NumberFormat)(2.0 * M_PI) * k / N;
this->twiddle[k] = std::exp(j * factor);
this->S[k] = 0;
this->x[k] = 0;
}
}
/// Determine whether the output data is valid
bool is_data_valid()
{
return this->data_valid;
}
/// Update the calculation with a new sample
/// Returns true if the data are valid (because enough samples have been
/// presented), or false if the data are invalid.
bool update(NumberFormat new_x)
{
// Update the storage of the time domain values
const NumberFormat old_x = this->x[this->x_index];
this->x[this->x_index] = new_x;
// Update the DFT
const NumberFormat r = this->damping_factor;
const NumberFormat r_to_N = pow(r, (NumberFormat)DFT_Length);
for (size_t k = 0; k < DFT_Length; k++) {
this->S[k] = this->twiddle[k] * (r * this->S[k] - r_to_N * old_x + new_x);
}
// Apply the Hanning window
this->dft[0] = (NumberFormat)0.5*this->S[0] - (NumberFormat)0.25*(this->S[DFT_Length - 1] + this->S[1]);
for (size_t k = 1; k < (DFT_Length - 1); k++) {
this->dft[k] = (NumberFormat)0.5*this->S[k] - (NumberFormat)0.25*(this->S[k - 1] + this->S[k + 1]);
}
this->dft[DFT_Length - 1] = (NumberFormat)0.5*this->S[DFT_Length - 1] - (NumberFormat)0.25*(this->S[DFT_Length - 2] + this->S[0]);
// Increment the counter
this->x_index++;
if (this->x_index >= DFT_Length) {
this->data_valid = true;
this->x_index = 0;
}
// Done.
return this->data_valid;
}
};