File indexing completed on 2024-05-12 04:50:07

0001 /*
0002     FHT - Fast Hartley Transform Class
0003 
0004     SPDX-FileCopyrightText: 2004 Melchior FRANZ <mfranz@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef FHT_H
0010 #define FHT_H
0011 
0012 /**
0013  * Implementation of the Hartley Transform after Bracewell's discrete
0014  * algorithm. The algorithm is subject to US patent No. 4,646,256 (1987)
0015  * but was put into public domain by the Board of Trustees of Stanford
0016  * University in 1994 and is now freely available[1].
0017  *
0018  * [1] Computer in Physics, Vol. 9, No. 4, Jul/Aug 1995 pp 373-379
0019  */
0020 class FHT
0021 {
0022     int m_exp2;
0023     int m_num;
0024     float *m_buf;
0025     float *m_tab;
0026     int *m_log;
0027 
0028     /**
0029      * Create a table of "cas" (cosine and sine) values.
0030      * Has only to be done in the constructor and saves from
0031      * calculating the same values over and over while transforming.
0032      */
0033     void makeCasTable();
0034 
0035     /**
0036      * Recursive in-place Hartley transform. For internal use only!
0037      */
0038     void _transform(float *, int, int);
0039 
0040 public:
0041     /**
0042      * Prepare transform for data sets with @f$2^n@f$ numbers, whereby @f$n@f$
0043      * should be at least 3. Values of more than 3 need a trigonometry table.
0044      * @see makeCasTable()
0045      */
0046     explicit FHT(int);
0047 
0048     ~FHT();
0049     inline int sizeExp() const
0050     {
0051         return m_exp2;
0052     }
0053     inline int size() const
0054     {
0055         return m_num;
0056     }
0057     float *copy(float *, float *);
0058     float *clear(float *);
0059     void scale(float *, float);
0060 
0061     /**
0062      * Exponentially Weighted Moving Average (EWMA) filter.
0063      * @param d is the filtered data.
0064      * @param s is fresh input.
0065      * @param w is the weighting factor.
0066      */
0067     void ewma(float *d, float *s, float w);
0068 
0069     /**
0070      * Logarithmic audio spectrum. Maps semi-logarithmic spectrum
0071      * to logarithmic frequency scale, interpolates missing values.
0072      * A logarithmic index map is calculated at the first run only.
0073      * @param p is the input array.
0074      * @param out is the spectrum.
0075      */
0076     void logSpectrum(float *out, float *p);
0077 
0078     /**
0079      * Semi-logarithmic audio spectrum.
0080      */
0081     void semiLogSpectrum(float *);
0082 
0083     /**
0084      * Fourier spectrum.
0085      */
0086     void spectrum(float *);
0087 
0088     /**
0089      * Calculates a mathematically correct FFT power spectrum.
0090      * If further scaling is applied later, use power2 instead
0091      * and factor the 0.5 in the final scaling factor.
0092      * @see FHT::power2()
0093      */
0094     void power(float *);
0095 
0096     /**
0097      * Calculates an FFT power spectrum with doubled values as a
0098      * result. The values need to be multiplied by 0.5 to be exact.
0099      * Note that you only get @f$2^{n-1}@f$ power values for a data set
0100      * of @f$2^n@f$ input values. This is the fastest transform.
0101      * @see FHT::power()
0102      */
0103     void power2(float *);
0104 
0105     /**
0106      * Discrete Hartley transform of data sets with 8 values.
0107      */
0108     void transform8(float *);
0109 
0110     void transform(float *);
0111 };
0112 
0113 #endif