blob: 4d0d8bf38ee90b62a681bd3c9405802d8f74fdf0 [file] [log] [blame]
andrew@webrtc.org325cff02014-10-01 17:42:181/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef COMMON_AUDIO_REAL_FOURIER_H_
12#define COMMON_AUDIO_REAL_FOURIER_H_
andrew@webrtc.org325cff02014-10-01 17:42:1813
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
andrew@webrtc.org325cff02014-10-01 17:42:1815#include <complex>
kwibergbfefb032016-05-01 21:53:4616#include <memory>
andrew@webrtc.org325cff02014-10-01 17:42:1817
Karl Wiberg29e7bee2018-03-22 13:11:5218#include "rtc_base/memory/aligned_malloc.h"
andrew@webrtc.org325cff02014-10-01 17:42:1819
20// Uniform interface class for the real DFT and its inverse, for power-of-2
21// input lengths. Also contains helper functions for buffer allocation, taking
22// care of any memory alignment requirements the underlying library might have.
23
24namespace webrtc {
25
26class RealFourier {
27 public:
28 // Shorthand typenames for the scopers used by the buffer allocation helpers.
kwibergbfefb032016-05-01 21:53:4629 typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper;
30 typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter>
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:5531 fft_cplx_scoper;
andrew@webrtc.org325cff02014-10-01 17:42:1832
andrew@webrtc.org325cff02014-10-01 17:42:1833 // The alignment required for all input and output buffers, in bytes.
pkasting25702cb2016-01-08 21:50:2734 static const size_t kFftBufferAlignment;
andrew@webrtc.org325cff02014-10-01 17:42:1835
36 // Construct a wrapper instance for the given input order, which must be
37 // between 1 and kMaxFftOrder, inclusively.
kwibergbfefb032016-05-01 21:53:4638 static std::unique_ptr<RealFourier> Create(int fft_order);
oprypin67fdb802017-03-09 14:25:0639 virtual ~RealFourier() {}
andrew@webrtc.org325cff02014-10-01 17:42:1840
andrew@webrtc.org04c50982015-03-19 20:06:2941 // Helper to compute the smallest FFT order (a power of 2) which will contain
42 // the given input length.
Peter Kastingdce40cf2015-08-24 21:52:2343 static int FftOrder(size_t length);
andrew@webrtc.org325cff02014-10-01 17:42:1844
andrew@webrtc.org04c50982015-03-19 20:06:2945 // Helper to compute the input length from the FFT order.
Peter Kastingdce40cf2015-08-24 21:52:2346 static size_t FftLength(int order);
andrew@webrtc.org04c50982015-03-19 20:06:2947
48 // Helper to compute the exact length, in complex floats, of the transform
49 // output (i.e. |2^order / 2 + 1|).
Peter Kastingdce40cf2015-08-24 21:52:2350 static size_t ComplexLength(int order);
andrew@webrtc.org325cff02014-10-01 17:42:1851
52 // Buffer allocation helpers. The buffers are large enough to hold |count|
53 // floats/complexes and suitably aligned for use by the implementation.
54 // The returned scopers are set up with proper deleters; the caller owns
55 // the allocated memory.
56 static fft_real_scoper AllocRealBuffer(int count);
57 static fft_cplx_scoper AllocCplxBuffer(int count);
58
59 // Main forward transform interface. The output array need only be big
60 // enough for |2^order / 2 + 1| elements - the conjugate pairs are not
61 // returned. Input and output must be properly aligned (e.g. through
62 // AllocRealBuffer and AllocCplxBuffer) and input length must be
63 // |2^order| (same as given at construction time).
andrew@webrtc.org04c50982015-03-19 20:06:2964 virtual void Forward(const float* src, std::complex<float>* dest) const = 0;
andrew@webrtc.org325cff02014-10-01 17:42:1865
66 // Inverse transform. Same input format as output above, conjugate pairs
67 // not needed.
andrew@webrtc.org04c50982015-03-19 20:06:2968 virtual void Inverse(const std::complex<float>* src, float* dest) const = 0;
andrew@webrtc.org325cff02014-10-01 17:42:1869
andrew@webrtc.org04c50982015-03-19 20:06:2970 virtual int order() const = 0;
andrew@webrtc.org325cff02014-10-01 17:42:1871};
72
73} // namespace webrtc
74
Mirko Bonadei92ea95e2017-09-15 04:47:3175#endif // COMMON_AUDIO_REAL_FOURIER_H_