I want to implement Fast Hartley Transform (Specifically Discrete Hartley Transform) in a script file in MATLAB. Does anyone know have a reference implementation of this in MATLAB or another language so that a complete novice can understand it?
-
4$\begingroup$ if you ask me, that makes little to no sense. The fact that you can save quite a few operation with the FHT will be immensely outweighed by the fact that you're doing this within matlab's interpreter – which is notoriously slow. Things like the FFT are implemented in highly-optimized native code, not in matlab scripts. Any matlab-script FFT would be much, much slower than just doing the DFT naively by multiplying with a DFT matrix (because matrix multiplication is, again, implemented natively). takeaway:matlab itself is very slow.If you need fast,implement natively and call from within matlab. $\endgroup$– Marcus MüllerCommented Jan 7, 2017 at 23:32
-
$\begingroup$ Note: Today there is no reason to expect the Hartley transform to be faster than a real-to-complex FFT. A long time ago it might have been faster, but that is no longer the case. See for example this warning in the FFTW docs. $\endgroup$– Cris LuengoCommented Sep 25, 2023 at 20:16
2 Answers
You can resort to the properties of the DHT with the DFT, given in the wikipedia article. For a real-valued function $f$, its DHT in terms of the DFT is given by
$$ \mathcal{H}f = \Re\{\mathcal{F}f\}-\Im\{\mathcal{F}f\}. $$
So, a function for fast Hartley transform can be given by
function Hf = fht(f)
F = fft(f);
Hf = real(F) - imag(F);
end
Though, you need to take care of the normalization factors, and tdhis is up to you and how you define the scale factors of the DHT.
-
1$\begingroup$ Make the answer complete by adding the inverse transform. Good answer! $\endgroup$– RoyiCommented Nov 18, 2019 at 14:39
-
$\begingroup$ @Royi The Hartley transform is its own inverse.
fht(fht(f)) == f
. $\endgroup$ Commented Sep 25, 2023 at 20:18 -
$\begingroup$ @CrisLuengo, Indeed. Just thought it makes sense to express it as part of the answer. $\endgroup$– RoyiCommented Sep 28, 2023 at 4:23
For the standard algorithmic version, you can get information in An Algorithm for the Fast Hartley transform, R. F. Ulmann (with Fortran versions). A Matlab version of the FHT is provided by Lars Gregersen, exactly as given by @MaximilianMatthé, except that it checks for a real signal input. C++ and C# versions can also be found in ALGLIB: Fast Hartley transform.
For improved versions, that can provide speed-up beyond algorithmic optimization, one can consider other factors, such as memory tricks, hardware optimization, use of binary inputs/outputs, higher-dimension extensions. You can check for instance: