blob: 1b851e919e20b7db056f8dd92a4227bfa1ac285c [file] [log] [blame]
andrew@webrtc.org1bbdf572014-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
11#ifndef WEBRTC_COMMON_AUDIO_REAL_FOURIER_H_
12#define WEBRTC_COMMON_AUDIO_REAL_FOURIER_H_
13
14#include <complex>
kwibergb670f852016-05-01 21:53:4615#include <memory>
andrew@webrtc.org1bbdf572014-10-01 17:42:1816
Henrik Kjellander78f65d02015-10-28 17:17:4017#include "webrtc/system_wrappers/include/aligned_malloc.h"
andrew@webrtc.org1bbdf572014-10-01 17:42:1818
19// Uniform interface class for the real DFT and its inverse, for power-of-2
20// input lengths. Also contains helper functions for buffer allocation, taking
21// care of any memory alignment requirements the underlying library might have.
22
23namespace webrtc {
24
25class RealFourier {
26 public:
27 // Shorthand typenames for the scopers used by the buffer allocation helpers.
kwibergb670f852016-05-01 21:53:4628 typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper;
29 typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter>
kwiberg@webrtc.orgdeb9dae2015-02-26 14:34:5530 fft_cplx_scoper;
andrew@webrtc.org1bbdf572014-10-01 17:42:1831
andrew@webrtc.org1bbdf572014-10-01 17:42:1832 // The alignment required for all input and output buffers, in bytes.
pkastingfa9f5a82016-01-08 21:50:2733 static const size_t kFftBufferAlignment;
andrew@webrtc.org1bbdf572014-10-01 17:42:1834
35 // Construct a wrapper instance for the given input order, which must be
36 // between 1 and kMaxFftOrder, inclusively.
kwibergb670f852016-05-01 21:53:4637 static std::unique_ptr<RealFourier> Create(int fft_order);
oprypin0f20d582017-03-09 14:25:0638 virtual ~RealFourier() {}
andrew@webrtc.org1bbdf572014-10-01 17:42:1839
andrew@webrtc.org2db66fc2015-03-19 20:06:2940 // Helper to compute the smallest FFT order (a power of 2) which will contain
41 // the given input length.
Peter Kastinga0ad2482015-08-24 21:52:2342 static int FftOrder(size_t length);
andrew@webrtc.org1bbdf572014-10-01 17:42:1843
andrew@webrtc.org2db66fc2015-03-19 20:06:2944 // Helper to compute the input length from the FFT order.
Peter Kastinga0ad2482015-08-24 21:52:2345 static size_t FftLength(int order);
andrew@webrtc.org2db66fc2015-03-19 20:06:2946
47 // Helper to compute the exact length, in complex floats, of the transform
48 // output (i.e. |2^order / 2 + 1|).
Peter Kastinga0ad2482015-08-24 21:52:2349 static size_t ComplexLength(int order);
andrew@webrtc.org1bbdf572014-10-01 17:42:1850
51 // Buffer allocation helpers. The buffers are large enough to hold |count|
52 // floats/complexes and suitably aligned for use by the implementation.
53 // The returned scopers are set up with proper deleters; the caller owns
54 // the allocated memory.
55 static fft_real_scoper AllocRealBuffer(int count);
56 static fft_cplx_scoper AllocCplxBuffer(int count);
57
58 // Main forward transform interface. The output array need only be big
59 // enough for |2^order / 2 + 1| elements - the conjugate pairs are not
60 // returned. Input and output must be properly aligned (e.g. through
61 // AllocRealBuffer and AllocCplxBuffer) and input length must be
62 // |2^order| (same as given at construction time).
andrew@webrtc.org2db66fc2015-03-19 20:06:2963 virtual void Forward(const float* src, std::complex<float>* dest) const = 0;
andrew@webrtc.org1bbdf572014-10-01 17:42:1864
65 // Inverse transform. Same input format as output above, conjugate pairs
66 // not needed.
andrew@webrtc.org2db66fc2015-03-19 20:06:2967 virtual void Inverse(const std::complex<float>* src, float* dest) const = 0;
andrew@webrtc.org1bbdf572014-10-01 17:42:1868
andrew@webrtc.org2db66fc2015-03-19 20:06:2969 virtual int order() const = 0;
andrew@webrtc.org1bbdf572014-10-01 17:42:1870};
71
72} // namespace webrtc
73
74#endif // WEBRTC_COMMON_AUDIO_REAL_FOURIER_H_
75