kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

dft.cpp (1887B)


      1 /**
      2  * KFR (https://www.kfrlib.com)
      3  * Copyright (C) 2016-2023 Dan Cazarin
      4  * See LICENSE.txt for details
      5  */
      6 
      7 #include <kfr/base.hpp>
      8 #include <kfr/dft.hpp>
      9 #include <kfr/dsp.hpp>
     10 #include <kfr/io.hpp>
     11 
     12 using namespace kfr;
     13 
     14 int main()
     15 {
     16     // Print the version of the KFR library being used
     17     println(library_version());
     18 
     19     // Define the size of the Fast Fourier Transform (FFT)
     20     const size_t size = 128;
     21 
     22     // Initialize input and output buffers
     23     // 'in' buffer is filled with a sine wave spanning 4 cycles over the given range
     24     // 'out' buffer is initialized with 'qnan' (quiet NaN) to represent uninitialized state
     25     univector<complex<fbase>, size> in  = sin(linspace(0.0, c_pi<fbase, 2> * 4.0, size));
     26     univector<complex<fbase>, size> out = scalar(qnan);
     27 
     28     // Create an FFT plan for the defined size
     29     const dft_plan<fbase> dft(size);
     30 
     31     // Dump the details of the FFT plan (for debugging or information purposes)
     32     dft.dump();
     33 
     34     // Allocate a temporary buffer for the FFT computation if needed
     35     univector<u8> temp(dft.temp_size);
     36 
     37     // Perform the forward FFT on the input buffer 'in', store the result in the 'out' buffer
     38     dft.execute(out, in, temp);
     39 
     40     // Scale the output of the FFT by dividing by the size
     41     out = out / size;
     42 
     43     // Convert the amplitude of the FFT output to decibels (dB)
     44     // 'cabs' computes the magnitude of the complex numbers in 'out'
     45     // 'amp_to_dB' converts the amplitude values to decibel scale
     46     univector<fbase, size> dB = amp_to_dB(cabs(out));
     47 
     48     // Print the maximum, minimum, mean, and root mean square (RMS) of the dB values
     49     println("max  = ", maxof(dB));
     50     println("min  = ", minof(dB));
     51     println("mean = ", mean(dB));
     52     println("rms  = ", rms(dB));
     53 
     54     // Print the input buffer 'in'
     55     println(in);
     56     println();
     57     // Print the dB values
     58     println(dB);
     59     return 0;
     60 }